How to correct ''Function definitions are not permitted in this context.''
Afficher commentaires plus anciens
I am trying to solve an optimization problem with multi objective genetic algorithms and I am having this error even I stored the file as (multiobj.m). Thanks in advance.
clear all
clc
%%given
D=0.1;
W=10e3;
ns=40;
x(3)=0.005;
x(1)=100e-6;
x(2)=0.4;
row=860;
Cp=4.19*10^3;
%%the average Reynolds number
U=pi*D*ns;
Re=(row*x(1)*U)/x(3);
%%the correction coefficients
if (Re<510)
alpha=1;
G=1/12;
elseif (510<=Re<1125)
alpha=5.914*Re^(-0.285);
G=2.915*Re^(-0.57);
elseif (1125<=Re<13500)
alpha=0.798;
G=2.915*Re^(-0.57);
else
alpha=0.756;
G=14.45*Re^(-0.75);
end
%%modified Sommerfield number
S=(ns*x(3)*(D^3)*x(2))/(48*G*(x(1)^2)*W);
%%the eccentricity ratio
epsilon=exp(-2.236*alpha*x(2)*sqrt(S));
%%the maximum film pressure
theta=1/(cos((1-sqrt(1+24*epsilon^(2)))/(4*epsilon)));
Pmax=((pi*ns*x(3)*D^(2)*alpha^(2)*x(2)^(2))/(8*G*x(1)^(2)))*((epsilon*sin(theta))/((1+epsilon*cos(theta))^(3)));
%%the friction force on the journal surface
if (Re<1125)
Fj=((pi^(2)*x(3)*ns*D^(3)*x(2))/(48*G*x(1)))*((1/sqrt(1-epsilon))+((1-epsilon)/(1-epsilon^(2))^(3/2)));
elseif (1125<=Re<13500)
Fj=((pi^(2)*x(3)*ns*D^(3)*x(2))/(48*G*x(1)))*((1.109*epsilon^(2))-(1.49*epsilon)+2.748);
else Fj=((pi^(2)*x(3)*ns*D^(3)*x(2))/(48*G*x(1)))*((1.792*epsilon^(3))-(1.523*epsilon^(2))-(3.697*epsilon)+8.734);
end
lb=[40e-6;0.2;0.0001];
ub=[300e-6;0.6;0.001];
A=[-1 0 0;1 0 0;0 -1 0;0 1 0;0 0 -1;0 0 1];
b=[-40e-6;300e-6;-0.2;0.6;-0.0001;0.001];
nvars=3;
function f=multiobj(x)
f(1)=(pi/4)*ns*x(1)*D^(2)*epsilon;
f(2)=(2*Fj)/(row*Cp*D*x(1)*epsilon);
end
[x,f,exitflag,output]=gamultiobj(@multiobj,nvars,A,b,[],[],lb,ub)
Réponse acceptée
Plus de réponses (2)
Image Analyst
le 18 Déc 2017
1 vote
The last line, about gamultiobj(), is not in any function. So you have a function right in the middle of a script, which is not allowed. Also, what release do you have, since it was about R2016b or so where they allowed functions to follow scripts in a file?
4 commentaires
Walter Roberson
le 18 Déc 2017
Also note that when functions are stored in script files, the name of the function cannot be the same as the name of the script file.
Khaled Gamal
le 18 Déc 2017
Walter Roberson
le 19 Déc 2017
In R2012a you need to store the function in a separate file.
Image Analyst
le 19 Déc 2017
Modifié(e) : Image Analyst
le 19 Déc 2017
Or call your m-file something like testga.m, and then put this line as the first line in your file
function testga()
Then follow with the rest of your code and other function. The line
[x,f,exitflag,output]=gamultiobj(@multiobj,nvars,A,b,[],[],lb,ub)
will have to go inside one of the functions. Since you ended multiobj() with "end", you'll also have to end testga() with an "end".
Actually, all of this is done in Jan's answer, so just follow that.
Alan Weiss
le 18 Déc 2017
Modifié(e) : Alan Weiss
le 18 Déc 2017
If I understand you correctly, you stored that whole file as multiobj.m. Instead, just store this as multiobj.m:
function f=multiobj(x)
f(1)=(pi/4)*ns*x(1)*D^(2)*epsilon;
f(2)=(2*Fj)/(row*Cp*D*x(1)*epsilon);
end
Take those lines out of your code. Store your code (without those lines) as a separate file, maybe runmultiobj.m.
Calling runmultiobj won't run because you are passing extra parameters. Rework things to make this a nested function, or add more arguments to multiobj and call it as @(x)multiobj(x,ns,D,row,epsilon) or whatever you need to pass.
Alan Weiss
MATLAB mathematical toolbox documentation
1 commentaire
Khaled Gamal
le 20 Déc 2017
Catégories
En savoir plus sur MATLAB dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!