if statement

7 visualizzazioni (ultimi 30 giorni)
Danilo NASCIMENTO
Danilo NASCIMENTO il 20 Dic 2011
Modificato: Jan il 24 Nov 2017
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 Commento
bym
bym il 20 Dic 2011
please format your code to make it readable

Accedi per commentare.

Risposta accettata

Jan
Jan il 20 Dic 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 Commenti
Danilo NASCIMENTO
Danilo NASCIMENTO il 20 Dic 2011
Ok. Now it is working man. Thanks.
C.J. Harris
C.J. Harris il 20 Dic 2011
No problem. Mark answer as accepted.

Accedi per commentare.

Più risposte (3)

C.J. Harris
C.J. Harris il 20 Dic 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 Commento
Jan
Jan il 20 Dic 2011
As far as I understand, this answer solves the problem. And in addition initializing all used variables is a good programming habit.

Accedi per commentare.


Walter Roberson
Walter Roberson il 20 Dic 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 Commenti
Danilo NASCIMENTO
Danilo NASCIMENTO il 20 Dic 2011
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 il 20 Dic 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.

Accedi per commentare.


arief hidayat
arief hidayat il 24 Nov 2017
Modificato: per isakson il 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 Commento
Jan
Jan il 24 Nov 2017
Modificato: Jan il 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.

Accedi per commentare.

Categorie

Scopri di più su Performance and Memory in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by