How to Input a value, get a result, input that result to calculate another value

5 Ansichten (letzte 30 Tage)
The input is time, which then calculates altitude. This value for altitude is then used to get a value for temperature. How do I make this work? The bolded text is what I just added to it and is not working, everything else works on its own.
prompt = 'Time of flight? (s) ';
t = input(prompt);
tx = t-200:t+200;
if (t <= 45)
h = @(x) 15*x.^2;
fprintf('Altitude: %dm\n\n', h(t))
v = @(x) 30*x;
fprintf('Velocity: %dm/s\n\n', v(t));
if (h <= 11000)
T = (h - 44332.30769230768) / -153.84615384615378;
elseif (h <= 25000)
T = 216.66;
elseif (h <= 47000)
T = (h + 47220) / 333.3;
elseif (h <= 53000)
T = 282.66;
elseif (h <= 79000)
T = (h - 115813) / -222.2;
elseif (h <= 90000)
T = 165.66;
elseif (h <= 110000)
T = (h - 48585) / 250;
end
figure(1)
subplot(2,1,1)
plot(t,h(t),'o',tx,h(tx)), xlabel('Time (s)'), ylabel('Altitude (m)'), title('Altitude vs Time')
subplot(2,1,2)
plot(t,v(t),'o',tx,v(tx)), xlabel('Time (s)'), ylabel('Velocity (m/s)'), title('Velocity vs Time')
elseif (t < 117.5)
h = @(x) 30375 + (1350*x) + 0.5*(-11.5)*x.^2;
fprintf('Altitude: %dm\n\n', h(t))
v = @(x) 1350 - 11.5*x;
fprintf('Velocity: %dm/s\n\n', v(t));
if (h <= 11000)
T = (h - 44332.30769230768) / -153.84615384615378;
elseif (h <= 25000)
T = 216.66;
elseif (h <= 47000)
T = (h + 47220) / 333.3;
elseif (h <= 53000)
T = 282.66;
elseif (h <= 79000)
T = (h - 115813) / -222.2;
elseif (h <= 90000)
T = 165.66;
elseif (h <= 110000)
T = (h - 48585) / 250;
end
figure(2)
subplot(2,1,1)
plot(t,h(t),'o',tx,h(tx)), xlabel('Time (s)'), ylabel('Altitude (m)'), title('Altitude vs Time')
subplot(2,1,2)
plot(t,v(t),'o',tx,v(tx)), xlabel('Time (s)'), ylabel('Velocity (m/s)'), title('Velocity vs Time')
end
  3 Kommentare
RetiredCheetoXI
RetiredCheetoXI am 7 Feb. 2022
Input a time (t), whcih is then used in a calculation to get altitude (h). This calculated altitude (h) is then used in another calculation to get temperature (T)
Voss
Voss am 8 Feb. 2022
Replace h with h(t) everywhere in the bolded text.
h is a function. To get h at some given time t, you have to evaluate the function at that time t. That's what h(t) does.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Prahlad Gowtham Katte
Prahlad Gowtham Katte am 18 Feb. 2022
Hello,
As mentioned in the comments please change h to h(t) wherever you are using assignment and comparison operators.
The errors which you got are due to "h" as it is a function handle and it cannot be used in comparison and arithmetic operations but h(t) is a return value of type double which can be used.
For more information on function handles please refer to the following link
Hope it helps.

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by