clear; fname = 'ABC'; par = [0.08 0.02]; % [0.0629595 0.0211523] myfile = strcat(fname, '.dat'); fname = load (myfile); tvec = fname(:,1); ymat = fname(:,2:4); x0 = [1 0 0]; %initial values, ODE's [t, x] = ode45(@mysystem, tvec, x0, [], par); plot (t, x, tvec, ymat, 'Marker', 'o', 'MarkerSize', 6) title ('Preliminary: concentrations vs. time'); xlabel ('\it t'); ylabel ('mole') new = fminsearch(@mydiff, par, [], tvec, ymat, x0) [t, x] = ode45(@mysystem, tvec, x0, [], new); figure plot (t, x, tvec, ymat, 'Marker', 'o', 'MarkerSize', 8) title ('(Optimised) Concentrations vs. time'); xlabel ('\it t'); ylabel ('mole') function xprime = mysystem(t,x,par) % ODE's k1 = par(1); k2 = par(2); xprime = [-k1*x(1); -k2*x(2) + k1*x(1); k2*x(2)]; end function d = mydiff(par, tvec, ymat, x0) [t, x] = ode45(@mysystem, tvec, x0, [], par); d = sum((x-ymat).^2, 'all') end