Optimising parameters in ODE with fminsearch or fmincon and ode15s

2 views (last 30 days)
I am trying to optimise two parameters, a0 and a1 to minimise Cb at z=2 for the following function:
function dCbdz = myModel(z,Cb,a0,a1)
T=a0+a1.*z;
R= 8.3145;
Ca0 = 0.7;
k1 = 1.37.*10^10.*exp(-75000./(R.*a));
k2 = 1.19.*10^17.*exp(-125000./(R.*a));
dCbdz = 1./(4.*1.37.*10^10.*exp(-75000./(R.*a)).*Ca0.*exp(-4.*.*z)-4.*k2.*Cb)
I've then put my objective function into a separate script:
function obj = myObj(a0,a1)
n = 10;
length = linspace(0,2,n);
Cb0 = 0;
[z,Cb]= ode45(@(z,Cb) myModel(z,Cb,a0,a1),length,Cb0);
Finally, I call the minimisation function
a = fminsearch('myObj',[300 0]);
I get an error of not enough input arguments
Error using myObj (line 6)
Not enough input arguments.
Any help please?

Answers (1)

Stephan
Stephan on 20 Apr 2021
I had to make some guesses, check how to adapt the code:
a = fminsearch(@myObj,[300 0]);
function obj = myObj(a) % only one input as vector
a0 = a(1); % unpack vector a
a1 = a(2); % unpack vector a
n = 10;
length = [0 n];
Cb0 = 0;
[~,Cb]= ode45(@(z,Cb)myModel(z,Cb,a0,a1),length,Cb0);
obj = sum(Cb); % you did not make a scalar return to minimze, what should be minimized?
end
function dCbdz = myModel(z,Cb,a0,a1)
T=a0+a1.*z; % T is unsued
R= 8.3145;
Ca0 = 0.7;
k1 = 1.37.*10^10.*exp(-75000./(R.*a0)); % replaced a with a0 --> correct?
k2 = 1.19.*10^17.*exp(-125000./(R.*a1)); % replaced a with a1 --> correct?
dCbdz = 1./(4.*1.37.*10^10.*exp(-75000./(R.*a0)).*Ca0.*exp(-4.*k1.*z)-4.*k2.*Cb);
% ^ ^
% | |
% replaced a with a0 |
% your code: exp(-4.*.*z)
end

Products

Community Treasure Hunt

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

Start Hunting!