clear;
clc;
syms a b;
c=[a b]";
A=[174 179 183 189 207 234 220.5 256 270 285];
B=cumsum(A);
n=length(A);
for i=1:(n-1)
C(i)=(B(i)+B(i+1))/2;
end
E=[-C;ones(1,n-1)];
D=A;
D(1)=[];
D=D";
c=inv(E*E")*E*D;
c=c";
a=c(1);
b=c(2);
F=[];
F(1)=A(1);
for i=2:(n+25)
F(i)=(A(1)-b/a)/exp(a*(i-1))+b/a;
G=[];
G(1)=A(1);
for i=2:(n+10)
G(i)=F(i)-F(i-1);
t1=1995:2004;
t2=1995:2014;
plot(t1,A,"O",t2,G)
可以參照這個程式,自己將資料和迴圈的次數n改了,就能用了
clear;
clc;
syms a b;
c=[a b]";
A=[174 179 183 189 207 234 220.5 256 270 285];
B=cumsum(A);
n=length(A);
for i=1:(n-1)
C(i)=(B(i)+B(i+1))/2;
end
E=[-C;ones(1,n-1)];
D=A;
D(1)=[];
D=D";
c=inv(E*E")*E*D;
c=c";
a=c(1);
b=c(2);
F=[];
F(1)=A(1);
for i=2:(n+25)
F(i)=(A(1)-b/a)/exp(a*(i-1))+b/a;
end
G=[];
G(1)=A(1);
for i=2:(n+10)
G(i)=F(i)-F(i-1);
end
t1=1995:2004;
t2=1995:2014;
plot(t1,A,"O",t2,G)
可以參照這個程式,自己將資料和迴圈的次數n改了,就能用了