Vector output from a function

4 ビュー (過去 30 日間)
Karlie Brillant
Karlie Brillant 2019 年 3 月 26 日
編集済み: Stephan 2019 年 3 月 26 日
Working on a projectile motion problem. I am trying to get x_dist and y_height vectors as outputs from my function so that I can then graph them. When I run my function it tells me that the variable x_dist does not exist even though it is in my function. Any help would be appreciated!
function [x_dist,y_height]=projectile(V_int,theta)
dt=1; %seconds
w=3; %kg
k=0.32;
g=9.81; %m/s^2
i=1;
y=0;
while y>0
a_y(i)=w*g-V_int*k;
a_x(i)=-V_int*k;
v_y(i)=(V_int*sin(theta))+(a_y(i)*dt);
v_x(i)=(V_int*cos(theta))+(a_x(i)*dt);
x_dist(i)=(V_int-v_x(i))/1;
y_height(i)=(V_int-v_y(i))/1;
V_int=v_x(i);
V_int=v_y(i);
i=i+1;
dt=dt+1;
end
pause(0.5);
comet(x_dist,y_height);
end

採用された回答

Stephan
Stephan 2019 年 3 月 26 日
編集済み: Stephan 2019 年 3 月 26 日
Hi,
you set y=0. one line later you start a while loop, that is valid as long as y>0. This can not work. For this reason xdist is not there, because the while loop stopps the same moment it starts. xdist is defined inside the while loop.
Best regards
Stephan

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2019 年 3 月 26 日
編集済み: Walter Roberson 2019 年 3 月 26 日
You initialize y = 0. You have while y>0 . But 0>0 is false, so the body of your while loop is never done, and nothing was ever assigned to x_dist or y_height.
Note: you also do not assign to y in the loop, so if the loop did ever get started, then it would not stop.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by