issues solving two interdependent optimization problems with nested functions
이전 댓글 표시
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!
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!