Estimating parameters of ODE using ode45 and fminsearch

22 views (last 30 days)
Hello all I am trying to estimate parameters of Ordinary Differential Equations (ODE). I am receiveing the following error:
Subscripted assignment dimension mismatch.
Error in fminsearch (line 190)
fv(:,1) = funfcn(x,varargin{:});
I tried but I couldn't recognize where I am making mistake. I am providing my code kindly suggest me where I am going wrong. -- Thanks
% == ODE ==
function dx=LV(t,x,theta)
dx(1) = x(1)*(alpha-beta*x(2));
dx(2) = -x(2)*(gamma-delta*x(1));
% == Error function ==
function err = ODE_fit(exp_t, exp_y, theta)
% exp_y = Experimental observation at time exp_t
[t,y] = ode45(@(t,X)LV(t,X,theta), exp_t, [5 3]);
err = sum((y-exp_y).^2); % compute error between experimental y and fitted y
% == Script ==
exp_t = [0, 0.20, 0.400, 0.60, 0.80, 1, 1.20, 1.40, 1.60, 1.80, 2];
exp_y = [4.35,4.26,2.96,3.13,2.25,2.65,3.22,2.85,4.97,4.99,5.94;...
theta0 = [2.5 0.75 4.25 1.5];
p_estimate = fminsearch(@(theta)ODE_fit(exp_t, exp_y, theta), theta0);

Answers (1)

Walter Roberson
Walter Roberson on 15 Jan 2016
ode45 returns a y with as many columns as there are values in the initialization vector. Your initialization has two elements so two columns will be returned. You do subtraction of an array of equal, getting an N x 2 array. You square the elements, getting an N x 2 array. You then sum(); by default sum() works along the first dimension, so your output is going to be 1 x 2 . You return that back to the internals of fminsearch, but fminsearch is expecting a scalar.
I speculate that you want another sum() around the sum() so that you get a scalar output.
Note: if you have the System Identification toolbox then if I understand correctly you can use some of the functions there to achieve the purpose you are coding for here.
Pankaj on 14 Nov 2017
sorry Kelly, but it's been too long, I might have solved it some way. At present I may not be able to dig it out.
Torsten on 15 Nov 2017
err = sum(sum((y-exp_y).^2));
should work.
Best wishes

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!