MATLAB Error - Local function name must be different from the script name.

조회 수: 134 (최근 30일)
Hi, I am using the following code (saved as c_objective.m) for defining the objective function used with gamultiobj. I am getting an error:
"Error: File: c_objective.m Line: 3 Column: 14
Local function name must be different from the script name."
Please help me resolve this. Thank you.
% Define the multi-objective function for Pareto search
global intcon f Q nmsi M S I
function F = c_objective(x)
F(1) = -(f'*x+x'*Q*x);
tempx=[nmsi x];
for m=1:M
for s=1:S
for i=1:I
index=tempx(find(tempx(:,2)==m & tempx(:,3)==s & tempx(:,4)==i));
dvstorage(m,s,i)=tempx(index,5);
end
end
end
entropy_simple=0;
for i = 1 : I % for each SKU
K1=0;
for m= 1: M % For each Aisle
nim=sum(dvstorage(m,:,i)); % sum for every I, for every m SKU stock(all ones)
K1=K1+nim*log(nim+0.00001);
end
K1=-K1;
Ni=sum(sum(dvstorage(:,:,i)));
K2=Ni*log(Ni+0.0000001);
entropy_simple=entropy_simple+K1+K2;
end
F(2) = -entropy_simple;
end

채택된 답변

Bruno Luong
Bruno Luong 2024년 9월 22일
편집: Bruno Luong 2024년 9월 22일
Perhaps swap those two fitst lines in your code file
function F = c_objective(x)
global intcon f Q nmsi M S I
...
Warning: using global variables is NOT recommended practice
  댓글 수: 4
Steven Lord
Steven Lord 2024년 9월 22일
I was not sure whether I can pass more than just the decision variable vector (x) to the objective function.
Yes, you can. This documentation page describes two approaches for doing so that don't involve global variables.

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

추가 답변 (1개)

Venkat Siddarth Reddy
Venkat Siddarth Reddy 2024년 9월 22일
편집: Venkat Siddarth Reddy 2024년 9월 22일
Hi Nilendra,
The error message indicates that the local function has same function name as the filename of a MATLAB script in which it is defined. This is not allowed in MATLAB, to fix the issue please rename either the function or the MATLAB script.
In this case please change the name from "c_objective" either for the local function or the MATLAB script.
I hope it helps!
  댓글 수: 3
Stephen23
Stephen23 2024년 9월 22일
_"The script is a standalone function file with the same name c_objective.m. It is not defined in any other MATLAB file."_
No, you do not have a function file. By adding code outside of the function definition you created a script (and a local function):
"There is one possibility. My declaring the variables outside the function code, was making this function a local function, embedded in another script. Perhaps that's why declaring the global variables after the function name"
Yes. Any code outside of a function definition turns the file into a script.

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

카테고리

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

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by