Trying to fit parameters for an ODE model to real data using lsqcurvefit

13 views (last 30 days)
Hi! I'm trying to fit parameters for an ODE model to real data using lsqcurvefit. The issue I'm having is that lsqcurvefit always returns the initial guess. I've looked at previous forum posts similar to this problem and found a function to use as an example (Igor_Moura.m) and I can't seem to find any real differences between the code I've constructed and that example. I think the issue may be a result of the parameter not being able to change once fed into the ODE function, but I'm not sure how to fix it if that's the case. (My file is called SIRpvectorattempt.m)
function SIRpvectorattempt
%define function so solve for data
function G = SIRsolve(pvector,t)
x0=[0,5.704*10^6];
%ODE solver
[T,x]= ode45(@ODESIRlsq,t,x0);
function dx = ODESIRlsq(t,x)
%x(1)=i(t), x(2)=s(t), pvector(1)=lambda, pvector(2)=mu
%define ODE's
dx=zeros(2,1);
dx(1)=pvector(1).*x(2).*x(1)-pvector(2).*x(1);
dx(2)=-pvector(1).*x(2).*x(1);
end
%infected and susceptible populations over timeframe
G=x; % (:,1:2);
end
t=[1;2;3;4;5;6;7;8;9;10];
%real data from xcel file
ydata=zeros(10,2);
ydata(:,1) = xlsread('Covid Case Data Sorted SIR with columns for singapore','Singapore','H2:H11'); %453 itreal
ydata(:,2) = xlsread('Covid Case Data Sorted SIR with columns for singapore','Singapore','I2:I11'); %streal
pvecguess = [1;1];
pvector = lsqcurvefit(@SIRsolve,pvecguess,t,ydata)
end

Accepted Answer

Star Strider
Star Strider on 8 May 2021
‘... I can't seem to find any real differences between the code I've constructed and that example.’
I agree that the initial parameter estimates are likely the problem. If you have the Global Optimization Toolbox, this version of that same code (attached), using the ga function to estimate the parameters, will likely do what you want. (This will require a few tweaks for your current code to work with ga.) It may take a few runs until ga discovers a suitable set of parameters, however it will likly succeed.
Note that if you want to use lsqcurvefit to produce results that could be used with nlparci to estimate the parameter confidence intervals (and to slightly tweak the paramters themselves), use the parameter estimates that ga produces as the initial parameter estimates for lsqcurvefit.
  4 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!