Vector length error in code
2 views (last 30 days)
Show older comments
Kailin Johnsson
on 12 Nov 2020
Commented: Kailin Johnsson
on 12 Nov 2020
Hello,
I am trying to run a simple code, and I am having a problem with vector lenght. I went back and counted all my data points and made sure each point was equal to 8.. perhaps I am missing something?
% Plot all models 1790-1860
%Malthusian Growth Model
% Time interval
% t = (1790:10:1860)';
% Previous population
Pn = [2.913 3.929 5.308 7.239 9.638 12.866 17.069 23.191]';
% Growth Rate Assumption
R = (0.349+1)';
%Malthusian Model
E = (Pn*R)';
% Discrete Logistic Model
% Parameter and intial condtions
r= (0.349);
% Carrying Capactiy
M= 330.0;
X0= 3.929;
p= [3.929 5.308 7.239 9.638 12.866 17.069 23.191 31.443]';
% For loop to generate seqeunce terms
for i=1:length(p)
F(i)=p(i)+r*p(i)*(1-p(i)/M)';
end
tBegin = 1790; % time begin
tEnd = 1860; % time end
%Gompertz Model
% Time Interval
% a=(1790:10:1860)';
% Population
b= [3.929 5.308 7.239 9.638 12.866 17.069 23.191 31.443]';
Data = b;
k2 = 330.0;
B = log(Data(2:8,1)./k2);
A = log(Data(1:7,1)./k2);
X = A\B;
b = -log(X(1,1));
P0 = Data(1,1);
LL = zeros(8,1);
LL(1,1) = P0;
for i = 2:8
LL(i,1) = exp((exp(-b)).*log(LL(i-1,1))+((1-exp(-b)).*log(k2)));
end
% Single Plot
% figure
% plot(Labels,Data,'b-*',Labels,PG,'r-*')
% title('Gompertz Model, 1790-1860')
% legend('Data','Model','Location','northwest')
% hold on
%R2
SST = sum((Data-mean(Data)).^2);
SSE = sum((Data-LL).^2);
R2 = 1- (SSE/SST)
% Beverton-Holt Model
% Parameter and intial condtions
Pe= 486.8
a1= 1.23065
b2= 2110.5
p3= [3.929 5.308 7.239 9.638 12.866 17.069 23.191 31.443]';
% Bevholt equation
E4 = a1*(p3)./(1+(p3/b2))
% Time Interval
% a=(1790:10:1860)';
% Population
b= [3.929 5.308 7.239 9.638 12.866 17.069 23.191 31.443]';
Data = b;
Labels2 = [1790:10:1860]';
Labels = [1790:10:2010]';
% Plot all models 1790-1860
figure
plot(Labels2,Data,'k-*',Labels2,E,'r-*',Labels2,F,'b-*',Labels2,LL,'m-*',Labels2,E4,'g-*')
title('US Population Data, 1790-1860')
legend('Data','Malthusian','Discrete Logistic','Gompertz','Beverton-Holt Model')
ylabel('Population (Millions)');
xlabel ('Years')
axis([1780 1860 0 200])
hold on
0 Comments
Accepted Answer
Cris LaPierre
on 12 Nov 2020
Edited: Cris LaPierre
on 12 Nov 2020
What line is causing your trouble? I just ran the code you shared and did not get any errors.
Best to copy/paste the entire error message (all the red text) instead of summarizing it.
Perhaps you have a variable in your workspace that is causing some trouble. Try clearning your workspace (they clear in your command window and press enter) and then run your code again.
More Answers (0)
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!