if statement
7 次查看(过去 30 天)
显示 更早的评论
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.
采纳的回答
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 个评论
更多回答(3 个)
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
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
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 个评论
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
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
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!