( N1 T8 j2 V& i ?' |& Kfunction ima_pre=predict_fun(b,step)& ^: _' c* r8 U8 b* {; }- B5 j- T
% main program invokes timeseries.m and serie**pan.m1 r* X) ?8 i& O& v# Q% V, X
% input parameters:3 L1 v+ [* c7 k: s9 s' ?
% b-------the training data (vector);, _! y a( ]! k# ~
% step----number of prediction data;9 k/ J# s5 {: u' P
% output parameters: ( `# C, Z* o; d# J& `0 a% ima_pre---the prediction data(vector); ( l, I3 _- i6 Eold_b=b; 3 P$ h6 ?! {' d* u: k# Pmean_b=sum(old_b)/length(old_b);, I. r% Y, B9 i4 k- k( B
std_b=std(old_b);7 S4 x# y" R' g- ?' \, D- i
old_b=(old_b-mean_b)/std_b;& x7 n0 e# {! T
[f,x]=timeseries(old_b);, |3 x1 p" S% q; g6 ^! N) f2 t
old_f2=serie**pan(old_b,step); 0 f. i: w5 n6 z1 k% f(f<0.0001&f>-0.0001)=f(f<0.0001&f>-0.0001)+eps; 9 w7 b9 J: f2 \" @8 eR=corrcoef(f);3 O2 P% \% ^- d4 l
[eigvector eigroot]=eig(R); / F. R% ?- E) ~& geigroot=diag(eigroot);7 \$ k9 j, ?4 ^3 N
a=eigroot(end:-1:1); 7 {: l/ `; ^7 I: \" C" ]8 E# Dvector=eigvector(:,end:-1:1);! o7 C- S0 j% a$ [- J
Devote=a./sum(a);7 F3 n( G/ D5 N+ G! A
Devotem=cumsum(Devote); ) m# T" V) X0 V/ z# im=find(Devotem>=0.995); 1 f6 j9 A% w2 m) }" G: g, r0 F" ]' ym=m(1);) A0 r* e( E" i& k! i
V1=f*eigvector'; # h& X# ^; |0 g7 e* Q) iV=V1(:,1:m); 5 A8 [1 c* m, f5 M% old_b=old_b;% n4 F% l! ^' i6 Y+ u2 a; g) a6 E
old_fai=inv(V'*V)*V'*old_b; 2 P1 s$ a/ g( R. @6 beigvector=eigvector(1:m,1:m); 3 B c/ M6 p& D( l% Nfai=eigvector*old_fai;8 ~ |! Z$ W# W. m+ N C3 V$ v( a
f2=old_f2(:,1:m);3 U& r" m2 [+ B3 E/ U0 u9 ]- q" P
predictvalue=f2*fai; 3 s5 T' U5 {' ~% [; D9 F, h3 _ima_pre=std_b*predictvalue+mean_b;' K( X% y: j2 R* U
- c4 g) R- T3 o! O
1.子函数: timeseries.m , W7 r' M$ q: p" m6 L
% timeseries program% $ ~! N4 v( ]2 _6 o" d; Y% this program is used to generate mean value matrix f; $ v! G, n4 @ V7 F/ Mfunction [f,x]=timeseries(data) ! {9 C! {! r& m; R: g( L b! G3 e- e
% data--------the input sequence (vector); g; Y7 v9 x+ {+ T; n% f------mean value matrix f; W% [* a9 b% n* o# J9 An=length(data);$ b8 T9 n# s8 X* I: _
for L=1:n/23 B! V' z$ L0 ^6 ]
nL=floor(n/L);7 w3 ?2 `' x0 e
for i=1:L " E, I3 i' P( I* d sum=0; $ [1 X9 H+ c b; H for j=1:nL 6 y3 A/ l- w" t: h sum=sum+data(i+(j-1)*L);) h& g7 m& K( a* X$ p$ z& U
end 5 C# v8 m1 [- I4 ~; f6 E0 q x{L,i}=sum/nL;$ L" Y( `) ~1 ~" P" n& Z: A
end 4 Z+ y; `' R5 a2 H. j) F) m, kend / {+ W: D/ [; B$ V" PL=n/2;/ ~* q$ n! b: \3 d8 |1 i+ y
f=zeros(n,L);9 f0 B1 ^" U9 W1 M* _4 N( e
for i=1:L1 ?* k* @) _# I
rep=floor(n/i); / [6 X- g, n7 f res=mod(n,i);, a7 G1 k1 E$ t: l! T) X
b=[x{i,1:i}];b=b'; ) _' j" d6 U' l+ S/ x# ` f(1:rep*i,i)=repmat(b,rep,1);: u, ~+ t2 H: `5 \( f0 {' T
if res~=0' Q/ H( A- e% e# z
c=rep*i+1:n; . L* }: c: h7 [8 A f(rep*i+1:end,i)=b(1:length(c)); 4 i( h; z( `7 H: {+ D, N1 z( P: S end# p. g" ]& N' F- S- e
end9 j% U8 X9 l! v8 h
/ n5 v* h4 Z* j* f+ t
% serie**pan.m+ M9 g7 u6 c8 U" }4 F! I7 \
% the program is used to generate the prediction matrix f; ( Z% X) }8 z w
function f=serie**pan(data,step);5 Z% K( e* `6 w, t
%data---- the input sequence (vector)4 m, ^: n8 W; s% C! j% s: T7 E$ H
% setp---- the prediction number;9 Q6 Y6 h* Q, @2 p3 h
n=length(data);7 G- x5 }! K7 _. L+ Z, P2 u$ E
for L=1:n/2- h& x' K+ K# z; `+ Z* U: N; C
nL=floor(n/L);% c7 T7 `8 ?% u* C
for i=1:L * U2 N% ~9 K2 O/ D: I sum=0; , D4 A: y* R& g: B for j=1:nL+ }7 p8 w0 _) ~
sum=sum+data(i+(j-1)*L); + f7 N' ^8 s- q$ q$ @9 D end / R$ V% A. S- I x{L,i}=sum/nL;" E! C0 Z+ E" ^) p2 i7 `) @4 b9 i
end ) p: c8 b' C8 uend' }3 s+ X' T! v9 {/ S
L=n/2; : ]% c1 h; M; o, \7 Qf=zeros(n+step,L);" C* D/ h0 }* z+ f" |
for i=1:L 0 @+ [! K, S3 Z- E rep=floor((n+step)/i);, e, Q0 N' b E$ i2 E! R
res=mod(n+step,i); 6 T8 ]) P; H, `# d5 U' a b=[x{i,1:i}];b=b'; & F: I- g" o* W* A. I f(1:rep*i,i)=repmat(b,rep,1); 3 X' a) f" x: A8 P$ i if res~=00 `" p: Q w) t/ u( P3 @
c=rep*i+1:n+step; - ]0 T7 i X. F1 t* U% r f(rep*i+1:end,i)=b(1:length(c));- x: ^/ a+ O( g* @6 G
end & _. `$ [+ H& H. ]/ k/ Iend4 v9 [/ b, Q3 k( u
* v: R/ s: o: K# {二 最短路Dijkstra算法 ( G) T( ?2 f$ b( G% dijkstra algorithm code program%4 J* X$ [' Q+ ? ?+ b6 m7 B- C
% the shortest path length algorithm+ d. \6 E/ _% M. h
function [path,short_distance]=ShortPath_Dijkstra(Input_weight,start,endpoint)9 B p6 o0 o- s* u- _: l( g, I
% Input parameters:1 L+ L/ I8 W% R% X
% Input_weight-------the input node weight! + t. i+ }9 a5 F- ^! z* x1 Z9 X% start--------the start node number;+ t' h1 s2 l% G @1 ^7 t! R
% endpoint------the end node number;6 B! B1 V' K/ l, z6 s: o1 z- c& H
% Output parameters:# d0 `( y- G7 E e6 ?6 s
% path-----the shortest lenght path from the start node to end node; 8 M* @* _$ x" {$ z1 v% short_distance------the distance of the shortest lenght path from the# z) N; D- X6 B8 h) e K V
% start node to end node.' J' D A* @. k" @
[row,col]=size(Input_weight);* a; s: o3 F1 i, ~. }8 p2 Y
; e5 D3 e$ X* k! U2 b. o- O%input detection9 N& y$ u/ U X: O
if row~=col 6 @1 f' p: { H" H8 _1 m! N3 n' f error('input matrix is not a square matrix,input error ' ); 7 j; `9 M g. I V# qend 0 y1 u" @3 Y% j/ p nif endpoint>row! N& M- D+ Q) B8 S6 |8 y
error('input parameter endpoint exceed the maximal point number');* I# n9 S2 o& i8 i( d
end8 }# Y" t; i1 |" D
0 q* k7 U0 b% u, D" h6 j%initialization 7 J' Y: h K+ ss_path=[start];8 P5 y& R( k$ J. M: y+ W
distance=inf*ones(1,row);distance(start)=0;1 e0 n4 v5 {0 m+ {! ~0 e( x5 n
flag(start)=start;temp=start;7 w9 ^ U" H0 G( y# z
7 ~% m1 a8 G9 L: r, e! X* Nwhile length(s_path)<row+ ?1 S- y% o1 ~& E) A
pos=find(Input_weight(temp, : )~=inf);6 y1 U7 k# L; G8 M! s0 P
for i=1:length(pos) / W. c$ p. h0 s- t0 e+ ]4 f if (length(find(s_path==pos(i)))==0)&# R# {1 S4 @' R4 l- g6 |
(distance(pos(i))>(distance(temp)+Input_weight(temp,pos(i)))) ' o, f) k ^/ g9 A6 G( P6 r distance(pos(i))=distance(temp)+Input_weight(temp,pos(i));% S! U; a! @, A: g
flag(pos(i))=temp; " \$ J/ t* i$ J# o! @5 z end $ D6 k3 C9 S" {& d- n end- I% \1 c* k0 B# L9 q
k=inf;# W1 h6 n9 T9 ~9 C* K
for i=1:row: ]) X( ~! J, Y0 F
if (length(find(s_path==i))==0)&(k>distance(i))8 z5 m6 y) B8 o4 ?; B. q# h' A
k=distance(i);- E4 ]7 o5 v( e l# @( o
temp_2=i; + w8 I5 a) K$ Q3 j+ O% l end & ]# h% x7 |' Y' B end 5 P! I3 W: ~, u, F s_path=[s_path,temp_2];, b( N# E4 E( e. U- K+ I8 X
temp=temp_2;6 e$ M+ u8 |# d9 r" q
end( g0 J4 ~' T# g# ` M
6 i6 \2 U5 j, F: l3 x$ Y/ `%output the result: {+ T8 A1 G7 {
path(1)=endpoint; 1 x H# Y+ F$ u! H$ x, ri=1;/ S n0 i' I# R- T( V x% L L( _* M
while path(i)~=start 6 J% W" m; _& N1 q4 o- H path(i+1)=flag(path(i)); : j- L* s7 A, [* K- g5 c i=i+1; 4 p! E# X, \/ Z; ^end8 Z) E, C- F. e5 X$ h
path(i)=start; ! R' b7 m8 l0 Ppath=path(end:-1:1); & _8 y! X8 b- D$ ^" c6 Wshort_distance=distance(endpoint); + ^/ d/ s* o9 ^6 h) G: E三 绘制差分方程的映射分叉图7 ^, U# S) V4 o- u. @
) W3 z8 C" R: ~
function fork1(a); + N* V9 M0 g9 E7 S 1 z7 X& D4 g2 n0 E: L7 Q% 绘制x_(n+1)=1-a*x^2_n映射的分叉图 ; c- @0 h T+ B* d7 J: e3 K, p% Example: * B) v3 |& H: H) ?; S% {# a0 a% fork1([0,2]); % R m c7 ?* Y- i% X( J6 w
N=300; % 取样点数 + X3 ]% o7 B! ?/ f. b3 q
A=linspace(a(1),a(2),N); % R1 C) E0 S5 k2 `' q8 I
starx=0.9; . p' C. \5 X1 Q* ^/ G0 x
Z=[];. A* k2 A7 }$ m+ K- f, c
h=waitbar(0,'please wait');m=1;; i$ p( Q$ m2 t3 R9 Z
for ap=A; % i1 Q6 w3 X7 u' \ x=starx; & E4 q9 n) O# I- O9 _ for k=1:50; $ x; i5 \2 v, h0 Q8 L. i% S x=1-ap*x^2; ; F- t( ]* N( i' a, q
end * T4 i4 `1 m/ ]. w7 k for k=1:201; 1 Y7 @. o8 M! S/ n- d" O3 w. w
x=1-ap*x^2; ( ~' j+ s. l# n1 R- t* j0 n Z=[Z,ap-x*i]; . |, [9 L1 m& e& ]' j
end 3 B: p! r& L l
waitbar(m/N,h,['completed ',num2str(round(100*m/N)),'%'],h); - _4 r1 N9 z8 o, u. H' P m=m+1;0 M3 i5 ? C. [7 {+ G
end , ^/ ]2 v& p! x1 y% G3 s" ~
delete(h); 0 d: I5 X. g& u& K% b* ?, A- Uplot(Z,'.','markersize',2) 6 D3 p! u8 w2 ^2 [9 c
xlim(a); 8 f; g" l9 s# @; e+ c& x4 ~; z% {; C$ Y
四 最短路算法------floyd算法7 ]7 t: {4 j2 w( Q8 v% {
function ShortPath_floyd(w,start,terminal) - R. c" U T7 g) H' [6 ?
%w----adjoin matrix, w=[0 50 inf inf inf;inf 0 inf inf 80;' X3 A. s# z2 v" d
%inf 30 0 20 inf;inf inf inf 0 70;65 inf 100 inf 0];5 g! e. \( J) n0 d9 s h1 @9 a
%start-----the start node;+ i7 {. S6 R: i
%terminal--------the end node; : X. a/ J$ H9 _' [+ C
n=size(w,1);9 }. c2 U/ q8 x" `
[D,path]=floyd1(w);%调用floyd算法程序* P$ \2 m* d1 j2 z
# Z8 o `, s" r0 z1 N5 Q: S
%找出任意两点之间的最短路径,并输出; D$ d8 \7 w% `/ J# Y, i* e
for i=1:n2 ], Q2 B6 M7 h! d
for j=1:n: ]0 E- h z( p" ~2 ?
Min_path(i,j).distance=D(i,j);8 x, ?, C) U5 L
%将i到j的最短路程赋值 Min_path(i,j).distance . Q; `8 b* U8 T %将i到j所经路径赋给Min_path(i,j).path: X2 r% ?; P' n" ]8 s9 A
Min_path(i,j).path(1)=i;$ \* B! v( I( l" Q9 D, _
k=1; 4 a) d. I$ I9 m, \. s# s- M A) y while Min_path(i,j).path(k)~=j v8 J. V- p. N# Y/ E# F4 ]- c k=k+1;) l0 R) I0 l$ a0 j
Min_path(i,j).path(k)=path(Min_path(i,j).path(k-1),j); 8 v) x2 F, V: ~0 r; | end # u; j7 h8 I6 `! s$ G end5 i, _ H7 }6 X f& a+ h! S
end e6 X2 b. N; W6 L
s=sprintf('任意两点之间的最短路径如下:'); s5 C3 ]; S9 [9 H& a! }" B% Ydisp(s);) N9 S- e- Y6 _# g! g/ ^
for i=1:n 2 P* h" w1 }5 o" S for j=1:n 7 H) w" }2 B. X0 b s=sprintf('从%d到%d的最短路径长度为:%d\n所经路径为:'...5 j! {7 W: o$ J' C# N& _
,i,j,Min_path(i,j).distance); & i& z5 M7 X! W" \3 F! O2 a9 q disp(s); * X4 `7 B9 G" ~7 `, t disp(Min_path(i,j).path);8 v8 R& L+ ^( _- V4 I; m
end , G. X' u& I, ]+ S* M% Bend ; g# S& j( m+ c* P& k' o. c" |7 b/ D* y8 F, _/ Z
%找出在指定从start点到terminal点的最短路径,并输出; X' U5 R S+ n
str1=sprintf('从%d到%d的最短路径长度为:%d\n所经路径为:',... 5 S5 m7 A0 `# m1 H start,terminal,Min_path(start,terminal).distance); % l) G: P1 ?+ ~* O* C7 |2 S. udisp(str1); 8 s5 E, T. }6 i7 j0 K0 x2 Wdisp(Min_path(start,terminal).path);8 g6 w5 b3 o5 T
7 ^/ t6 i, b n! H2 a3 q9 s" U
%Foldy's Algorithm 算法程序+ j+ A9 h& D g* |# e+ H
function [D,path]=floyd1(a)8 }0 v( @0 w+ ?1 p
n=size(a,1);6 `9 S- z4 M8 D/ V) z
D=a;path=zeros(n,n);%设置D和path的初值 . F( |# w {1 ^2 Ffor i=1:n/ Q2 o2 @. N- {2 [
for j=1:n0 z: c4 U' Q3 N0 }
if D(i,j)~=inf 3 X+ M! m7 O4 o) j$ N path(i,j)=j;%j是i的后点 1 v: v* T- k2 ?2 S$ y end! l- b. G- D" v* l* }
end 3 U" ]: t8 e" H. O0 l7 N- kend9 v% ~8 H; u8 M1 @4 e
%做n次迭代,每次迭代都更新D(i,j)和path(i,j) " [8 N. R" w2 U' \, Zfor k=1:n - u6 ?, ^7 E) c, N for i=1:n) M5 _6 A( I: z c0 ?; P; f
for j=1:n6 M* I* B* x1 Q, j, ]
if D(i,k)+D(k,j)<D(i,j); ?+ T2 f( h3 k5 d% d! ]. r; u) P
D(i,j)=D(i,k)+D(k,j);%修改长度* y9 }+ g1 W; E9 l2 \
path(i,j)=path(i,k);%修改路径 & ]5 ~6 }5 W) z& Y5 _4 d2 V end% l: O4 v- g/ L+ M" K3 ~
end . C. \& B5 R& K+ \) P+ C9 W end |8 H. ?; R' Q
end; b- f; c) K- Z6 L9 O% w