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

1 view (last 30 days)
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)

Answers (1)

Cris LaPierre
Cris LaPierre on 6 Feb 2022
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

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!