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

127 ビュー (過去 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.
Nilendra Singh Pawar
Nilendra Singh Pawar 2024 年 9 月 23 日
Thaks Steven. That is helpful.

サインインしてコメントする。

その他の回答 (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.
Nilendra Singh Pawar
Nilendra Singh Pawar 2024 年 9 月 23 日
Stephen, Thanks for confirming this.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGet 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