How to correct ''Function definitions are not permitted in this context.''

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)

 Accepted Answer

You can copy the complete code to a function:
[EDITED] Parameters added to multiobj():
function [x,f,exitflag,output] = yourFcn
% clear all % Omit this waste of time
clc
%%given
D=0.1;
... % Left out due to clarity
nvars=3;
fcn = @(x) multiobj(x, ns, D, epsilon, row, CP, Fj);
[x, f, exitflag, output] = gamultiobj(fcn,nvars,A,b,[],[],lb,ub)
end
function f = multiobj(x, ns, D, epsilon, row, CP, Fj)
f(1) = (pi/4) * ns * x(1)* D^(2) * epsilon;
f(2) = (2*Fj) / (row * Cp * D * x(1) * epsilon);
end
In modern Matlab versions functions can be defined in scripts also, but not in the middle of it. But using functions is much better, because it keeps the workspace clear and you can omit the darn clear all.

7 Comments

Thank you for your response. I tried what you told me, but I have a new error which is "undefined function or variable 'ns'. (the second function)
All of those variables in multiobj, like ns, D, epsilon, row, etc. will have to be defined either in the function, or passed in via the input parameter list.
See [EDITED]: Parameters are now provided.
Or use a nested function, if your Matlab version allows this:
function [x,f,exitflag,output] = yourFcn
% clear all % Omit this waste of time
clc
%%given
D=0.1;
... % Left out due to clarity
nvars=3;
[x, f, exitflag, output] = gamultiobj(@multiobj,nvars,A,b,[],[],lb,ub)
function f = multiobj(x, ns, D, epsilon, row, CP, Fj)
f(1) = (pi/4) * ns * x(1)* D^(2) * epsilon;
f(2) = (2*Fj) / (row * Cp * D * x(1) * epsilon);
end
end
Thanks Mr.Jan for your help. Now, I am having a list of errors which is below:
Error using multiobj/multiobj (line 57)
Not enough input arguments.
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in gamultiobjMakeState (line 25)
Score = FitnessFcn(state.Population(1,:));
Error in gamultiobjsolve (line 11)
state = gamultiobjMakeState(GenomeLength,FitnessFcn,output.problemtype,options);
Error in gamultiobj (line 238)
[x,fval,exitFlag,output,population,scores] = gamultiobjsolve(FitnessFcn,nvars, ...
Error in multiobj (line 54)
[x,f,exitflag,output]=gamultiobj(@multiobj,nvars,A,b,[],[],lb,ub)
Caused by:
Failure in initial user-supplied fitness function evaluation. GAMULTIOBJ cannot continue.
Kindly find a solution for this.
Thanks in advance.
Thanks Mr.Jan and Mr.Walter for your help. It worked.

Sign in to comment.

More Answers (2)

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 Comments

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.
Thank you for your response. I have Matlab R2012a
In R2012a you need to store the function in a separate file.
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.

Sign in to comment.

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

Categories

Find more on MATLAB in Help Center and File Exchange

Asked:

on 18 Dec 2017

Edited:

on 20 Dec 2017

Community Treasure Hunt

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

Start Hunting!