Save results if fminsearch in a table or m.file

1 次查看(过去 30 天)
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 个评论
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