Failure in initial objective function evaluation. LSQNONLIN cannot continue

Hello
I am trying to optimize the following ODE
Here, B is a known constant. p1 and p2 are the variables to be optimized.
So I first created function files for the function and the ode as follows.
Next I wrote the code for optimization using the 'Optimization Toolbox'.
type diffun1
a0 = 0;
B= 5000;
temp=SFOR_P3_datapts(:,1);
p=optimvar("p",2,"LowerBound",1,"UpperBound",10^20) %p is the optimization variable ( containing p1 and p2)
%minimize sum of squares of the differences between the ODE solution with parameters p and the actual values
myfcn=fcn2optimexpr(@ode1,p,temp,a0,'OutputSize',[length(temp),1])
obj=sum((myfcn-a_actual).^2);
prob=optimproblem("Objective",obj); % default is to minimize the objective function
show(prob)
p0.p=[100,10]; %Initial guess of the parameters
[p_soln,sumsq]=solve(prob,p0) %Determining the parameters such that obj fcn (sum of squares) is the minimum
But here an error message pops up.
The procedure adopted is similar to the Mathworks example "Fit ODE Problem Based"
https://in.mathworks.com/help/optim/ug/fit-ode-problem-based-least-squares.html
Any help on how to solve this error would be deeply appreciated.
Thank You

 Réponse acceptée

You define your function to expect 3 input arguments and ignore the first of them. But you pass only two. MATLAB works positionally not by name.

3 commentaires

Thank you so much for the suggestion. That issue is resolved. But when I run the code now for different initial values of the optimization variable, the result is always the initial value itself.
Any suggestions to correct this?
Thank you for the help!
MATLAB is case sensitive. dadt and dadT are different variables. You assign 0 to the one you return.
The code is working now. Thank you so much for your help!

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by