I'm trying to use lsqcurvefit to fit three coefficiencients to a system of coupled equations that I solve using ode45. The data I am fitting comes from the below code:
function [N T] = rk4method(k,t)n0=[1e20, 0];[T,Nv]=ode45(@DifEq,t,n0);%
function dN=DifEq(t,n)dndt = zeros(2,1);dndt(1)= -((8.89e+6)+(1.75e+7)).*n(1) + (1.91e+6).*n(2) - k(1).*n(1).^2 - k(2).*n(1).*n(2) + 0.25.*k(3).*n(2).^2;dndt(2)= (1.75e+7).*n(1)-(1.91e+6).*n(2)-(3.43e+5).*n(2)-1.25.*k(3).*n(2).^2;dN=dndt;endN = Nv(:,1);T = T;end
I use this to create my data:
[ndat t] = rk4method([5.1e-12 1.12e-12 5.12e-14], [0 2e-6]);
Here's my fitting code:
n = ndat;k0=[1e-11 1e-13 1e-15];lb=[1e-16 1e-16 1e-18];ub=[1e-8 1e-8 1e-8];%tried but doesn't help: 'FunctionTolerance',1e-8,'FiniteDifferenceType','central',
options = optimoptions('lsqcurvefit','Diagnostics','on','Display','iter-detailed');[k,Resnorm]=lsqcurvefit(@rk4method,k0,t,n,lb,ub,options);kNfin = rk4method(k,t);figure(1)hold on;plot(t, log10(ndat(:,1)), '*r');plot(t, log10(Nfin), '-k');function [N T] = rk4method(k,t)n0=[1e20, 0];[T,Nv]=ode45(@DifEq,t,n0);function dN=DifEq(t,n)dndt = zeros(2,1);dndt(1)= -((8.89e+6)+(1.75e+7)).*n(1) + (1.91e+6).*n(2) - k(1).*n(1).^2 - k(2).*n(1).*n(2) + 0.25.*k(3).*n(2).^2;dndt(2)= (1.75e+7).*n(1)-(1.91e+6).*n(2)-(3.43e+5).*n(2)-1.25.*k(3).*n(2).^2;dN=dndt;endN = Nv(:,1);T = T;end
lsqcurvefit isn't actually changing the value of the parameters k from its initial values k0, with Warning: Derivative finite-differencing step was artificially reduced to be within bound constraints. This may adversely affect convergence. Increasing distance between bound constraints, in dimension 3, to be at least 2e-08 may improve results.
I've tried tweaking my ub to be greater, however this doesn't result in convergence.
Best Answer