一基于均值生成函数时间序列预测算法程序 1. predict_fun.m为主程序;
2. timeseries.m和serie**pan.m为调用的子程序
function ima_pre=predict_fun(b,step)
% main program invokes timeseries.m and serie**pan.m% u' W ]7 e& X j % input parameters:
% b-------the training data (vector);% G( R) ]: M9 h% I8 r: w$ Z) [3 X % step----number of prediction data; % output parameters:! o3 h8 U& I\
% ima_pre---the prediction data(vector);8 G5 e$ K\ old_b=b;- _# ]6 K: L6 i3 M1 m$ ?
mean_b=sum(old_b)/length(old_b); std_b=std(old_b);
old_b=(old_b-mean_b)/std_b; [f,x]=timeseries(old_b);
old_f2=serie**pan(old_b,step);1 N2 t. {5 h5 q8 s: A: G
% f(f<0.0001&f>-0.0001)=f(f<0.0001&f>-0.0001)+eps;1 f* w9 o5 C: C9 T R=corrcoef(f);
[eigvectoreigroot]=eig(R); eigroot=diag(eigroot);
a=eigroot(end:-1:1);
vector=eigvector(:,end:-1:1);$ j6 S$ `\ Devote=a./sum(a);4 Z, g U* h h Devotem=cumsum(Devote); m=find(Devotem>=0.995); m=m(1);6 w( Q8 F/ l- o5 ~# {: Z1 g V1=f*eigvector';\ V=V1(:,1:m); % old_b=old_b;
old_fai=inv(V'*V)*V'*old_b; eigvector=eigvector(1:m,1:m);
fai=eigvector*old_fai;( W9 F4 e! a0 c- f& g4 ^2 D f2=old_f2(:,1:m);
predictvalue=f2*fai;
ima_pre=std_b*predictvalue+mean_b;) }\
1.子函数: timeseries.m1 H3 ]: r: k+ m. t) [, e P % timeseries program%
% this program is used to generate mean value matrix f;1 E2 f2 Q+ p( @ function [f,x]=timeseries(data)
% data--------the input sequence (vector);5 |& L7 c4 t$ a9 C4 b % f------mean value matrix f; n=length(data);
for L=1:n/2 nL=floor(n/L); for i=1
sum=0;
for j=1:nL8 N& a3 j! K# O% w4 ^6 k sum=sum+data(i+(j-1)*L); end3 N) h\
x{L,i}=sum/nL;1 o) U G- }0 c2 ]: S7 o; t end- z\ end8 G- A) Q) R0 ?: d. C+ Z4 Q. e L=n/2;( G) g' T5 E- }% S. W! p f=zeros(n,L); for i=1
rep=floor(n/i);/ ]% e$ h [0 [8 E$ ?\ res=mod(n,i);* B& e3 y( t5 B; a+ _% e b=[x{i,1:i}];b=b';8 `% ?) V, b& s( T) e. G' B f(1:rep*i,i)=repmat(b,rep,1); if res~=0
c=rep*i+1:n;% S9 o- m& h% n2 U
f(rep*i+1:end,i)=b(1:length(c));$ w+ k5 d. \\9 ~* m E end end
% serie**pan.m4 y\
% the program is used to generate the prediction matrix f; 8 `$ {0 c; q( W8 | function f=serie**pan(data,step);# Q8 L7 l3 x$ }# b úta---- the input sequence (vector)
% setp---- the prediction number;: ?& C! Z) S1 b n=length(data); for L=1:n/2 nL=floor(n/L); for i=1
; R9 a/ d8 Y! M' B% ]& ^8 Z! j0 O
sum=0;
for j=1:nL# y; @0 u! Y7 n8 W+ L sum=sum+data(i+(j-1)*L); end
x{L,i}=sum/nL; end: O' H6 v6 @' @
end9 P; A; e2 U! {1 a0 z8 ^, x L=n/2;* J) @ H# {' ~3 U0 T
f=zeros(n+step,L);/ W5 b# k9 E& o- O! @: N' D2 j, l
for i=1$ |4 x, A; I( w, C- Y
rep=floor((n+step)/i);
res=mod(n+step,i);3 W% F% }' t& x/ { b=[x{i,1:i}];b=b';
f(1:rep*i,i)=repmat(b,rep,1);9 `$ S3 ?9 L. D& W: O' W if res~=0
c=rep*i+1:n+step;
f(rep*i+1:end,i)=b(1:length(c)); end
end+ D! p# @& ]8 K4 z
二最短路Dijkstra算法
% dijkstra algorithm code program%
% the shortest path length algorithm
function [path,short_distance]=ShortPath_Dijkstra(Input_weight,start,endpoint) % Input parameters:. Q# V, A$ T. B) a3 {$ v& x, p( o % Input_weight-------the input node weight!
% start--------the start node number;. A9 C4 |& r6 W4 k7 q! ` % endpoint------the end node number; % Output parameters:/ o4 G, J) A\
% path-----the shortest lenght path from the start node to end node;
% short_distance------the distance of the shortest lenght path from the6 Y# D6 A1 r2 E % start node to end node.
[row,col]=size(Input_weight);7 h2 \\4 Y t8 M \\
%input detection
if row~=col/ f4 C8 s9 j$ q. r\
error('input matrix is not a square matrix,input error ' ); end' {! f* r3 d8 Y
if endpoint>row( c. i, \\- a- o# e0 F
error('input parameter endpoint exceed the maximal point number'); end6 A( y2 d m7 h* m7 W u 4 z' f6 n/ |% p- u/ \\ %initialization s_path=[start];
distance=inf*ones(1,row);distance(start)=0; flag(start)=start;temp=start;8 Q& `5 D% t9 b5 {- g( ` 9 r5 @' j) b\
while length(s_path) pos=find(Input_weight(temp, : )~=inf);1 L1 U% u; P/ T) w* q! i- p for i=1:length(pos)) L r6 Q0 x' N if (length(find(s_path==pos(i)))==0)&( ~, B. H* u5 U3 d0 r( g, H (distance(pos(i))>(distance(temp)+Input_weight(temp,pos(i)))) distance(pos(i))=distance(temp)+Input_weight(temp,pos(i));5 y2 ?0 j9 c' L+ t% k. F flag(pos(i))=temp;. B9 R `8 ]. r! O8 k end end, V2 I/ F% K/ E- H k=inf; for i=1:row+ q0 H9 g Y6 }; L r0 F( A8 j if (length(find(s_path==i))==0)&(k>distance(i))2 B+ z% Q3 ] A k=distance(i);; X+ G1 {* }0 X% y temp_2=i;2 w7 v! g0 P9 x k+ k! j end+ s k\ end s_path=[s_path,temp_2];1 t: }+ ]; A6 ?3 ^ temp=temp_2; end ; m a) p\ %output the result2 ~$ {6 L0 ~2 A\ path(1)=endpoint; i=1; while path(i)~=start path(i+1)=flag(path(i));, x2 y: ]0 b4 N6 d8 W* s2 \\ i=i+1;9 y: d. O/ {( |, W. M1 X* H+ q/ _, | end path(i)=start; path=path(end:-1:1);+ z* e5 W0 ?& h short_distance=distance(endpoint); 三绘制差分方程的映射分叉图& E% b Z; I& C6 A5 g$ { function fork1(a); 9 Y9 b* w M- ]6 r- D7 Q % 绘制x_(n+1)=1-a*x^2_n映射的分叉图, e0 _. @& G9 l1 }- V# H % Example: / l4 c3 a, N. G, ]: \\8 d4 p % fork1([0,2]); N=300; % 取样点数 A=linspace(a(1),a(2),N); + C8 z3 l: r# X4 g6 ?- f9 U starx=0.9; 5 ^; Z1 n0 ]: O8 n! c Z=[];: h0 Z# o& H+ u+ X\ h=waitbar(0,'please wait');m=1; for ap=A; x=starx; * r: K) J# l\ for k=1:50; : H/ Q/ {1 R. N0 o! k0 S2 b2 ? x=1-ap*x^2; end 3 k, `2 m2 i2 M for k=1:201; & {- N6 Q& I5 T4 [9 ?$ E, X# F x=1-ap*x^2; Z=[Z,ap-x*i]; end ' l3 [% _0 n, g4 A5 S waitbar(m/N,h,['completed ',num2str(round(100*m/N)),'%'],h);2 \\1 F8 {5 b9 K/ W m=m+1; end delete(h);( `6 w/ Y) N\ plot(Z,'.','markersize',2) xlim(a);! S& n% `# v4 F) g ) K K$ h8 n$ N3 ^+ _+ X 四最短路算法------floyd算法 function ShortPath_floyd(w,start,terminal) & c( ]% q) @6 z\ %w----adjoin matrix, w=[0 50 infinfinf;inf 0 infinf 80;# x* ^6 F$ p8 T& U* {, F %inf 30 0 20 inf;infinfinf 0 70;65 inf 100 inf 0];; _: K. V) @, \\/ u8 p( G %start-----the start node; %terminal--------the end node; . t9 G. n L- k8 f n=size(w,1); [D,path]=floyd1(w);%调用floyd算法程序 / a( ^5 |) S8 J4 R8 r( P) g %找出任意两点之间的最短路径,并输出 for i=1:n for j=1:n Min_path(i,j).distance=D(i,j);! P* B% i! {- P' i) b %将i到j的最短路程赋值Min_path(i,j).distance( Y: R0 |1 y9 Z) \\; v& U %将i到j所经路径赋给Min_path(i,j).path Min_path(i,j).path(1)=i;( m! m. ^$ b7 | k=1;* k3 K* u6 H# j3 j while Min_path(i,j).path(k)~=j k=k+1;: Q* V3 K8 E2 a; f Min_path(i,j).path(k)=path(Min_path(i,j).path(k-1),j);6 z m1 a! f2 u- o' h0 V end end( f( X* r' F3 M G\ end* y* Z. d5 Y! O9 O s=sprintf('任意两点之间的最短路径如下:');+ u |! a. k9 @2 @8 |+ g9 Y\ disp(s); for i=1:n for j=1:n7 T* A3 [- m$ N( ]( ] m1 H s=sprintf('从%d到%d的最短路径长度为:%d\\n所经路径为:'...7 _, Q* a' u# E4 R1 ~ ,i,j,Min_path(i,j).distance);3 i+ _0 x% `0 `: ^( V! Y4 a. H) S3 {! R disp(s); disp(Min_path(i,j).path);( o4 l% _7 a& M% G end& p3 w5 c! s\ end 2 d z5 e5 v8 }0 l& M) O %找出在指定从start点到terminal点的最短路径,并输出' W+ {' ]: b# L4 w7 d