通过fminsearch将参数传递给ode45

时间:2014-11-10 13:12:35

标签: matlab

我需要解决七个差异系统。通过fmin研究相对于实验值的最小差异,找出最佳系数集( k 的8个值和 y0 的7个值)。

function dY = KIN241(t,y,k)
%
% version 2: direct formation of intermediates of the type {L}2M from MR3 and L (ligand)
% MR3  +  2L  --> {L}2M
% simultaneous formation of 4 intermediates A, B, C and D in four parallel reactions, 
% followed by 4 reactions to afford only one product P
%
%   dL/dt = -2*k_tot*MR3*(L)^2, where k_tot = k_LA + k_LB + k_LC + k_LD
%   dMR3/dt = -k_tot*MR3*(L)^2
%   with :
%   Variables: x(1) =  L, x(2) = MR3
%   Parameters: B(1) = k_LA, B(2) = k_LB, B(3) = k_LC, B(4) = k_LD 
%
%   dP/dt = k_AP*A + k_BP*B + k_CP*C + k_DP*D
%   dA/dt = 2*k_LA*MR3*(L)^2 - k_AP*A
%   dB/dt = 2*k_LB*MR3*(L)^2 - k_BP*B
%   dC/dt = 2*k_LC*MR3*(L)^2 - k_CP*C
%   dD/dt = 2*k_LA*MR3*(L)^2 - k_DP*D
%
%   with :
%   Variables: y(3) =  P, y(4) = A, y(5) = B, y(6) = C, y(7) = D
%   Parameters: B(5) = k_AP, B(6) = k_BP, B(7) = k_CP, B(8) = k_DP 
%   dL = dY(1); 
%   dMR3 = dY(2); 
%   dP = dY(3); 
%   dA = dY(4); 
%   dB = dY(5); 
%   dC = dY(6); 
%   dD = dY(7);
%y = indata;
%B = rand(1,8);
%disp(B);
k_LA = k(1);
k_LB = k(2);
k_LC = k(3);
k_LD = k(4);
k_AP = k(5);
k_BP = k(6);
k_CP = k(7);
k_DP = k(8);
%
        dY(1) = -2.*(k_LA + k_LB + k_LC + k_LD).*y(2).*(y(1))^2; 
        dY(2) = -(k_LA + k_LB + k_LC + k_LD).*y(2).*(y(1))^2;
        dY(3) = k_AP.*y(4) + k_BP.*y(5) + k_CP.*y(6) + k_DP.*y(7);
        dY(4) = 2.*k_LA.*y(2).*(y(1))^2 - k_AP.*y(4);
        dY(5) = 2.*k_LB.*y(2).*(y(1))^2 - k_BP.*y(5);
        dY(6) = 2.*k_LC.*y(2).*(y(1))^2 - k_CP.*y(6);
        dY(7) = 2.*k_LD.*y(2).*(y(1))^2 - k_DP.*y(7);
 dY = [dY(1); dY(2); dY(3); dY(4); dY(5); dY(6); dY(7)];

调用ode的函数如下:

function SSE = SSE_minimization(tspan_inp,val_exp,par)
         k_inp = par{1};
         y0_inp = par{2};
         f = @(Tt,Ty) KIN241(Tt,Ty,k_inp);  %function to call ode45
         %size_limit = length(k_inp);
         %options = odeset('NonNegative',1:size_limit,'RelTol',1e-8,'AbsTol',[1e-8   1e-8     1e-8 1e-8 1e-8 1e-8 1e-8]);
         [ts,val_theo] = ode45(f, tspan_inp, y0_inp);  %Cexp is the state variable     predicted by the model
         err = val_exp - val_theo;
         SSE = sum(sum(err.^2));   %sum squared-error

实验数据在那里:

Time_M =

       0
      60
     381
     702
    1023
    1344
    1665
    1986
    2307
    2628
    2949
    3270
    3591
    3912
    4233
    4554
    4875
    5196
    5517
    5838
    6159
    6480
    6801
    7122
    7443
    7764
    8085

浓度_M =

0.0385    0.0224    0.0385    0.0385    0.0385    0.0385    0.0064
0.0384    0.0343    0.0381    0.0351    0.0359    0.0262    0.0301
0.0374    0.0377    0.0251    0.0207    0.0377    0.0348    0.0369
0.0364    0.0379    0.0184    0.0264    0.0376    0.0366    0.0373
0.0358    0.0380    0.0163    0.0291    0.0375    0.0371    0.0375
0.0353    0.0380    0.0162    0.0300    0.0374    0.0373    0.0376
0.0348    0.0381    0.0166    0.0305    0.0374    0.0374    0.0376
0.0344    0.0381    0.0170    0.0312    0.0373    0.0374    0.0377
0.0339    0.0381    0.0177    0.0312    0.0372    0.0376    0.0378
0.0336    0.0382    0.0184    0.0317    0.0372    0.0375    0.0378
0.0331    0.0382    0.0190    0.0317    0.0372    0.0376    0.0379
0.0327    0.0382    0.0194    0.0319    0.0372    0.0376    0.0379
0.0324    0.0382    0.0201    0.0322    0.0372    0.0376    0.0379
0.0320    0.0382    0.0206    0.0324    0.0371    0.0377    0.0379
0.0317    0.0382    0.0212    0.0327    0.0371    0.0376    0.0380
0.0313    0.0382    0.0217    0.0328    0.0371    0.0377    0.0380
0.0309    0.0382    0.0222    0.0329    0.0371    0.0377    0.0380
0.0307    0.0382    0.0228    0.0333    0.0371    0.0377    0.0380
0.0304    0.0383    0.0233    0.0335    0.0371    0.0377    0.0381
0.0301    0.0383    0.0237    0.0337    0.0371    0.0377    0.0381
0.0299    0.0383    0.0242    0.0339    0.0371    0.0378    0.0381
0.0296    0.0383    0.0248    0.0341    0.0371    0.0378    0.0381
0.0291    0.0383    0.0250    0.0336    0.0370    0.0378    0.0381
0.0288    0.0383    0.0255    0.0339    0.0370    0.0382    0.0381
0.0285    0.0383    0.0258    0.0340    0.0370    0.0378    0.0382
0.0283    0.0383    0.0262    0.0341    0.0370    0.0378    0.0382
0.0283    0.0383    0.0268    0.0346    0.0370    0.0379    0.0381

当我从主代码打电话时:

    y0 = (rand(1,7))';
    k0 = (rand(1,8))';
    par0{1} = k0; par0{2} = y0;
[solution, fval, exitflag, output] = fminsearch(@(par) SSE_minimization(tspan,Concentration_M,par0),y0);
        k_opt = solution(1);
        y0 = solution(2);

一个周期后,我收到以下错误:

 Attempted to access k(2); index out of bounds because numel(k)=1.

Error in KIN241 (line 34)
k_LB = k(2);

实际上,代码只找到k_opt的一个值而不是八个,而y0只找到一个而不是七个......

请您帮我设置/更正代码,以优化此系统方程的k和y0参数。

提前多多谢谢!

0 个答案:

没有答案