MATLAB Answers

Pankaj
0

Estimating parameters of ODE using ode45 and fminsearch

Asked by Pankaj
on 15 Jan 2016
Latest activity Commented on by Torsten
on 15 Nov 2017
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=zeros(2,1);
alpha=theta(1);
beta=theta(2);
gamma=theta(3);
delta=theta(4);
dx(1) = x(1)*(alpha-beta*x(2));
dx(2) = -x(2)*(gamma-delta*x(1));
end
% == 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
end
% == 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;...
2.60,3.23,2.50,1.89,2.25,1.21,1.05,1.55,1.66,1.44,1.76]';
theta0 = [2.5 0.75 4.25 1.5];
p_estimate = fminsearch(@(theta)ODE_fit(exp_t, exp_y, theta), theta0);

  0 Comments

Sign in to comment.

1 Answer

Answer by 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.

  4 Comments

Show 1 older comment
Ever figure it out?
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.
err = sum(sum((y-exp_y).^2));
should work.
Best wishes
Torsten.

Sign in to comment.