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.
Comments
Post a Comment