Search code examples
matlabcurve-fittingdifferential-equations

Matlab: Parameter fit of ODE45 equation - only initial value for xdata


I have a system of 3 ODEs:

Cf' = kin * Cw * F - (kout + km) * Cf
Cw' = kout * Cf - kin * Cw(i) * F
Cm' = km * Cf

I measured data for all time points of Cf and only the initial values of Cw and Cm. Now I would like to fit my model to this data, finding the best parameter values for kin,kout and km.

Can I do this somehow with lsqcurvefit and ode45? Thank you!


Solution

  • So you got an ODE function

    function Dy = derivs(t,y,p)
      Cf = y(1); Cw = y(2); Cm = y(3);
      kin = p(1); kout = p(2); km = p(3);
      DCf = kin * Cw * F - (kout + km) * Cf;
      DCw = kout * Cf - kin * Cw * F;
      DCm = km * Cf;
      Dy = [ DCf DCw DCm ]
    end
    

    and a function to get solutions for specified points

    function y_data = solution(x_data,y0,p)
      sol = ode45(@(t,y)derivs(t,y,p), [x0,xe], y0)
      y_data = deval(sol, x_data)
    end
    

    Now the function to pass to the fitting procedure is

    @(p) solution(x_data,y0,p) 
    

    and you get a more detailed value table with the optimized parameters p_opt for plotting by

    x_plot = linspace(x0,xe,300)
    y_plot = solution(x_plot,y0,p_opt)
    plot(x_plot, y_plot)