Code running in one tab, but not another

2 ビュー (過去 30 日間)
Kailin Johnsson
Kailin Johnsson 2020 年 11 月 12 日
コメント済み: Kailin Johnsson 2020 年 11 月 12 日
Hi all,
I am running a code (attached below) and there is an error... although I have run the same code just with more values for data perfectly fine. I have also attached a pciture of the error in case that helps. Does anyone have any ideas why this might be? Thanks
% 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= 451.7;
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:23,1)./k2);
A = log(Data(1:22,1)./k2);
X = A\B;
b = -log(X(1,1));
P0 = Data(1,1);
LL = zeros(23,1);
LL(1,1) = P0;
for i = 2:23
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(Labels,Data,'k-*',Labels,E,'r-*',Labels,F,'b-*',Labels,LL, 'm-*', Labels,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

採用された回答

Durganshu
Durganshu 2020 年 11 月 12 日
In the line no. 59:
B = log(Data(2:23,1)./k2);
You are accessing Data values in the rows 2:23, while Data is just an 8x1 double matrix. That's why you're getting this error. Make sure that the values inside Data() don't exceed its limit.
Here, you can either try to access the values within the range of Data or you can extend the size of Data according to your need. Only then you won't get such errors.
  3 件のコメント
Steven Lord
Steven Lord 2020 年 11 月 12 日
Can you show line 113 and maybe 5 lines before and after that line, for context? In the screen shot you posted originally there was a $ at the end of that line, but that wasn't in the code you copied and pasted into the post.
Kailin Johnsson
Kailin Johnsson 2020 年 11 月 12 日
Ah, yes there was an accidently $! That fixed the problem! Thank you!!!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by