필터 지우기
필터 지우기

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일
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개)

카테고리

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