fminsearch function stops after a few iterations in simulink real time with inverter

Hello all,
I have a problem when running fminsearch in simulink in a real time application. I want to calculate the stress amplitude and the stress angle by fminsearch, so that the unbalance factor kU2, or the norm of kU2 and kU0 becomes as small as possible. These values are then to be passed to an inverter. A part of the code is shown here:
x0 = [u1_abs u1_phi]
fun2 = @(x)kU2kU0berechnung(x,u2,u3,din);
y = fminsearch(fun2,x0);
function meankU2kU0 = kU2kU0berechnung(x,u2,u3,din)
u123 = [x(1)*exp(1i*x(2)/360*2*pi);u3;u2];
a = exp(1i*120/360*2*pi);
a2 = a^2;
T = [1 1 1; a2 a 1; a a2 1];
umg0 = T\u123;
meankU2kU0 = sqrt((abs((umg0(2)/umg0(1))*100)^2+((1-din)*abs((umg0(3)/umg0(1))*100))^2)/2);
end
Unfortunately, the fminsearch function stops after a few iterations. I think that this could be because the computer on which the model is running is too slow and then the Matlab function block in Simulink is triggered again before fminsearch is finished.
How can I adjust my model so that fminsearch really comes to a result?

Risposte (1)

Hi Yannic,
I would suggest explicitly defining / checking the tolerances and stopping criteria of the "fminsearch" function in your code to make sure that it executes the correct number of times / until a good solution is found.
I doubt that the issue you're facing is due to the speed of your target machine: with Simulink Real-Time, if a process cannot not be completed before being triggered again, it would result in a CPU overload and the simulation would stop.
I hope this is helpful!
Dimitri

11 Commenti

Hi Dimitri,
thank you for you answer!
I exported the measurement data and ran the code I wrote above. From this I created a mesh (attached). In the picture you can see which value for uabs was calculated in the Simulink Real-Time run. If you compare the two measurements you can see that the calculation in Simulink Real-Time would have to stop after only a few iterations to get the results from the picture.
So i don't think it's because of the tolerances and stopping criteria. Do you have another idea?
Is there a possibility to set a minimum of iterations for fminsearch?
Use "MaxIter" and "MaxFunEvals" out of the possible options:
"TolFun" and "TolX" might also be useful for your purpose.
Thanks for you answer!
Changing these parameters didn't help.
I managed to get fminsearch to run a certain number of times by customizing the matlab file for fminsearch. Unfortunately, the results of the simulation are different from the results of the calculation with the same data.
For example, here in the image I have the target amplitude calculated in Simulink. In the figure you can see the target amplitude that I calculated afterwards. The difference is very big.
I really don't know what the reason is.
Maybe my fminsearch file for code generation is defect?! Can someone upload a working file of fminsearch for code generation, please?
At the first glance, your objective function does not look that complicated. Can't you solve analytically for its minimum ?
That could also be a solution. Do you know how to do this in MATLAB?
The time to solve was too long to execute it here.
If you have more luck with your MATLAB at home, you could hardcode the solution for Simulink.
I think the sqrt, the division by 2 and the factor 100 do not influence the minimum - so I deleted them.
syms x [2 1] real
syms u2 u3 din real
u123 = [x(1)*exp(1i*x(2)/360*2*pi);u3;u2];
a = exp(1i*120/360*2*pi);
a2 = a^2;
T = [1 1 1; a2 a 1; a a2 1];
umg0 = T\u123
meankU2kU0 = (umg0(2)/umg0(1))*conj(umg0(2)/umg0(1))+(1-din)^2*(umg0(3)/umg0(1))*conj(umg0(3)/umg0(1))
dx1 = diff(meankU2kU0,x(1))
dx2 = diff(meankU2kU0,x(2))
solve([dx1==0,dx2==0],x)
Thanks for your solution. Unfortunately, the minimum calculation must go very quickly, because I want to give the results to an inverter that converts the values. Therefore, I have a maximum of one second to calculate the values.
As I said: If it works, you have explicit formulas for x(1) and x(2) that you can copy from the editor into a function file. Then it's just like evaluating an equation as
x(1) = a
x(2) = b
This would be great. With your function of meankU2kU0 Matlab does not manage to calculate anything and crashes. With mine I get a warning that it is unable to find explicit solution.

Accedi per commentare.

Prodotti

Release

R2020b

Richiesto:

il 2 Lug 2023

Modificato:

il 3 Lug 2023

Community Treasure Hunt

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

Start Hunting!

Translated by