while loop nestled in a for loop

조회 수: 10 (최근 30일)
Emil Blomberg
Emil Blomberg 2016년 9월 15일
댓글: Stephen23 2016년 9월 16일
This is the first code I have ever written so there might be a simple solution or not even possible to make a code this way.
Im supposed to write a code that simulates the path of a thrown ball including airresistance. And calculate what angle gives the maximal length?
I started to simulate the path of a thrown ball including airresistance with a launch angle of 45degrees and succeded to find the maximum x for that angle. Then I thought I could just do a for loop outside and have the angle go from 0-90degrees instead and finding the maximum x that corresponds to each degree. And then plot the maximum x vs. degree graph.
But something is wrong and I cant se what.
I am very thankful for help!
clear; close all;
%Constants for Tennisball
m=0.057; %mass in kg
r=0.033; %radius in m
C=0.47; %Drag Coefficient of a sphere
rho= 1.2; %density of air kg/m^3
g=9.81; %m/s^2 (acceleration due to gravity)
A=pi*r^2; %m^2
D=rho*C*A/2; %
dn=0.1;
NT=900*dn;
for theta =1:NT;
%Initial Conditions
dt=0.001; %s
x(1)=0;
y(1)=0;
v=10; %m/s
vx=v*cosd(theta);
vy=v*sind(theta);
t(1)=0;
%Start Loop
i=1;
while y >= 0;
ax=-(D/m)*v*vx;
ay=-g-(D/m)*v*vy;
vx=vx+ax*dt;
vy=vy+ay*dt;
x(i+1)=x(i)+vx*dt+0.5*ax*dt^2;
y(i+1)=y(i)+vy*dt+0.5*ay*dt^2;
t(i+1)=t(i)+dt;
v=sqrt(vx^2 + vy^2);
i=i+1;
end
c = max(x);
clist(theta)=c;
end
tlist = (1:NT)*dn;
plot(tlist,clist)
  댓글 수: 2
Stephen23
Stephen23 2016년 9월 16일
Read the while documentation and it clearly explains its behavior with non-scalar inputs: "An expression is true when its result is nonempty and contains only nonzero elements".

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

답변 (1개)

Dalibor Knis
Dalibor Knis 2016년 9월 16일
편집: Dalibor Knis 2016년 9월 16일
The issue is that you use y as an array. I don't think the while statement is defined over array. The while expects single boolean value. I do not know how exactly guys in Matlab implemented 'while' statement but my experience is that while over array evaluates as false. That's why the second iteration of theta never jumps in the while loop.
You could easily identify the problem if you placed [theta, i] printout in the innermost loop.
So what you want is something like this:
clc; clear; close all;
%Constants for Tennisball
m=0.057; %mass in kg
r=0.033; %radius in m
C=0.47; %Drag Coefficient of a sphere
rho= 1.2; %density of air kg/m^3
g=9.81; %m/s^2 (acceleration due to gravity)
A=pi*r^2; %m^2
D=rho*C*A/2; %
dn=0.1;
NT=900*dn;
for theta =1:NT;
%Initial Conditions
dt=0.001; %s
i=1;
x(i)=0;
y(i)=0;
t(i)=0;
v=10; %m/s
vx=v*cosd(theta);
vy=v*sind(theta);
%Start Loop
while y(i) >= 0;
ax=-(D/m)*v*vx;
ay=-g-(D/m)*v*vy;
vx=vx+ax*dt;
vy=vy+ay*dt;
x(i+1)=x(i)+vx*dt+0.5*ax*dt^2;
y(i+1)=y(i)+vy*dt+0.5*ay*dt^2;
t(i+1)=t(i)+dt;
v=sqrt(vx^2 + vy^2);
i=i+1;
end
c = max(x);
clist(theta)=c;
end
tlist = (1:NT);
plot(tlist,clist)
i=i+1;
BTW: if this is indeed the first code you have ever written then is it not so bad. :-)
  댓글 수: 2
Walter Roberson
Walter Roberson 2016년 9월 16일
"if" and "while" with array or vector arguments are considered true only if all the elements in the vector are non-zero. It is well defined in MATLAB, but it is bad programming practice to use it deliberately rather than explicitly calling all() on the vector to make it clear to readers what is being tested.
Dalibor Knis
Dalibor Knis 2016년 9월 16일
Thank you. 👍

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

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by