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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by