Problem with execution of "nested if" commands.

조회 수: 1 (최근 30일)
meher
meher 2011년 9월 11일
I need to find values of a piece wise linear function. I used nested if condition as shown in code. But for all values of t, it is executing only first "if else" loop, and giving always x1d(t)= 0.5 andx2d(t)= 0.5 as output values. Am not able to figure out the mistake. help me.
for t = 1:1:26
if 0<t<=4
x1d(t) = 0.5*t;
x2d(t) = 0.5*t;
elseif 4<t<=7
x1d(t) = 0.5*t;
x2d(t) = 3.33;
elseif 7<t<=10
x1d(t) = 0.5*t;
x2d(t) = 10/3 - 0.5*(t-40/3);
elseif 10<t<=26
x1d(t) = 0.5*t;
x2d(t) = 0;
end
end

채택된 답변

Paulo Silva
Paulo Silva 2011년 9월 11일
This if 0<t<=4 doesn't work properly in MATLAB, but this does if 0<t && t<=4 , you have to separate the conditions because MATLAB doesn't evaluate two conditions at the same time unless you use & or | operators.
  댓글 수: 1
meher
meher 2011년 9월 11일
Yes it worked :-) thanku very much...

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

추가 답변 (1개)

Derek O'Connor
Derek O'Connor 2011년 9월 11일
Boredom Forecast: Discussion of Syntax. High: (78/100)
First, get the preferred indentation by using "smart indent" in the Matlab Editor.
for t = 1:1:26
if 0<t<=4
x1d(t) = 0.5*t;
x2d(t) = 0.5*t;
elseif 4 < t && t <= 7.
x1d(t) = 0.5*t;
x2d(t) = 3.33;
elseif 7<t<=10
x1d(t) = 0.5*t;
x2d(t) = 10/3 - 0.5*(t-40/3);
elseif 10<t<=26
x1d(t) = 0.5*t;
x2d(t) = 0;
end
end
Second, as Paulo Silva points out, compound logical expressions, such as 4 < t <= 7 are not allowed in Matlab, Fortran, Pascal, ..., etc. Instead you have to write (for the convenience of the compiler-interpreter (writer)), 4 < t && t <= 7.
Count yourself lucky. In some languages you would have to write
if 4 < t && (t < 7 || t = 7) etc.
This logical expression would take an eagle-eyed programmer anywhere from 10 mins to a day to check for errors (precedence rules for && versus ||, etc.).
This is crazy. Any compiler-interpreter that rejects 4 < t <= 7 is doing so for purely lexical-syntactical convenience. This is not a code generation problem. Most 1st or 2nd-year computer science students could write a (character-by-character) program that would translate
4 < t <= 7 into 4 < t && t <= 7.
For me, the key to Matlab's success, and what attracted me, was the important syntactical innovation by Cleve Moler: a notation that made it easy to translate statements in Linear Algebra into a programming language. This is what IBM's Jim Backus tried to do with the FORTRAN (FORmula TRANslation) (56?) compiler. He succeeded, despite the sceptics, and Cleve Moler succeeded, despite the sceptics, to produce programming languages that are widely used in scientific computing.
So, in typical Irish fashion, I will answer your question with a question:
Why can't Matlab, etc., recognize the simple statement
if 4 < t <= 7
Derek O'Connor.
PS: Another question: why is it so difficult to write anything but plain text on this site?
Anybody for Stack Exchange?
PPS: Yes, yes: we know how to get rid of all the elseif's
  댓글 수: 2
Walter Roberson
Walter Roberson 2011년 9월 12일
Derek, I suggest you move this to the "What frustrates you about MATLAB thread", http://www.mathworks.com/matlabcentral/answers/1427-what-frustrates-you-about-matlab
Derek O'Connor
Derek O'Connor 2011년 9월 12일
@Walter, done. Should I remove my answer from here? I'm not sure of the etiquette on double posting.

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by