Hello,
I seem to be having this issue of an unrecognize variable, despite being defined, when trying to run a simple Gompertz equation. I have tried removing the clear and clc becaue that was the issue last time, however it did not work. I have attached a screenshot and my code below. Thanks!
%Gompertz Equation
clear
clc
% Parameter and intial condtions
r= (0.349);
P= [2.913 3.929 5.308 7.239 9.638 12.866 17.069 23.191 39.818 50.189 62.947 76.212 92.228 106.021 122.775 132.164 150.697 179.323 203.302 226.545 248.709 281.421 308.745];
k2= 330.0;
% Time interval
t = (1790:10:2010);
%Gompertz Equation
% for i=1:length(t)
%GM(i)= -r*P(i)*log((P(i)/k2));
%end
for i=1:length(t)
LL(i,1) = exp((exp(-j)).*log(LL(i-1,1))+((1-exp(-j)).*log(k)));
end
%% Fix K = 3.3e+8
k2 = 330.0;
B = log(P(2:23,1)./k2);
A = log(P(1:22,1)./k2);
X = A\B;
j = -log(X(1,1));
P0 = P(1,1);
LL = zeros(23,1);
LL(1,1) = P0;
% Time Interval
a=(1790:10:2010)';
% Population
j= [3.929 5.308 7.239 9.638 12.866 17.069 23.191 31.443 39.818 50.189 62.947 76.212 92.228 106.021 122.775 132.164 150.697 179.323 203.302 226.545 248.709 281.421 308.745]';
% Plot
plot (a,j,'bo');hold on
plot(t,GM,'r');
line_color=['r'];
hold off
legend('Census Data','Gompertz Model');
axis([1790 2010 0 500]);
title('US Population Data');
ylabel('Population (Millions)');
xlabel('Years');

 채택된 답변

Cris LaPierre
Cris LaPierre 2020년 11월 11일
The problem is you are using LL to define the values of LL. The first time through, LL has not yet been defined. Incidentally, you also use k in the equation, but define a variable k2.
Upon fixing that, you will encounter another error that indices must be real positive integers. This is because, when i=1, log(LL(i-1,1)) will try to take the log of LL(0,1).
My suggestion is to first define a value for LL(1,1), and then start your for loop at i=2.
% Parameter and intial condtions
k= 330.0;
% Time interval
t = (1790:10:2010);
LL = 0;
for i=2:length(t)
LL(i,1) = exp((exp(-j)).*log(LL(i-1,1))+((1-exp(-j)).*log(k)));
end
LL
LL = 23×1
0 0 0 0 0 0 0 0 0 0

댓글 수: 8

Thank you, I have fixed those issues, but now it is saying "Index in position 1 exceeds array bounds (must not exceed 1).
Error in GompertzEquation (line 23)
B = log(P(2:23,1)./k2);"
Cris LaPierre
Cris LaPierre 2020년 11월 11일
편집: Cris LaPierre 2020년 11월 11일
P is defined at the beginning of your code. Check its size in your workspace. It looks like it is 1x23, but here you are trying to access rows 2:23 in the first column (indices are backwards), which does not exist. Hence the error.
I am sorry I am having such a difficult time with this. I have attached the code I am trying to run now, and have changed what feels like each 1x23 element with no luck... maybe I am missing something furhter?
%Gompertz Equation
clear
clc
% Parameter and intial condtions
r= (0.349);
P= [2.913 3.929 5.308 7.239 9.638 12.866 17.069 23.191 39.818 50.189 62.947 76.212 92.228 106.021 122.775 132.164 150.697 179.323 203.302 226.545 248.709 281.421 308.745];
k2= 330.0;
P0 = P(1,1);
LL = zeros(23,1);
LL(1,1) = P0;
%% Fix K = 3.3e+8
k2 = 330.0;
B = log(P(2:23,1)./k2);
A = log(P(1:22,1)./k2);
X = A\B;
j = -log(X(1,1));
% Time Interval
a=(1790:10:2010)';
% Population
j= [3.929 5.308 7.239 9.638 12.866 17.069 23.191 31.443 39.818 50.189 62.947 76.212 92.228 106.021 122.775 132.164 150.697 179.323 203.302 226.545 248.709 281.421 308.745]';
% Parameter and intial condtions
k2= 330.0;
% Time interval
t = (1790:10:2010);
LL = 0;
for i=2:length(t)
LL(i,1) = exp((exp(-j)).*log(LL(i-1,1))+((1-exp(-j)).*log(k2)));
end
LL;
% Time Interval
% a=(1790:10:2010)';
% Population
b= [3.929 5.308 7.239 9.638 12.866 17.069 23.191 31.443 39.818 50.189 62.947 76.212 92.228 106.021 122.775 132.164 150.697 179.323 203.302 226.545 248.709 281.421 308.745]';
Data = b;
% Plot all models 1790-2010
figure
plot(Labels,Data,'k-*',Labels,LL, 'g-*')
title('Comparison of Models of US Census Data, 1790-2010')
legend('Data','Gompertz')
ylabel('Population (Millions)');
xlabel('Years');
axis([1780 2020 0 400])
hold on
The size of P is 1x23. This means one row, 23 columns.
When you try to index a value, you put rows first, then columns.
P(2:23,1) says rows 2-23 in column 1. I think you instead mean P(1,2:23) or just P(2:23).
As an observation, you should be more careful with your variables. You reuse the same variables multiple times (e.g. k2, t). You also calculate values of one variable just to immediately overwrite them (e.g. j). As an aside, you originally used j before it was definied, which then treats j as an imaginary number.
Kailin Johnsson
Kailin Johnsson 2020년 11월 11일
편집: Kailin Johnsson 2020년 11월 11일
I have changed it to P(2:23) and changed i=2.33 instead of 2. I am getting this error which seems related.
Index in position 1 is invalid. Array indices must be positive integers or logical values.
Error in GompertzEquation (line 34)
LL(i,1) = exp((exp(-j)).*log(LL(i-1,1))+((1-exp(-j)).*log(k2)));
Also, I apologize for my variables being all over the place, this is my first time using MATLAB.
i = 2.33
would not be usable as an index.
i = 2:length(t);
would be usable as an index. Howevever, in context, because the value of LL(K) depends upon the value of LL(K-1) you cannot calculate LL in vectorized form, not using those calculations, so you should loop instead of using a vector.
Also, because of how you defined j in the latest versino of your code, the equation inside your for loop now returns an array as a result instead of a single number. This will cause an assignment error since you are trying to assign the result to a single element of LL.
With all these indexing issues, I'm going to suggest you take a minute to go through Ch 5 of MATLAB Onramp.
Okay, thank you all!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2020년 11월 11일

1 개 추천

for i=1:length(t)
LL(i,1) = exp((exp(-j)).*log(LL(i-1,1))+((1-exp(-j)).*log(k)));
You are defining LL(i,1) in terms of LL(i-1,1) but at that point in the code, you have not assigned anything at all to LL yet.
Note too that your i starts from 1, and i-1 would be 0, so if LL did exist, you would be trying to index LL(0,1)
LL(1,1) = P0;
That initialization of LL(1,1) is not until a number of lines after you try to compute LL based upon exp() and so on. Furthermore, the line before that in the code
LL = zeros(23,1);
would throw away whatever had been stored in LL and replace it with zeros.
You need to initialize LL before the for i loop, and your for loop should start from 2.

카테고리

도움말 센터File Exchange에서 Mathematics에 대해 자세히 알아보기

질문:

2020년 11월 11일

댓글:

2020년 11월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by