MATLAB: How to use the lsqcurvefit to find the best fit for the data

curve fittingfunlsqcurvefitodeode'sOptimization Toolbox

Due to the fact that I am very new to MatLab, I am attempting to use the built in tools of MatLab, so please help me understand the problem.
So I am currently working on modelling a kinetic reaction, and I am using ODE23t to solve the differential equations, giving me an output I can plot against the data I have collected. How can I use lsqcurvefit to match the theoretical curve more closely with the experimental curve obtained?
Here is an idea of what I have used:
time=HaPer(:,1);
absorbance=HaPer(:,2);
tstart=time(1);
tend=time(end);
tinterval=(time(2)-time(1));
tspan=tstart:1:tend;
iconc=[0.00001,0.0001,0,0,0,0];
k1=50000;k2=100;k3=0.3;k4=0.1;
The k(n) variables above are estimates. Now I added a function m file in the equation
function [dCONCdt]=HaemPeroxide(I,D)
global k1 k2 k3 k4
A=D(1);
B=D(2);
C=D(3);
E=D(4);
dCONCdt=[-k1*A*B+k2*C;
-k1*A*B+k2*C;
k1*A*B-k2*C-k3*C;
k3*C;
k3*C;
k4*E];
After which I use the ODE function to solve the differential equations
[T,CONCout]=ode23t('HaemPeroxide',tspan,iconc);
ext=[45635.167,0,0,0,0,0];
A=CONCout(:,1)*ext(1);
% Now I plot the two graphs against one another
hold off;
plot(time, absorbance)
hold on;
plot(time, A)
The corresponding graph shows that the two data sets does not match, and I want to try matching the theoretical data to the experimental data with lsqcurvefit by adapting the k(n) values. As I understand it, you need a function fun(x0, xdata) and your ydata. In my case, what would x0, xdata, and ydata represent? And can I use my function for 'fun', or is a different function setup required?

Best Answer

xdata=time;
ydata=absorbance;
x0 = k; %your initial estimate
For fun, you must write a function A=fun(k,time) that takes an arbitrary guess for the k vector as input and returns the corresponding A as output. It might also be a good idea to read this documentation which is specifically about using the Optimization Toolbox with ODEs.