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)')

 Respuesta aceptada

Image Analyst
Image Analyst el 24 de Sept. de 2017

0 votos

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.

1 comentario

Kelvin Kong
Kelvin Kong el 24 de Sept. de 2017
Sorry, forgot to update x. x is meant to be h. Edited*.

Iniciar sesión para comentar.

Más respuestas (2)

John D'Errico
John D'Errico el 24 de Sept. de 2017
Editada: John D'Errico el 24 de Sept. de 2017

0 votos

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 comentarios

Kelvin Kong
Kelvin Kong el 24 de Sept. de 2017
Gotcha!
I'll be back! In Arnold's voice hehe..
Kelvin Kong
Kelvin Kong el 24 de Sept. de 2017
What about this for loop? for (h > 0) && (h = 0)
Error with '>'.
Image Analyst
Image Analyst el 24 de Sept. de 2017
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

Iniciar sesión para comentar.

Kelvin Kong
Kelvin Kong el 25 de Sept. de 2017

0 votos

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

Categorías

Más información sobre Programming en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 24 de Sept. de 2017

Respondida:

el 25 de Sept. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by