Vector output from a function
3 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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
0 个评论
更多回答(1 个)
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!