SOLUTION:
12.
Note: Since the student has not asked for question 12, I have taken the liberty of using a Matlab code instead of a graphing calculator. Hope that is okay.
Please find the code for fitting attached below.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%This code fits time vs population data into exponential and
logistic
%models
%time
t=0:10:70;
t=t';
P=[5.3,7.2,9.6,12.9,17.0,23.2,31.4,39.8]'; %population
f=fit(t,P,'exp1'); %exponential fitting and plotting
disp(f);
figure(1);
plot(f,t,P);
xlabel('Time in years from 1800');
ylabel('Population in millions');
title('Exponential model');
[ Qpre, D, sm, varcov] = fit_logistic(t,P); %logistic
fitting
tfit=0:1:70;
Plogifit=D(2)./(1 + exp(-D(3)*(tfit-D(1))));%finding the fit
data
%plotting
disp(D);
figure(2);
scatter(t,P);
hold on
plot(tfit,Plogifit);
xlabel('Time in years from 1800');
ylabel('Population in millions');
title('Logistic model');
legend('data','fitted model');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%This open source code is downloaded from https://in.mathworks.com/matlabcentral/fileexchange/41781-fit_logistic-t-q
function [ Qpre, p, sm, varcov] = fit_logistic(t,Q)
%fit a logistic function to time series Q(t).
% Inputs: t (time),Q (time series variable)
% Outputs: Qpre (logistic model fit to data) and
% p is 3 element vector containing parameters describing the
logistic:
% thalf, Qinf, and alpha
% Q(t) = Qinf/(1 + exp(-alpha*(t-thalf)))
% thalf is symmetric inflection point
% Qinf is value as t --> infinity
% alpha is time decay constant
% sm is 3 element vector giving 1-sigma confidence limits of
parameters
% e.g., thalf = p(1) +/- sm(1)
% simply double the values in sm to get 95% confidence limits
% varcov is the complete 3x3 variance-covariance matrix for
investigating
% how model paramters co-vary with each other.
% sm is sqrt(diag(varcov))
%
% Example:
% Qinf = 10.2; alpha = 0.33; thalf = 108.5;
% t = 100:120;
% Q = Qinf./(1 + exp(-alpha*(t-thalf)));
% noise = randn(1,length(t));
% Qd = Q+noise;
% Qpre = fit_logistic(t,Qd);
% figure(1)
% clf
% hold on
% plot(t,Qd,'o') % data
% plot(t,Qpre) % best fitting logistic
%
% Written by James Conder, Southern Illinois University, Oct.
2010
% Cleaned up for publishing May 16, 2013
% May 17, 2013: Allow for decreasing logistic.
% May 23, 2013: Fix instability when using short
% or long absolute times (relative to alpha = 1).
% May 28, 2013: added example in comments, fixed an introduced
bug
% from May 23 edit.
% Feb 12, 2014: Revisited occasional flatlining problem.
% (Qpre goes to mean).
% Made initial alpha more robust. Scaled to time rather than
simply
% defaulting to one (removes much of need for rescaling
time).
% Added check for flatlining. If occurs, reset seeds with
larger
% alpha and start over.
% Jan 25, 2016: calculate confidence limits for parameters
% equations are set up to solve for an increasing logistic.
% Check if decreasing and if so, reverse time
[~,I] = sort(t);
reverse_t = false;
if sum(diff(Q(I))) < 0 % decreasing in time
reverse_t = true;
t = -t;
end
% stretch short or long sequences in time to stabilize
alpha
tstretch = [];
if max(t)-min(t) < 1.e-4 || max(t)-min(t) > 1e5;
tstretch = 1./(max(t) - min(t));
t = t*tstretch;
end
% initial guesses for parameters
thalf = 0.5*(min(t) + max(t));
Qinf = max(Q);
alpha = 1./(max(t)-min(t)); alphareset = alpha;
flipQ = false;
if isrow(Q)
flipQ = true; % expecting a column vector. flip if row.
t = t';
Q = Q';
end
itermax = 1000 ; % number of maximum
iterations
epsilon = 1;
ii = 0 ; % initialize counter
thresh = 1.e-6 ; % threshold to stop iterating
G = zeros(length(t),3) ; % dimensionalize partial derivative
matrix
while epsilon > thresh
ii = ii + 1 ;
Qpre = Qinf./(1 + exp(-alpha*(t-thalf))) ; % 'predicted' data
if max(Qpre) - min(Qpre) == 0
% if Qpre flatlines, "a" likely needed to be seeded higher
% (sharper climb)
alphareset = 2*alphareset;
thalf = 0.5*(min(t) + max(t));
Qinf = max(Q);
alpha = alphareset;
Qpre = Qinf./(1 + exp(-alpha*(t-thalf))) ;
end
d = Q - Qpre ; % data vector (predicted - observed)
% linearized partial derivatives
ee = min(exp(-alpha*(t-thalf)),1.e12) ;
eee = 1./((1 + ee).^2) ;
G(:,1) = -Qinf*alpha*(ee.*eee) ; % dd/dthalf
G(:,2) = 1./(1 + ee) ; % dd/dQinf
G(:,3) = Qinf*(t-thalf).*(ee.*eee) ; % dd/dalpha
[U,S,V] = svd(G,0);
% Singular Value
Decomposition
Sinvdiag = 1./diag(S) ;
ising = Sinvdiag(1)./Sinvdiag < 1.e-12 ;
Sinvdiag(ising) = 0;
Sinv = diag(Sinvdiag);
dm = 0.1*V*Sinv*U'*d;
% get new parameters: m = m0 + dm
thalf = thalf + dm(1);
Qinf = Qinf + dm(2);
alpha = alpha + dm(3);
epsilon = norm(dm);
if ii > itermax
disp('max number of iterations
reached...exiting')
disp(['normalized epsilon: ' num2str(epsilon/thresh)])
epsilon = thresh ;
end
end
Qpre = Qinf./(1 + exp(-alpha*(t-thalf))) ; % 'predicted' data
if ~isempty(tstretch)
thalf = thalf/tstretch;
alpha = alpha*tstretch;
end
if reverse_t % decreasing logistic
thalf = -thalf;
alpha = -alpha;
end
if flipQ
Qpre = Qpre';
Q = Q'; % necessary for a posteriori variance
end
p = [ thalf Qinf alpha ];
%%% find confidence bounds for parameters (1-sigma)
if nargout > 2
sd = sum((Q-Qpre).^2)/(length(Q)-3); % a posteriori variance
varcov = sd*sd*inv(G'*G); % model variance-covariance matrix
sm = sqrt(diag(varcov)); % model variance
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
By running the code, we get the models and their graphs as given below:
13, 14:
15:
NOTE: IN Q15, IT IS GIVEN THAT y = c/2 WHEN THE GROWTH IS MAXIMUM IN THE LOGISTIC MODEL. AS A BONUS, THIS IS SHOWN IN THE FOLLOWING PAGES:
PLEASE GIVE A POSITIVE RATING IF YOU LIKE MY ANSWER. THANK YOU!
The table shows the population P (in millions) of the United States from 1800 to 1870 where t rep...
Consider the following Year Population (in millions) 1790 1800 1810 1820 1830 1840 1850 1860 1870 1880 1890 1900 1910 1920 1930 1940 1950 3.929 5.308 7.240 9.638 12.866 17.069 23.192 31.433 38.558 50.156 62.948 75.996 91.972 105.711 122.775 131.669 150.697 (a) Census data for the United States between 1790 and 1950 are given in the table above. Construct a logistic population model using the data from 1790, 1860, and 1930. (Assume that t is years since 1790 and P...
(a) Census data for the United States between 1790 and 1950 are given in the table above. Construct a logistic population model using the data from 1790, 1840, and 1910. (Assume that t is years since 1790 and P is population in millions. Round all coefficients to four decimal places.) P(t) = (b) Construct a table comparing actual census population with the population predicted by the model in part (a). Compute the error and the percentage error for each entry...