Run Fminsearch in a loop

3 次查看(过去 30 天)
Joe
Joe 2023-2-22
编辑: Joe 2023-2-23
Hello again,
I have a question regarding fminsearch and my code.
The idea is to run fminsearch in a loop. Therefore I wrote a costfunction with 3 optimization variables (A, B, C).
Inside the costfunction there is a variable 'U' that should vary from 30 to 50. Actually it is constant at 30 but it should vary for a range of values.
After all I want to save the results in a matfile.
Until now I started fminsearch from the console with fminsearch(@costfunction,[10;5;7] with the guess parameters for example 10,5,7
It would be great if someone can help me finding a solution for this!
Thanks in advance!
Here is the code :
function [cost_value] = costfunction(optimize)
U = 30; % should run from from 30 to 50
A = optimize(1)
B = optimize(2);
C = optimize(3);
H = [0;5;0];
.
.
.
Vect1 = [0; C; 0];
Vect2 = [0; 0; B; B; 0; A];
.
.
cost_value=norm(out.simout)
end
save results.mat U, A, B, C

采纳的回答

Steven Lord
Steven Lord 2023-2-22
Parameterize your costfunction. The "Minimize with Extra Parameters" example on the documentation page for the fminsearch function does this using an anonymous function and you can use a similar approach in your code. Instead of passing @costfunction into fminsearch pass @(v) costfunction(v, U) as the first input and modify costfunction so it accepts two inputs, optimize and U.
  2 个评论
Joe
Joe 2023-2-23
编辑:Joe 2023-2-23
I have an other question, can you please specifiy what "v" is in your description?
"Instead of passing @costfunction into fminsearch pass @(v) costfunction(v, U) as the first input "

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by