if statement

조회 수: 7 (최근 30일)
Danilo NASCIMENTO
Danilo NASCIMENTO 2011년 12월 20일
편집: Jan 2017년 11월 24일
hey guys I'm trying to do an if statement inside an embedded matlab function in simulink, but it is not working, I don't know why.
------------------------------------------------
This is the matlab function
function [Wv_membr,lambda_m] = fcn(Ist,T_st,phi_ca,phi_an)
Mv=18.02E-3;
n=381;
Afc=280;
F=96485;
tm=0.01275;
%phi_an=0.5;
%phi_ca=0.80;
am=(phi_ca+phi_an)/2;
if (am>0) && (am<=1)
lambda_m=0.043+17.81*am-39.85*am^2+36*am^3;
elseif (am>1) && (am<3)
lambda_m=14+1.4*(am-1);
end
i=Ist/Afc;
if (lambda_m<2)
Dy=1E-6;
elseif (lambda_m>=2) && (lambda_m<=3)
Dy=1E-6*(1+2*(lambda_m-2));
elseif (lambda_m>3) && (lambda_m<4.5)
Dy=1E-6*(3-1.67*(lambda_m-3));
elseif (lambda_m>=4.5)
Dy=1.25E-6;
end
if (phi_an>0) && (phi_an<=1)
lambda_an=0.043+17.81*phi_an-39.85*phi_an^2+36*phi_an^3;
elseif (phi_an>1) && (phi_an<3)
lambda_an=14+1.4*(phi_an-1);
end
if (phi_ca>0) && (phi_ca<=1)
lambda_ca=0.043+17.81*phi_ca-39.85*phi_ca^2+36*phi_ca^3;
elseif (phi_ca>1) && (phi_ca<3)
lambda_ca=14+1.4*(phi_ca-1);
end
pm_dry=0.002;
Mm_dry=1.1;
cv_an=pm_dry/Mm_dry * lambda_an;
cv_ca=pm_dry/Mm_dry * lambda_ca;
Dw=Dy*exp(2416*(1/303-1/T_st));
nd=0.0029*lambda_m^2+0.05*lambda_m-3.4E-19;
Nv_membr=nd*i/F * Dw*(cv_ca-cv_an)/tm;
Wv_membr=Nv_membr*Mv*Afc*n;
end
---------------------------------------------------
when I run it, it says that lamda_m is not a defined variable, but this is impossible. It is very weird to me. I wonder for some help.
  댓글 수: 1
bym
bym 2011년 12월 20일
please format your code to make it readable

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

채택된 답변

Jan
Jan 2011년 12월 20일
When I uncomment the lines as you mentioned, I still do not get an error:
phi_an=0.5;
phi_ca=0.80;
am=(phi_ca+phi_an)/2;
if (am>0) && (am<=1)
lambda_m=0.043+17.81*am-39.85*am^2+36*am^3;
elseif (am>1) && (am<3)
lambda_m=14+1.4*(am-1);
end
if (lambda_m<2)
Dy=1E-6;
elseif (lambda_m>=2) && (lambda_m<=3)
Dy=1E-6*(1+2*(lambda_m-2));
elseif (lambda_m>3) && (lambda_m<4.5)
Dy=1E-6*(3-1.67*(lambda_m-3));
elseif (lambda_m>=4.5)
Dy=1.25E-6;
end
All is fine.
  댓글 수: 7
Danilo NASCIMENTO
Danilo NASCIMENTO 2011년 12월 20일
Ok. Now it is working man. Thanks.
C.J. Harris
C.J. Harris 2011년 12월 20일
No problem. Mark answer as accepted.

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

추가 답변 (3개)

C.J. Harris
C.J. Harris 2011년 12월 20일
Within an embedded matlab function you have to ensure all variables are set independent of execution path through the code.
While you might be able to assure us the values of 'lambda_m' and 'Dy' are always within the specified range, Matlab cannot assure this, as the values of 'lambda_m' and 'Dy' are dependent on the function inputs.
In order to fix this error just assign 'lambda_m' and 'Dy' a value at the top of your code, and if they are within the specified ranges defined by your 'if-else if' statements these 'initial' values will just be overwritten.
  댓글 수: 1
Jan
Jan 2011년 12월 20일
As far as I understand, this answer solves the problem. And in addition initializing all used variables is a good programming habit.

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


Walter Roberson
Walter Roberson 2011년 12월 20일
Consider your code
if (am>0) && (am<=1)
lambda_m=0.043+17.81*am-39.85*am^2+36*am^3;
elseif (am>1) && (am<3)
lambda_m=14+1.4*(am-1);
end
What happens in your code if am is not in the range 0 < am < 3 ? If, that is, am <= 0 or am >= 3 ?
  댓글 수: 2
Danilo NASCIMENTO
Danilo NASCIMENTO 2011년 12월 20일
No man.. I assure to you that am is in this interval. You can, for example, take off % of lines 12 and 13 and you'll see that there is another mistake. Matlab will give you that variable Dy is not defined. But this is also impossible.
Jan
Jan 2011년 12월 20일
@Danilo: If "am" is not in the expected ranges, the error message you have posted will appear. Without doubtm there can be a further problem also. But Wlater's correct statement is affect by this.

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


arief hidayat
arief hidayat 2017년 11월 24일
편집: per isakson 2017년 11월 24일
Hi anyone help me for my script, when i running script, i got error "not enough statement" script :
if ((rate ~= rate_tx || (Nbpsc ~= Nbpsc_tx) || (psdu_byte ~= psdu_byte_tx)))
Percounter = 1;
noviterbi_Y = [];
PSDU = [];
return ;
else
Percounter = 0;
  댓글 수: 1
Jan
Jan 2017년 11월 24일
편집: Jan 2017년 11월 24일
Do not hijack an existing thread by inserting a new question in the section for answers. Open a new thread instead and delete this one. Otherwise it is confusing, to which question an answer belong and you cannot accept the answer, which solves your problem. Thanks.
Include the complete error message, not just a part of it. Format your code using the "{} Code" button to make it readable.

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

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by