Unrecognized Variable despite being defined
이전 댓글 표시
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');

채택된 답변
추가 답변 (1개)
Walter Roberson
2020년 11월 11일
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에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!