%least squares example 1 %this code creates "experimental data" with error and then fits a model to this data global RU0 %global variables to use in functions global RUeq error = 100; %amount of experimental noise, to be multiplied by rand-0.5 RU0 = 50; %baseline RU value for an SPR experiment RUeq = 1500; %equilibrium RU value for an SPR experiment t = 0:50:600; %time vector, 0 to 600 s %first, make the true solution (Rtrue) using the true parameters %then make Rdata, our experimental data (Rtrue+noise) koff = 0.005; %true parameter, in 1/s for i=1:length(t) Rtrue(i) = RU0 + (RUeq - RU0)*exp(-koff.*t(i)); Rdata(i) = Rtrue(i)+error*(rand-1/2); end %then, starting with the known initial conditions, %and a guess for koff, %fit the model to the data and get a fitted value for koff k = 0.1; %guess for koff %fit for 1 parameter: koff [fit_koff, Res, J] = nlinfit(t,Rdata,@SPR_function,k); %and lastly, use your fitted koff to solve for R for i=1:length(t) Rfitted(i) = RU0 + (RUeq - RU0)*exp(-fit_koff.*t(i)); end figure(1); plot(t,Rtrue,'b-'); hold on plot(t,Rdata,'r*'); plot(t,Rfitted,'g-'); legend('true RU function','RU data','RU model from fitted koff'); xlabel('time'); ylabel('RU'); title('SPR dissociation data');