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!