How do I graph a constant within a function?
Show older comments
I was given
x = .5*g*t^2
where x is the position of an object at time t and g is the acceleration.
I was asked to "create a function that has time as an input and returns the position (along x) of the object, the x-axis velocity at time “t” and the acceleration of the object. Plot the above quantities from t = 0 to 10 with 100 elements. You should use subplot where, from top to bottom the plots should be in the order: position, velocity, acceleration. All plots should be labeled appropriately". I assumed that the acceleration was meant to be -9.8 for gravity.
My function is as follows:
function [X,V,A] = kin(t)
X = .5*(-9.8)*t.^2
V = (-9.8)*t
A = -9.8
end
In my main script:
T = linspace(0, 10, 100);
[XX, VV, AA] = kin(T);
figure(3)
subplot(3,1,1)
plot(T, XX)
title('Position')
xlabel('t')
ylabel('m')
subplot(3,1,2)
plot(T, VV)
title('Velocity')
xlabel('t')
ylabel('m/s')
subplot(3,1,3)
plot(T, AA)
title('Acceleration')
xlabel('t')
ylabel('m/s^2')
Everything seemed to be correct save for the fact that my acceleration graph did not display the constant -9.8 m/s^2. I am not sure if this is because there is no independent variable ("t") in the acceleration equation or what. All help is appreciated.
Accepted Answer
More Answers (0)
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!