MATLAB: HELP ME PLEASE! How to find parameter estimation with fminsearch? And solve it with ode45? Why it always error when i run the codes

differential equationserrorfminsearchfunctionindex exceeds matrix dimensionsmodelingodeode45parameterparameter estimation

this is my code, i save the function with fungsi.m
function dydt = fungsi(t,y,p)
dydt(1) = p(1)*(1135254-y(1)-y(2))*(y(1)/(y(1)+p(4)*1135254))-(p(2)+136)*y(1);
dydt(2)= p(2)*y(1)-(136+p(3))*y(2);
end
this in the other file for fminsearch (filename: fmin.m) with data of y(1) and t
rng default
t = [1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:30:31:32:33:34:35:36:37:38:39:40:41:42:43:44:45:46:47:48:49:50:51:52:53:54:55:56:57:58:59:60:61];
y = [25:24:27:22:22:22:16:11:18:18:19:24:12:22:15:10:11:11:20:10:16:21:13:13:10:12:12:12:6:13:6:9:10:10:6:10:10:4:3:6:8:8:8:13:12:12:15:4:14:12:10:12:8:8:8:5:12:20:10:12:12];
fun = @(p)fungsi(p,t,y);
x0 = 25;
[x,fval] = fminsearch(fun,x0)
This is the error output:
Index exceeds matrix dimensions.
Error in fungsi (line 2)
dydt(1) = p(1)*(1135254-y(1)-y(2))*(y(1)/(y(1)+p(4)*1135254))-(p(2)+136)*y(1);
Error in fmin>@(p)fungsi(p,t,y)
Error in fminsearch (line 200)
fv(:,1) = funfcn(x,varargin{:});
Error in fmin (line 6)
[x,fval] = fminsearch(fun,x0)
I want to search parameter of p (p(1) until p(4) Tell me whats wrong.. please correct me! and what should i do then for coding use ode45? I need it really soon for my research, today. PLEASE HELP! Thank you!

Best Answer

This runs.
You can run it as posted if you wrap all of this code in a function (rather than a script). Remember that the last line of the function must an end call.
Otherwise, save the ‘fit_fungsi’ function to its own .m file named ‘fit_fungsi.m’ in your MATLAB user path. Then run the rest of my code as a separate script.
You will need to define the appropriate initial parameter estimates ‘p0’, and decide which column returned by ode15s is the one you want to return as ‘yr’ that fminsearch uses to estimate subsequent elements of ‘p’. I use ode15s here because I know nothing about the system you are estimating, and it is good for ‘stiff’ systems.
function yr = fit_fungsi(p,t) % Objective Function: Integrates ODE & Returns Vector
x0 = p(5:6);
[t,ys]=ode15s(@fungsi,t,x0);
function dydt = fungsi(t,y)
dydt = zeros(2,1);
dydt(1) = p(1)*(1135254-y(1)-y(2))*(y(1)/(y(1)+p(4)*1135254))-(p(2)+136)*y(1);
dydt(2)= p(2)*y(1)-(136+p(3))*y(2);
end
yr = ys(:,2);
end
t = [1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20;21;22;23;24;25;26;27;28;29;30;31;32;33;34;35;36;37;38;39;40;41;42;43;44;45;46;47;48;49;50;51;52;53;54;55;56;57;58;59;60;61];
y = [25;24;27;22;22;22;16;11;18;18;19;24;12;22;15;10;11;11;20;10;16;21;13;13;10;12;12;12;6;13;6;9;10;10;6;10;10;4;3;6;8;8;8;13;12;12;15;4;14;12;10;12;8;8;8;5;12;20;10;12;12];
p0 = rand(6,1)*1000; % Parameter Vector: p(1:4) = Parameters, p(5:6) = ODE Initial Conditions
B = fminsearch(@(p) norm(y - fit_fungsi(p,t)), p0) % Estimate Parameters
yfit = fit_fungsi(B,t);
figure(1)
plot(t, y, 'pg', 'MarkerFaceColor','g')
hold on
plot(t, yfit, '-r')
hold off
grid
For an explanation of the code, see the discussions in any of these:
All of these involve estimating the parameters of a system of differential equations, using lsqcurvefit to do the regressions. Other than using lsqcurvefit, they are essentially the same as your problem, and use the same approach.