Calculation does not work, and it says "Index exceeds the number of array elements"

1 次查看(过去 30 天)
I am trying to calculate temperature, with an imput of "h". And based on the input it would use a certain value for a. It is not working properly and is saying "Index exceeds the number of array elements." I am also trying to make this I can call in another code, and I am not sure if this is the correct way to go about doing that
function [h, a] = Temperature(T)
prompt = 'Altitude (m) ';
h = input(prompt);
if (h <= 11000)
a = -6.5e-3;
elseif (h <= 25000)
a = 1;
elseif (h <= 47000)
a = 3e-3;
elseif (h <= 53000)
a = 1;
elseif (h <= 79000)
a = -4.5e-3;
elseif (h <= 90000)
a = 1;
elseif (h <= 110000)
a = 4e-3;
end
T = 273.15 + a(h)

回答(1 个)

Cris LaPierre
Cris LaPierre 2022-2-6
The error is caused by this code at the bottom of your function: a(h)
Your variable a only contains a single number, so there is no reason to index it using h. Try just adding a.
T = 273.15 + a

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by