X1end(1)=x1(1); for i=2:(n+5) X1end(i)=(X1end(1)-alph(2)/alph(1))*exp(-alph(1)*(i-1)) +alph(2)/alph(1); end
时间: 2023-08-22 21:10:24 浏览: 81
这段代码是用来计算指数衰减的序列。首先,将x1的第一个元素赋值给X1end的第一个元素。然后,从i=2开始循环,每次循环通过指数衰减公式计算X1end(i)的值:(X1end(1)-alph(2)/alph(1))*exp(-alph(1)*(i-1)) +alph(2)/alph(1)。其中,alph(1)和alph(2)是之前计算的线性回归模型的系数。通过这个循环,可以得到一个衰减序列X1end。
相关问题
DD=xlsread('residual.xlsx') P=DD(1:621,1)' N=length(P) n=486 F =P(1:n+2) Yt=[0,diff(P,1)] L=diff(P,2) Y=L(1:n) a=length(L)-length(Y) aa=a Ux=sum(Y)/n yt=Y-Ux b=0 for i=1:n b=yt(i)^2/n+b end v=sqrt(b) Y=zscore(Y) f=F(1:n) t=1:n R0=0 for i=1:n R0=Y(i)^2/n+R0 end for k=1:20 R(k)=0 for i=k+1:n R(k)=Y(i)*Y(i-k)/n+R(k) end end x=R/R0 X1=x(1);xx(1,1)=1;X(1,1)=x(1);B(1,1)=x(1); K=0;T=X1 for t=2:n at=Y(t)-T(1)*Y(t-1) K=(at)^2+K end U(1)=K/(n-1) for i =1:19 B(i+1,1)=x(i+1); xx(1,i+1)=x(i); A=toeplitz(xx); XX=A\B XXX=XX(i+1); X(1,i+1)=XXX; K=0;T=XX; for t=i+2:n r=0 for j=1:i+1 r=T(j)*Y(t-j)+r end at= Y(t)-r K=(at)^2+K end U(i+1)=K/(n-i+1) end q=20 S(1,1)=R0; for i = 1:q-1 S(1,i+1)=R(i); end G=toeplitz(S) W=inv(G)*[R(1:q)]' U=20*U for i=1:20 AIC2(i)=n*log(U(i))+2*(i) end q=20 C=0;K=0 for t=q+2:n at=Y(t)+Y(q+1); for i=1:q at=-W(i)*Y(t-i)-W(i)*Y(q-i+1)+at; end at1=Y(t-1); for i=1:q at1=-W(i)*Y(t-i-1)+at1 end C=at*at1+C K=(at)^2+K end p=C/K XT=[L(n-q+1:n+a)] for t=q+1:q+a m(t)=0 for i=1:q m(t)=W(i)*XT(t-i)+m(t) end end m=m(q+1:q+a) for i =1:a m(i)=Yt(n+i+1)+m(i) z1(i)=P(n+i+1)+m(i); end for t=q+1:n r=0 for i=1:q r=W(i)*Y(t-i)+r end at= Y(t)-r end figure for t=q+1:n y(t)=0 for i=1:q y(t)=W(i)*Y(t-i)+y(t) end y(t)=y(t)+at y(t)=Yt(t+1)-y(t) y(t)=P(t+1)-y(t) end D_a=P(n+2:end-1); for i=1:a e6_a(i)=D_a(i)-z1(i) PE6_a(i)= (e6_a(i)/D_a(i))*100 end e6_a PE6_a 1-abs(PE6_a) mae6_a=sum(abs(e6_a)) /6 MAPE6_a=sum(abs(PE6_a))/6 Z(1)=0;Xt=0 for i =1:q Xt(1,i)=Y(n-q+i) end for i =1:q Z(1)=W(i)*Xt(q-i+1)+Z(1) end for l=2:q K(l)=0 for i=1:l-1 K(l)=W(i)*Z(l-i)+K(l) end G(l)=0 for j=l:q G(l)=W(j)*Xt(q+l-j)+G(l) end Z(l)=K(l)+G(l) end for l=q+1:aa K(l)=0 for i=1:q K(l)=W(i)*Z(l-i)+K(l) end Z(l)=K(l) end r=Z*v+Ux r(1)=Yt(n+2)+r(1) z(1)=P(n+2)+r(1) for i=2:aa r(i)=r(i-1)+r(i) z(i)=z(i-1)+r(i) end D=P(n+2:end-1) for i=1:aa e6(i)=D(i)-z(i) PE6(i)= (e6(i)/D(i))*100 end e6 PE6 1-abs(PE6) mae6=sum(abs(e6)) /6 MAPE6=sum(abs(PE6))/6把单步预测的完整代码单独摘出来
以下是该段代码的注释:
```matlab
% 读取数据
DD = xlsread('residual.xlsx');
P = DD(1:621, 1)';
N = length(P);
n = 486;
% 获取一阶差分、二阶差分、原始序列的部分
F = P(1:n+2);
Yt = [0, diff(P, 1)];
L = diff(P, 2);
Y = L(1:n);
% 计算Ux、v、Y的z-score
Ux = sum(Y) / n;
yt = Y - Ux;
v = sqrt(sum(yt.^2) / n);
Y = zscore(Y);
% 计算R、X、U、AIC2、C、K、m、y、e6、PE6、mae6、MAPE6等
R0 = sum(Y.^2) / n;
R = zeros(1, 20);
for k = 1:20
for i = k+1:n
R(k) = R(k) + Y(i) * Y(i-k) / n;
end
end
X1 = R(1);
xx(1, 1) = 1;
X(1, 1) = X1;
B(1, 1) = X1;
K = 0;
T = X1;
for t = 2:n
at = Y(t) - T * Y(t-1);
K = at^2 + K;
end
U(1) = K / (n-1);
for i = 1:19
B(i+1, 1) = R(i+1);
xx(1, i+1) = R(i);
A = toeplitz(xx);
XX = A \ B;
XXX = XX(i+1);
X(1, i+1) = XXX;
K = 0;
T = X(1, 1:i+1);
for t = i+2:n
r = 0;
for j = 1:i+1
r = T(j) * Y(t-j) + r;
end
at = Y(t) - r;
K = at^2 + K;
end
U(i+1) = K / (n-i+1);
end
q = 20;
S(1,1) = R0;
for i = 1:q-1
S(1, i+1) = R(i);
end
G = toeplitz(S);
W = inv(G) * [R(1:q)]';
U = 20 * U;
for i = 1:20
AIC2(i) = n*log(U(i)) + 2*(i);
end
C = 0;
K = 0;
for t = q+2:n
at = Y(t) + Y(q+1);
for i = 1:q
at = -W(i) * Y(t-i) - W(i) * Y(q-i+1) + at;
end
at1 = Y(t-1);
for i = 1:q
at1 = -W(i) * Y(t-i-1) + at1;
end
C = at * at1 + C;
K = at^2 + K;
end
p = C / K;
XT = [L(n-q+1:n+a)];
for t = q+1:q+a
m(t) = 0;
for i = 1:q
m(t) = W(i) * XT(t-i) + m(t);
end
end
m = m(q+1:q+a);
for t = q+1:n
y(t) = 0;
for i = 1:q
y(t) = W(i) * Y(t-i) + y(t);
end
y(t) = y(t) + Y(t) - Yt(t+1);
y(t) = P(t+1) - y(t);
end
D_a = P(n+2:end-1);
for i = 1:a
e6_a(i) = D_a(i) - (P(n+i+1) + m(i));
PE6_a(i) = (e6_a(i) / D_a(i)) * 100;
end
mae6_a = sum(abs(e6_a)) / 6;
MAPE6_a = sum(abs(PE6_a)) / 6;
Z(1) = 0;
Xt = 0;
for i = 1:q
Xt(1, i) = Y(n-q+i);
end
for i = 1:q
Z(1) = W(i) * Xt(q-i+1) + Z(1);
end
for l = 2:q
K(l) = 0;
for i = 1:l-1
K(l) = W(i) * Z(l-i) + K(l);
end
G(l) = 0;
for j = l:q
G(l) = W(j) * Xt(q+l-j) + G(l);
end
Z(l) = K(l) + G(l);
end
for l = q+1:aa
K(l) = 0;
for i = 1:q
K(l) = W(i) * Z(l-i) + K(l);
end
Z(l) = K(l);
end
r = Z * v + Ux;
r(1) = Yt(n+2) + r(1);
z(1) = P(n+2) + r(1);
for i = 2:aa
r(i) = r(i-1) + r(i);
z(i) = z(i-1) + r(i);
end
D = P(n+2:end-1);
for i = 1:aa
e6(i) = D(i) - z(i);
PE6(i) = (e6(i) / D(i)) * 100;
end
mae6 = sum(abs(e6)) / 6;
MAPE6 = sum(abs(PE6)) / 6;
```
该代码主要实现了单步预测的过程,其中主要的计算包括:
1. 计算一阶差分、二阶差分和原始序列的部分;
2. 计算Ux、v、Y的z-score;
3. 计算R、X、U、AIC2、C、K、m、y、e6、PE6、mae6、MAPE6等;
4. 实现单步预测过程,计算得到预测值z1和z;
5. 计算e6、PE6、mae6和MAPE6等评价指标。
这两个网络的流量矩阵怎么计算,用MATLAB实现:m0=2 m=2 N=20 x1=100rand(1,m0); y1=100rand(1,m0); x2=100rand(1,m0); y2=100rand(1,m0); for i=1:N z11(i)=10 end z1=z11' for i=1:N z22(i)=90 end z2=z22' %for i=1:N %z1(i)=10 %end %for i=1:N %z2(i)=90 %end for i=1:m0 for j=i+1:m0 p1=rand(1,1); p2=rand(1,1); if p1>0.5 a1(i,j)=1; a1(j,i)=0; end if p2>0.5 a2(i,j)=1; a2(j,i)=0; end end end for k=m0+1:N M=size(a1,1);p=zeros(1,M); M1=size(a2,1);p1=zeros(1,M1); x0=100rand(1,1);y0=100rand(1,1); x1(k)=x0;y1(k)=y0; x2(k)=x0;y2(k)=y0; if length(find(a1==1))==0 p(:)=1/M; else for i=1:M p(i)=length(a1(i,:)==1)/length(find(a1==1)); end if length(find(a2==1))==0 p1(:)=1/M1; else for i=1:M1 p1(i)=length(a2(i,:)==1)/length(find(a2==1)); end end end pp=cumsum(p); pp1=cumsum(p1); for i=1:m random_data=rand(1,1); random_data1=rand(1,1); aa=find(pp>=random_data);jj=aa(1); aa1=find(pp1>=random_data1);jj1=aa1(1); a1(k,jj)=1; a1(jj,k)=1; a2(k,jj1)=1; a2(jj1,k)=1; end end
这段代码的作用是生成两个网络的流量矩阵a1和a2,其中m0是已经存在的节点数,N是总的节点数,x1、y1、x2、y2是节点的坐标,z1和z2是节点的流量需求。下面是计算流量矩阵的代码:
```matlab
m0 = 2;
m = 2;
N = 20;
x1 = 100*rand(1,m0);
y1 = 100*rand(1,m0);
x2 = 100*rand(1,m0);
y2 = 100*rand(1,m0);
% 初始化节点流量需求
for i = 1:N
z11(i) = 10;
end
z1 = z11';
for i = 1:N
z22(i) = 90;
end
z2 = z22';
% 初始化连接矩阵
a1 = zeros(N,N);
a2 = zeros(N,N);
% 随机连接已有节点
for i = 1:m0
for j = i+1:m0
p1 = rand(1,1);
p2 = rand(1,1);
if p1 > 0.5
a1(i,j) = 1;
a1(j,i) = 0;
end
if p2 > 0.5
a2(i,j) = 1;
a2(j,i) = 0;
end
end
end
% 生成新节点并进行连接
for k = m0+1:N
% 计算节点k与已有节点的连接概率
M = size(a1,1);
p = zeros(1,M);
M1 = size(a2,1);
p1 = zeros(1,M1);
x0 = 100*rand(1,1);
y0 = 100*rand(1,1);
x1(k) = x0;
y1(k) = y0;
x2(k) = x0;
y2(k) = y0;
if length(find(a1==1)) == 0
p(:) = 1/M;
else
for i = 1:M
p(i) = length(a1(i,:)==1)/length(find(a1==1));
end
end
if length(find(a2==1)) == 0
p1(:) = 1/M1;
else
for i = 1:M1
p1(i) = length(a2(i,:)==1)/length(find(a2==1));
end
end
pp = cumsum(p);
pp1 = cumsum(p1);
for i = 1:m
random_data = rand(1,1);
random_data1 = rand(1,1);
aa = find(pp>=random_data);
jj = aa(1);
aa1 = find(pp1>=random_data1);
jj1 = aa1(1);
a1(k,jj) = 1;
a1(jj,k) = 1;
a2(k,jj1) = 1;
a2(jj1,k) = 1;
end
end
```
其中,`a1`和`a2`即为两个网络的流量矩阵。
阅读全文