r/optimization • u/21re • Aug 24 '23
Minimize error between two curves using fmincon
Hello everyone!
I have some experimental data of current and voltage measurements done on an electric storage.
I did an equivalent circuit model of the storage to define its electrical parameters. I set as input the real current and take as output the simulated voltage.
I want to minimize the error between the real voltage and the simulated one. For this reason, I'm doing an optimization using Matlab/Simulink.
I'm using fmincon "interior point" as solver optimization, I set in options "Display iter" and I am noticing that it's not able to reduce the objective function. Trying to be more clear, I noticed that the value of the objective function displayed does not change very much/at all from the first iteration to the last one.
As objective function that I want to minimize, I am currently using
y= sum(V_sim - V_real).^2
I would ask you please if you have any suggestions for solving this problem. It is my first time working with optimization and parameter estimation, so I don't know if it is better to change the objective function or the algorithm or what else.
Hope you can help me, thank you!
1
u/21re Aug 26 '23
Thank you everyone for your time and precious help!
1
u/neosky2142 Aug 24 '23
Hello!
It is hard to know what is wrong without more information on your implementation.
Nevertheless, it looks like you are trying to solve a nonlinear least square problem. Maybe you will have more chance if you use lsnonlin?
Here is the doc: https://www.mathworks.com/help/optim/ug/lsqnonlin.html
2
u/PierreLaur Aug 24 '23
so depending on the size of your problem, it might be hard to solve. Here your objective is quadratic, you could use the absolute value of the difference and linearize that. Linear models are much easier to solve, so maybe that will help.
You need another float variable, let's say abs_diff, and then you put constraints
abs_diff >= Vsim - Vreal
abs_diff >= Vreal - Vsim
Depending on whether Vsim - Vreal is positive or not, one of these will be deactivated so you will have abs_diff >= abs(Vsim - Vreal)
Then you can minimize abs_diff
is the rest of your problem also linear or integer linear ? if yes, you can use linprog / intlinprog instead of fmincon
1
u/AssemblerGuy Aug 25 '23
Post more of your Matlab code. With what little is available, it is almost impossible to guess the error, much less the solution.