フィルターのクリア

Run Fminsearch in a loop

4 ビュー (過去 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 日
Thanks a lot!
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 件)

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by