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 个评论

Keep in mind this is an embedded matlab function within Simulink. Different rules apply, hence all is not fine.
So I don't know what is going on. I made attempts into differents computers with different matlabs. When I do the same you did, it gives me this message "Variable 'Dy' is undefined on some execution paths."
It can't happen because it is independent of the inputs of the function.
See my answer above. 'am' is dependent on inputs phi_ca and phi_an. 'lambda_m' is dependent on 'am', and 'Dy' is depenent on 'lambda_m'. Therefore it is undefined on some execution paths, and this is no allowed for embedded matlab.
You must give this variable a default value, or add 'else' conditions to your 'if-else if' statements.
I cannot change if statement. What I can do is set an initial value to the variables 'phi_ca' and 'phi_an. But I don't know if it is correct the way I did above, just setting it within the function. Because at every iteration of simulink it must update this values. And doing this within the function I presuppose that 'phi_ca' and 'phi_an' will always stay with the initial values.
No, you can't set your inputs to have default values, only the internal parameters (such as Dy) and outputs (such as lambda_m).
As stated, please initialise all your outputs and parameters to default values.
Ok. Now it is working man. Thanks.
No problem. Mark answer as accepted.

请先登录,再进行评论。

更多回答(3 个)

C.J. Harris
C.J. Harris 2011-12-20

1 个投票

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 个评论

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.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Performance and Memory 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by