How to use two different equations depending on the value

조회 수: 2 (최근 30일)
RetiredCheetoXI
RetiredCheetoXI 2022년 2월 3일
댓글: Davide Masiello 2022년 2월 4일
It only displays the value for t, but I would also like it to display the value for h, and it also does not use the second equation when t > 45. What am I doing wrong?
prompt = 'Time of flight? ';
t = input(prompt)
if (0 < t && t <=45)
h = 15*t.^2;
elseif (t > 45)
h = 30375 + -5.75*t.^2;
end

채택된 답변

Davide Masiello
Davide Masiello 2022년 2월 3일
In order to have the value of h displayed in the command window, remove the semicolon after the equations computing it.
prompt = 'Time of flight? ';
t = input(prompt)
if (0 < t && t <=45)
h = 15*t.^2
elseif (t > 45)
h = 30375 + -5.75*t.^2
end
I don't know about the other issue. If I input t = 46 I get h = 18208, which is the value obtained using the second equation, so it seems to work for me.
  댓글 수: 2
RetiredCheetoXI
RetiredCheetoXI 2022년 2월 4일
Thanks for your help!
Davide Masiello
Davide Masiello 2022년 2월 4일
No problem. If it helped, please accept the answer.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by