How do I graph a constant within a function?

2 次查看(过去 30 天)
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.

采纳的回答

Walter Roberson
Walter Roberson 2016-10-9
A = -9.8 * ones(size(t));
  3 个评论
Walter Roberson
Walter Roberson 2016-10-9
Yes, except for the cases where the output constant is 0, +/- inf, true, false, or nan, in which case you use zeros(size(t)), -inf(size(t)), inf(size(t)), true(size(t)), false(size(t)), nan(size(t))
Some people prefer to use something like
A = t;
A(:) = -9.8;

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by