Dear community,
I have a question concerning the optimization toolbox.
I have used the optimization toolbox to solve a problem. And that is working fine so far.
Now, I would like to see the solutions for different input variables (let's say for values from 40 to 800) using a loop and store the solutions in a matrix.
The problem is, if I just wrap the editor in a loop (as I would do with usual code), I get an error.
I think MATLAB does not recognize that the "for N = 40:800" at the beginning of the editor and the "end" at the editor's end belong together.
Does somebody know a solution to this problem?
I really appreciate any help you can provide.

3 件のコメント

Cris LaPierre
Cris LaPierre 2023 年 11 月 30 日
It would be helpful if you could share your code, as well as the full error message (all the red text).
Andrew
Andrew 2023 年 11 月 30 日
Hi, in the code below I have converted the task to editable code.
The error I get is: "Function definition are not supported in this context. Functions can only be created as local or nested functions in code files."
for N = 40:80
v = [1];
m = zeros(1,N);
for n = 1:N
m(n)=n;
end
nvar = 5;
LB = [5 0 -5 -10 -20];
UB = [40 3 5 20 20];
q = zeros(1,N);
% Pass fixed parameters to objfun
objfun3 = @(optimInput)objectiveFcn(optimInput,Daten,m,N,v);
% Set nondefault solver options
options3 = optimoptions("ga","PopulationSize",300,"MaxGenerations",400);
% Solve
[solution,objectiveValue] = ga(objfun3,nvar,[],[],[],[],LB,UB,[],[],options3);
% Clear variables
clearvars objfun3 options3
function f = objectiveFcn(optimInput, Daten, m, N, v)
Function definition are not supported in this context. Functions can only be created as local or nested functions in code files.
a = optimInput(1);
b = optimInput(2);
c = optimInput(3);
d = optimInput(4);
e = optimInput(5);
for n = 1:N
q(n) = (Daten(v,n)-(a*sin(b*m(n)+c) + (d+e*m(n))))^2;
end
f = sum(q);
end
end
Walter Roberson
Walter Roberson 2023 年 11 月 30 日
Your function definition is inside your for N loop.

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

 採用された回答

Matt J
Matt J 2023 年 11 月 30 日

0 投票

This runs:
for N = 40:42 %<---shortened
Daten=rand(N);%<-----added
v = [1];
m = zeros(1,N);
for n = 1:N
m(n)=n;
end
nvar = 5;
LB = [5 0 -5 -10 -20];
UB = [40 3 5 20 20];
q = zeros(1,N);
% Pass fixed parameters to objfun
objfun3 = @(optimInput)objectiveFcn(optimInput,Daten,m,N,v);
% Set nondefault solver options
options3 = optimoptions("ga","PopulationSize",300,"MaxGenerations",400);
% Solve
[solution,objectiveValue] = ga(objfun3,nvar,[],[],[],[],LB,UB,[],[],options3);
% Clear variables
clearvars objfun3 options3
end
ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because the average change in the fitness value is less than options.FunctionTolerance. ga stopped because it exceeded options.MaxGenerations.
function f = objectiveFcn(optimInput, Daten, m, N, v)
a = optimInput(1);
b = optimInput(2);
c = optimInput(3);
d = optimInput(4);
e = optimInput(5);
for n = 1:N
q(n) = (Daten(v,n)-(a*sin(b*m(n)+c) + (d+e*m(n))))^2;
end
f = sum(q);
end

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeFluid Dynamics についてさらに検索

質問済み:

2023 年 11 月 30 日

コメント済み:

2023 年 11 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by