Save results if fminsearch in a table or m.file

3 views (last 30 days)
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!

Accepted Answer

Star Strider
Star Strider on 24 Feb 2023
Edited: Star Strider on 22 Jun 2023
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 Comments
Steven Lord
Steven Lord on 22 Jun 2023
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!