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

3 次查看(过去 30 天)
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 个评论
RetiredCheetoXI
RetiredCheetoXI 2022-2-7
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 2022-2-8
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.

请先登录,再进行评论。

回答(1 个)

Prahlad Gowtham Katte
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.

类别

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

标签

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by