For loop if else if statement not working

1 回表示 (過去 30 日間)
Austen Thomas
Austen Thomas 2018 年 2 月 16 日
回答済み: Use 2018 年 8 月 7 日
I am trying to make a graph where two different sets of equations need to be used at different h values but i keep getting the error code "Undefined function 'h' for input arguments of type 'double'"
can anyone correct my code and explain to me why this is wrong? Thanks
g=32.2;
R=1716;
a=-0.003563;
x = g./(a.*R);
y = g/R;
T_SSL=518.69;
rho_SSL=0.002377;
hv = 1:50:45000;
for i = 1:numel(hv)
if (h(i) <= 36152)
% lower then 36152
h(i) = hv(i);
T(i) = T_SSL+a*h(i);
rho(i) = rho_SSL*((T(i)/T_SSL)^-(x+1));
TH(i) = 6647034.077*rho(i);
elseif(h(i) > 36152)
% for over 36152 ft
h(i) = hv(i);
T(i) = 389.7;
rho(i) = rho_SSL*exp(-(y/T(i))*h(i));
TH(i) = 6647034.077*rho(i);
end
end
plot (h,TH);
xlabel('Altitude (ft)');
ylabel('Power Avaiable (hp)');
title('Power Avaiable vs Altitude');

回答 (2 件)

KSSV
KSSV 2018 年 2 月 16 日
g=32.2;
R=1716;
a=-0.003563;
x = g./(a.*R);
y = g/R;
T_SSL=518.69;
rho_SSL=0.002377;
hv = 1:50:45000;
for i = 1:numel(hv)
if (hv(i) <= 36152)
% lower then 36152
h(i) = hv(i);
T(i) = T_SSL+a*h(i);
rho(i) = rho_SSL*((T(i)/T_SSL)^-(x+1));
TH(i) = 6647034.077*rho(i);
elseif(hv(i) > 36152)
% for over 36152 ft
h(i) = hv(i);
T(i) = 389.7;
rho(i) = rho_SSL*exp(-(y/T(i))*h(i));
TH(i) = 6647034.077*rho(i);
end
end
plot (h,TH);
xlabel('Altitude (ft)');
ylabel('Power Avaiable (hp)');
title('Power Avaiable vs Altitude');
Variable should be hv not h. Code need to be tuned a lot.
  1 件のコメント
Austen Thomas
Austen Thomas 2018 年 2 月 16 日
Thanks! I am relatively new to matlab if you could give me any pointers it would be appreciated

サインインしてコメントする。


Use
Use 2018 年 8 月 7 日
Austen Thomas, have you solved this problem? I also got same problem.

カテゴリ

Help Center および File ExchangeMATLAB Compiler についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by