Optimising parameters in ODE with fminsearch or fmincon and ode15s
조회 수: 3 (최근 30일)
이전 댓글 표시
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?
댓글 수: 0
답변 (1개)
Stephan
2021년 4월 20일
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!