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!