Script for solving Newton’s laws of motion & plotting an object's trajectory

9 ビュー (過去 30 日間)
Kyle
Kyle 2014 年 11 月 15 日
回答済み: Zoltán Csáti 2014 年 11 月 15 日
Hello,
I am very much new to Matlab and coming from C++. I need help in how to the script would look for the following equation and then plotting the results:
Xt = cos(A*pi/180) * V0 * T
Yt = sin(A*pi/180) * V0 * T - 0.5*g*T^2
I'm trying to write a MATLAB program consisting of the function
function PlotTrajectory(V0, A, TimeStop, StepValue)
Do I initialize the values of A(angle in degrees), V0(initial velocity), g, T (time at position of the object)?

採用された回答

Zoltán Csáti
Zoltán Csáti 2014 年 11 月 15 日
Write it into the Editor:
A = 10;
v0 = 2;
T = 5;
t = 0:0.01:T;
g = 9.81;
x = cos(A*pi/180)*v0*t;
y = sin(A*pi/180)*v0*t - 0.5*g*t.^2;
plot(t,x,t,y);
  1 件のコメント
Kyle
Kyle 2014 年 11 月 15 日
Thank you!
One more question, what if I wanted to plot various times, angles & initial velocities rather than have A always = 10, V0 = 2 & T = 5?
For example, PlotTrajectory(88.0, 42.0, 5.0, 0.1);

サインインしてコメントする。

その他の回答 (2 件)

Star Strider
Star Strider 2014 年 11 月 15 日
It’s not necesary to initialise them in your function, since all variables are local to the function. It is always a good idea to preallocate vectors and matrices, especially if they are supposed to have specific sizes (for instance a row or column vector) as output, and for speed.
It is always advisable to vectorise your equations to allow for element-by-element operations, since the default behaviour in MATLAB is to use array operations:
Xt = cos(A*pi/180) * V0 * T;
Yt = sin(A*pi/180) * V0 * T - 0.5*g*T.^2;
I vectorised the ‘T.^2’ operation here (with (.^) replacing (^)), anticipating that ‘T’ will be a vector.
The semicolons at the end of the statements suppress the default behaviour of printing the value of that variable to the Command Window.

Zoltán Csáti
Zoltán Csáti 2014 年 11 月 15 日
As Star Strider said, create a function to pass those arguments.

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by