Run Fminsearch in a loop
3 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!