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)')
채택된 답변
추가 답변 (2개)
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
Kelvin Kong
2017년 9월 24일
Kelvin Kong
2017년 9월 24일
Image Analyst
2017년 9월 24일
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
Kelvin Kong
2017년 9월 25일
카테고리
도움말 센터 및 File Exchange에서 Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!