Matlab for loop error with '>='

Dear Matlab gurus,
Having trouble plotting several graphs using a for loop. Error comes upto '>='. Tried with '>' only, and it too won't work.
"Error: File: ******.m Line: 14 Column: 7 Unexpected MATLAB operator."
I'm not too good at computing. Help would be much appreciated.
Thanks,
Kelvin
% Drag force coefficient
c = 1.3
% Surface area of skydiver - m^2
A = 1.2
% Altitude - m
h = 40000
% Atmospheric pressure - Pa
p = 101325
% Specific gas constant for dry air - J/kg K
R = 287.058
% Density - mol
D = P/R.*T
for h >= 0
% Temperature - K
T = -1.033.*10.^-16.*h.^4 + 3.344.*10.^-12.*h.^3 + 2.521.*10.^7.*h.^2 - 0.009527.*h + 293;
% Pressure
P = p.*(1 - 2.256.*10.^-5.*h).^5.256;
% Drag Force
F = 1/2.*p.*v.^2.*c.*A;
h = h - 1;
end
% Q1
figure(1);
plot(T, h)
title('Temperature as a fn of Altitude')
xlabel('Temperature (K)')
ylabel('Height (m)')
% Q2
figure(2);
plot(P, h)
title('Pressure as a fn of Altitude')
xlabel('Pressure (Pa)')
ylabel('Height (m)')
% Q3
figure(3);
plot (D, h)
title('Density as a fn of Altitude')
xlabel('Density (mol.)')
ylabel('Height (m)')

 采纳的回答

Image Analyst
Image Analyst 2017-9-24

0 个投票

It doesn't make sense. What is x??? You didn't define it. Even if you did, you would use "if" or "while" instead of "for". And if you used a while, x would have to change within the loop or else the loop would never end.

更多回答(2 个)

John D'Errico
John D'Errico 2017-9-24
编辑:John D'Errico 2017-9-24
for x >= 0
is not valid MATLAB syntax. No matter what you think it means, you cannot make up new syntax and expect it to work. Computer languages don't work that way.
Perhaps you wanted a while loop. But x is undefined at that point.
I'd start by reading the help docs on for and while. Reading the manual is always a good idea.
doc for
doc while

3 个评论

Gotcha!
I'll be back! In Arnold's voice hehe..
What about this for loop? for (h > 0) && (h = 0)
Error with '>'.
Uh, no. What value can be both equal to zero and greater than zero at the same time? Anyway, (h=0) is an assignment, and that will probably return "true" since the assignment can be made successfully. But a for loop is
for k = startValue : stepValue : endingValue
or
for k = setOfKValues
where setOfKValues is a set of values that k will take on in turn as the loop iterates.
I'm thinking you probably want
while h > 0

请先登录,再进行评论。

So' I changed the 'for' into 'if', and the error went away. Updated my code, got the graphs windows to open up but no plots. What did I do wrong?
% Drag force coefficient
c = 1.3
% Surface area of skydiver - m^2
A = 1.2
% Altitude - m
h = 40000
% Atmospheric pressure - Pa
p = 101325
% Specific gas constant for dry air - J/kg K
R = 287.058
% Density - mol.
D = P/R.*T
% Mass of Space Diver - kg
m = 100
% Acceleration of gravity - m/s
g = 9.8
% Initial velocity - m/s
u = 0
% Drag
DF = 0
for x = 0:40000:1
if h >= 0
% Temperature - K
T = -1.033.*10.^-16.*h.^4 + 3.344.*10.^-12.*h.^3 + 2.521.*10.^7.*h.^2 - 0.009527.*h + 293;
% Pressure
P = p.*(1 - 2.256.*10.^-5.*h).^5.256;
% Acceleration Eq.
a = (m.*g - DF)/m;
% Velocity Eq.
h0 = h
s = h - h0
v = sqrt(u.^2 + 2.*a.*s);
% Drag Force
DF = 1/2.*p.*v.^2.*c.*A;
h = h - 1;
end
end
% Q1
figure(1);
plot(T, h)
title('Temperature as a fn of Altitude')
xlabel('Temperature (K)')
ylabel('Height (m)')
grid on
% Q2
figure(2);
plot(P, h)
title('Pressure as a fn of Altitude')
xlabel('Pressure (Pa)')
ylabel('Height (m)')
grid on
% Q3
figure(3);
plot (D, h)
title('Density as a fn of Altitude')
xlabel('Density (mol.)')
ylabel('Height (m)')
grid on
% Q4
figure(4);
plot (v, h)
title(' Velocity of Space Diver as Fn of Altitude')
xlabel('Velocity (m/s)')
ylabel('Height (m)')
grid on

类别

帮助中心File Exchange 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by