Need help writing a MatLab code b x 12 gt2 where g 981 and
Need help writing a MatLab code
b. x = 1/2 gt^2
where g = 9.81 and time is defined in seconds. Allow the user to input the total time in which he/she would like to know the position of the object. Fix your step-size at 0.1 seconds and start your vector at zero (ending at the value of “total time”, which should be input as a scalar). Additionally, create a selection structure that allows the user to select which graph to plot - either the position, velocity or acceleration vs. the time vector you create. Plot the position, velocity and acceleration in 3 separate code executions for a total time of 5 seconds.
Solution
%inputing time
time = input(\'The total time to know the position: \');
%intialising vectors for position, vel and acceleration
x = [];
v = [];
a = [];
ts = [];
g = 9.81;
%iterating through times to calculate the values at each time instance
for t=0:0.1:time
ts(end+1) = t;
x(end+1) = 0.5*g*t^2;
v(end+1) = g*t;
a(end+1) = g;
end
%getting plot type as input from the user
plot_type = input(\'Input 1 for plotting position v/s time.\ Input 2 for plotting velocity v/s time.\ Input 3 for plotting acceleration v/s time\ \');
%plotting the corresponding graph based on the user input
if(plot_type == 1)
plot(ts, x);
elseif(plot_type == 2)
plot(ts, v);
elseif(plot_type == 3)
plot(ts, a);
else
printf(\'You did not enter a valid keyword\');
end
