You have the following data for bacteria population size, N, vs time (hours), t.
t=[0 .5 1 1.5 2 5 8 10];
N=[1005 1492 2226 33320 49530 54598 601845 2980958];
Write a MATLAB code segment that uses nlinfit to determine the best fit curve for the points in t and N according to this equation
N(t) = N(0)e^(rt)
where N0 and r are constants. Use initial guesses of N0 = 1000 and r =0.7 (offspring per hour). Determine the coefficient r and store it in the variable r.
Hints:
Your code must include the data for N and t above.
Use the command exp() for the exponential
clear
clc
t=[0 .5 1 1.5 2 5 8 10];
N=[1005 1492 2226 33320 49530 54598 601845 2980958];
f=@(b,x)b(1)*exp(b(2)*x);
beta = nlinfit(t,N,f,[1000,.7]);
SStot = sum((N-mean(N).^2));
SSres = sum((N-beta(1)*exp(beta(2)*t)).^2);
r=sqrt(1-SSres/SStot);
fprintf('r: %f\n',r)
%%below commands are for plotting
plot(t,N,'*')
tt=0:.01:10;
NN=beta(1)*exp(beta(2)*tt);
hold on
plot(tt,NN)
grid on
legend('Data','Fit','location','best')
title(sprintf('N(t) = (%g)e^{%gt}',beta))
Get Answers For Free
Most questions answered within 1 hours.