필터 지우기
필터 지우기

Save results if fminsearch in a table or m.file

조회 수: 1 (최근 30일)
Joe
Joe 2023년 2월 24일
편집: Star Strider 2023년 6월 22일
Hello,
i need your help for my matlab programm.
I have a code that runs fminsearch for a range of speeds to find Values.
So far the code works well but I have a problem with saving the Values or the results of the fminsearch.
I want that every time a Value was found for each speed, the trimpoints get saved in a table or m.file, for example:
X Y Z A
35 400 0.15 2
36 405 0.18 2.3
and so on..
That's my function that runs fminsearch for a range of speeds (U). A cost function (costfunct) stored in costfunctm interacts with a simulink model to calculate the Values
U_values = [01:1:10];
for k=1:length(U_values)
code
.
.
.
end
Many thanks in advance!

채택된 답변

Star Strider
Star Strider 2023년 2월 24일
편집: Star Strider 2023년 6월 22일
I assume that ‘a’, ‘xi’, and ‘phi’ are parameters returned (in that order) by fminsearch.
If so, then perhaps this —
U_values = [35:1:38];
for k=1:length(U_values)
k
U=U_values(k)
%Minimize with extra parameters
@(U) cost_ss(parameter,U);
testfunct=@(parameter)cost_ss(parameter,U);
%initial guess
x0=[800;0;0];
B = fminsearch(testfunct,x0);
a(k,:) = B(1);
xi(k,:) = B(2);
phi(k,:) = B(3);
end
U = U_values(:);
Results = table(U,a,xi,phi, 'VariableNames',{'U [m/s]','a [N]','xi [°]','phi [°]'})
I obviously can’t test this, however it should work if my assumptions are correct.
NOTE — The requirement that table variable names may not be valid MATLAB variable names may have been introduced after R2019b. (I don’t remember when it was introduced.) If so, the variable names will have to be changed (probably using underscores) to conform to that requirement. My table creation call will work in the most recent MATLAB releases that do not restrict them to be valid MATLAB variable names.
EDIT — (22 Jun 2023 at 18:54)
Different variable and function names, code unchanged.
.
  댓글 수: 2
Star Strider
Star Strider 2023년 2월 24일
As always, my pleasure!
You, too!
Steven Lord
Steven Lord 2023년 6월 22일
FYI the capability to have table variable names that are not valid MATLAB identifiers was introduced in release R2019b.
I don't know if you want to add that information to the list of MATLAB release features in Answers and/or to the Release History section of the Wikipedia page for MATLAB.

댓글을 달려면 로그인하십시오.

추가 답변 (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