I have matlab2007 and if i use multiple elseif the program don't run the second or third eleseif. This is the code... x=input('Give x a value x:') if x<-2 f=1 elseif -2<x<3 f=x+1 elseif x>=3 f=x^2 end

 채택된 답변

Star Strider
Star Strider 2015년 11월 15일
편집: Star Strider 2015년 11월 15일

0 개 추천

The condition in the first elseif statement is not stated correctly. This should work:
xc=inputdlg('Give x a value x:');
x = str2num(xc{:});
if x<-2
f=1
elseif (-2<x) && (x<3)
f=x+1
elseif x>=3
f=x^2
end

댓글 수: 3

Jan
Jan 2015년 11월 15일
@vlad vladut: Note that f is undefined for x==-2.
vlad vladut
vlad vladut 2015년 11월 15일
Ty a lot works nicely...saved me:))
Star Strider
Star Strider 2015년 11월 15일
My pleasure.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

질문:

2015년 11월 15일

댓글:

2015년 11월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by