Programming Using GNU Octave

 

Questions: 1

A small object is projected from a toy gun to travel some distance and land on the ground.

1. Write a function to model the dynamics of the object in Octave called f_trajectory. f_trajectory will accept following arguments in the same order (measured in the given units). Also specify the default values for each parameter as given in brackets.

i. h(5) : Initial height of the projectile (m)

ii. g(9.81) : acceleration from gravity (m/s/s)

iii. v(25) : speed at initial release (m/s)

iv. theta(π/4) : angle of release (radians)

v. t : a vector representing the points in time

For default values of t check if the variable ‘exist’ with name ‘t’ and type ‘var’ if not initialize it as a row vector starting from 0 and goes to 5 with increments of 0.1.

If x is distance and y is height, the equations below describe their dependence on time and all the other parameters.

𝑥(𝑡)=𝑣𝑐𝑜𝑠(𝜃)𝑡 ----------------(eq1)

𝑦(𝑡)=ℎ+𝑣𝑠𝑖𝑛(𝜃)𝑡− 12𝑔𝑡2 ---------------(eq2)

Convert the above equations to Octave code of f_trajectory for evaluating x and y for given t. f_trajectory will return 3 output variables x, y, t in that order.


2. Use the f_trajectory in a second function called traject as below steps.

i. Call f_trajectory in traject to find x, y, t vectors (with default params).

ii. Use code given in “Figure 1: plot 1 code” to generate two subplots.

iii. Take two screenshots as eq1 and eq2 by rotating the 3d plot in right using tool, such that two variables appear at once i.e. (x-t) or (y-t). Rotate the viewport to match the 2d plot in the left.


3. To approximate when the projectile hits the ground find the index of y where height first becomes negative (use find).

i. Find the distance at which the projectile hits the ground is value of x at that index, say xcross.

ii. printf the statement : 'X nearest crossing point is %d' where argument is xcross.

iii. Make a separate figure (use figure) and plot in black solid line, y against x.

iv. Mark the x axis as distance, y axis as height and title as trajectory.

v. Plot x = 0 line in blue dash lines in the same figure.

vi. Plot y = xcross line in green dash lines.


Questions: 2

1. Write a function named f_charcount that counts the number of characters in a given text file. The function takes two arguments, fname a char array representing the file name/path and cquery a character vector representing the characters to be searched. The function returns a number array named cresult a vector that contains the count of each char in cquery. If the file is not found and the input is not a char array the function must return -1 and -2 respectively. Use simple.txt and AStudyInScarlet.txt to test your function.

2. Write a function named f_avgdist that accepts an excel file with similar format as dist_1.xlsx and read it using the xlsread function. (You will need to install the io package in Octave and enable it) Your function should return a 2 cell arrays that points to each place (string) and to the average distance (numeric) from that place to the other places. (For each cell array the same cell index should return the corresponding average distance of each place)

3. Write a simple shuffling ‘encryption’ algorithm as function f_crypto which takes a message msg as a character array and returns a shuffled message msgen and a random integer vector kou of the same length of the original message. The encoded message and the key kou can be used for decryption. If a message and a kin parameter is given it is assumed that the operation is to decrypt, given the msg only it will be the encryption operation. Use randperm and exist functions to generate a shuffled indexed key vector and check the function calling conditions.  The generated key is a randomized indices for each character of the message to be rearranged. Optional: The encryption and decryption operations can be vectorized. Following is an example of using the function.


Comments

Popular posts from this blog

Library Problem

What Is A Gamma Ray Burst?