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

조회 수: 3 (최근 30일)
Khaled Gamal
Khaled Gamal 2017년 12월 18일
편집: Khaled Gamal 2017년 12월 20일
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)

채택된 답변

Jan
Jan 2017년 12월 18일
편집: Jan 2017년 12월 19일
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
Khaled Gamal
Khaled Gamal 2017년 12월 20일
편집: Khaled Gamal 2017년 12월 20일
Thanks Mr.Jan and Mr.Walter for your help. It worked.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Image Analyst
Image Analyst 2017년 12월 18일
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
Walter Roberson
Walter Roberson 2017년 12월 19일
In R2012a you need to store the function in a separate file.
Image Analyst
Image Analyst 2017년 12월 19일
편집: Image Analyst 2017년 12월 19일
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
Alan Weiss 2017년 12월 18일
편집: Alan Weiss 2017년 12월 18일
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

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by