if statement
6 views (last 30 days)
Show older comments
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.
Accepted Answer
Jan
on 20 Dec 2011
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 Comments
More Answers (3)
C.J. Harris
on 20 Dec 2011
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 Comment
Jan
on 20 Dec 2011
As far as I understand, this answer solves the problem. And in addition initializing all used variables is a good programming habit.
Walter Roberson
on 20 Dec 2011
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 Comments
Jan
on 20 Dec 2011
@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
on 24 Nov 2017
Edited: per isakson
on 24 Nov 2017
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 Comment
Jan
on 24 Nov 2017
Edited: Jan
on 24 Nov 2017
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.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!