How can I solve this problem using if-else statement?

조회 수: 22 (최근 30일)
Helia  Zarbafi
Helia Zarbafi . 2022년 3월 17일
답변: Kumar Pallav . 2022년 3월 21일
Consider the following continuous mathematical function y =f(x):
y = 1 for x < -1
y = x^2 for -1 <= x <= 2
y = 4 for x > 2
Using if-else statement, write a script named findY.m that promotes the user for a value for x and returns a value for y using the function y = f(x).
  댓글 수: 2
Voss 2022년 3월 17일
You might use the input() function to prompt the user for a value of x, and you might use if-elseif-else statements, based on the value of x, to determine which one of those three equations for y you need to use.

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

답변 (1개)

Kumar Pallav
Kumar Pallav 2022년 3월 21일
You can solve the problem by using the conditional statements (if/else).Please refer this link for more on if/elseif/else staements.
To take user input, use the input command.
You can create scripts in MATLAB which are the simplest codes having no input or output arguments. Refer to this to learn more on writing scripts in MATLAB with examples.
Hope it helps! Happy learning!


Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by