issues solving two interdependent optimization problems with nested functions
Mostrar comentarios más antiguos
Hi,
I am trying to write a nested function to do the following:
- Solve a series of equation using fsolve - find values of a dependent variable (vector x) for which a list of equations are all set to 0
- These equations depend on some external parameters, a couple of which I need to solve a minimization problem for.
- In particular, I would like to use lsqnonlin to minimize the distance between some model output (functions of the x vector mentioned above) and some external data targets that I am trying to match. This should provide me with the missing parameters.
As the two problems are interdependent, I thought nested functions would work here but I am struggling, never having used these before. I copy some example code that I tried with only one component for x and 2 parameters.
function [param,xsol] = callfun(x0,par,trgt)
param=lsqnonlin(@(par)nestedf,par,trgt);
xsol = fsolve(@(x)mainf,x0,param);
function F=mainf(x,param)
F=x^2*param(1)+param(2);
@nestedf;
function G=nestedf(param,targets)
calib(1,1)=param(1)*x;
calib(2,1)=param(2);
G=(calib-targets);
end
end
end
Besides various different error messages and moving things around and out of the function itself, I am not convinced this actually does what I have in mind. For sure I am not calling the functions correctly in the outer one. I thought it would have to be something like:
- Starting from a guess of x0, par0 and some targets, call an lsqnonlin solver to find the parameters that minimize the distance from the targets for those guesses
- Call an fsolve to find the x for which function F is 0, given those parameters.
- Repeat these processes until both have converged.
Any help would be greatly appreciated, thanks!
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Solver Outputs and Iterative Display en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!