How do I graph a constant within a function?

조회 수: 5 (최근 30일)
Japheth Gielen
Japheth Gielen 2016년 10월 9일
댓글: Japheth Gielen 2016년 10월 9일
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;
Japheth Gielen
Japheth Gielen 2016년 10월 9일
Oh okay I see. Thanks again.

댓글을 달려면 로그인하십시오.

추가 답변 (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