- fun is the function to be minimized. It accepts an input x and returns a scalar f, the objective function evaluated at x. The function fun can be specified as a function handle for a function file
How to run ODE45 with optimizing parameters and dependent variables on time
5 views (last 30 days)
Show older comments
Hi all,
I am trying to run a ode45 solver for system of first order differential equations for chemical kinetics equations. I have set up a function called "myodefcn" defined as
function dx = myodefcn(T, X, kHEPSelf, kHEPSelfA, kHEPSelfB, kHEPHO2,...
kHOCH2O2, kHO2Self, kOO2, kHO2O3, kHO2Cl, kO3Cl, kClCEb, kClCEa, ...
kClO2, kClself, kHO2OH, kOHO, kOHO2, kOHClO)
which has all the differential equations in form dx/dt. Rest all are constants named like k___, these constants depend on parameters A and B defined as
X0(1) = 0.4e14*num*B; %and similar
kHEPSelf = 3.5e-12*scale*A; % and similar
and these are defined in another function "myobjective" which calculates these k___ values and calls the myodefcn as,
f = @(T,X) myodefcn(T, X, kHEPSelf, kHEPSelfA, kHEPSelfB, kHEPHO2, kHOCH2O2, kHO2Self,...
kOO2, kHO2O3, kHO2Cl, kO3Cl, kClCEb, kClCEa, kClO2, kClself, kHO2OH, kOHO, ...
kOHO2, kOHClO);
T = bX;
options = odeset('RelTol',1e-4,'AbsTol',[1e-4 1e-4 1e-5]);
[ts,xs] = ode45(f,T, X0);
err = bY - xs(:,1);
SSE = sum(err.^2);
the call for function "myobjective" is as follows
[ts,xs, SSE] = myobjective(P, bX, bY)
where
A = P(1); B = P(2);
and bX and bY are experimentally determined time and data vectors.
Now the problem is when I try to call the "myobjective" function using the fminsearch to minimize error SSE
A = 10; B = 2; P0 = [A, B];
p = fminsearch(@(P) myobjective(P, bX, bY), P0);
and get the values for A and B. The optimization fails and gives this error
Subscripted assignment dimension mismatch.
Error in fminsearch (line 197)
fv(:,1) = funfcn(x,varargin{:});
Also, I tried curve fitting the bX-bY data using 1-order polyfit and use coefficients for A and B but that doesn't seems the right fit as fit is linear and if I go with higher order, I don't know how to relate them to A and B.
Can someone tell me how to solve this error and get the values for A, B and X? Or how to pass these parameters to ode45 solver where it can solve the diff eqns and optimize the values for A and B?
Thanks.
0 Comments
Answers (1)
Star Strider
on 9 May 2014
I believe the problem are the values returned from myobjective. It returns 3 outputs instead of the required 1. That would likely throw the error you are seeing.
From the documentation for fminsearch:
Have it return a single output, and providing there are no other errors, fminsearch should be happy. (If you have constraints, consider using fmincon instead, if you have the Optimization Toolbox.)
3 Comments
Star Strider
on 9 May 2014
The problem is that fminsearch doesn’t use the other two and doesn’t want them. If you need them in another function (once you have optimised it), write two versions of myobjective, one for fminsearch and one for your other function. I don’t know the details of what you’re doing, but there are two possibilities:
- Write a third function that only returns the scalar to fminsearch that it wants,
- declare the other variables that you use elsewhere, but that fminsearch doesn’t want, as global variables — not the best programming practice in my opinion, but perhaps necessary here — and have myobjective return only what fminsearch wants to it.
See Also
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!