matlab 常用算法大全_第1頁(yè)
matlab 常用算法大全_第2頁(yè)
matlab 常用算法大全_第3頁(yè)
matlab 常用算法大全_第4頁(yè)
matlab 常用算法大全_第5頁(yè)
已閱讀5頁(yè),還剩21頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、-作者xxxx-日期xxxxmatlab 常用算法大全【精品文檔】Matlab 高級(jí)算法程序代碼匯總一、灰色預(yù)測(cè)模型matlab程序% renkou1=renkou(:,1);%年末常住人口數(shù) % renkou2=renkou(:,2);%戶籍人口% renkou3=renkou(:,3);%非戶籍人口% shjian=1979:2010; %以上數(shù)據(jù)自己給x0=renkou2;n=length(x0);lamda=x0(1:n-1)./x0(2:n)range=minmax(lamda)x1=cumsum(x0)for i=2:nz(i)=0.5*(x1(i)+x1(i-1);endB=-z

2、(2:n),ones(n-1,1);Y=x0(2:n);u=BYx=dsolve(Dx+a*x=b,x(0)=x0);x=subs(x,a,b,x0,u(1),u(2),x1(1);yuce1=subs(x,t,0:n-1);digits(6),y=vpa(x) %為提高預(yù)測(cè)精度,先計(jì)算預(yù)測(cè)值,再顯示微分方程的解yuce=x0(1),diff(yuce1)epsilon=x0-yuce %計(jì)算殘差delta=abs(epsilon./x0) %計(jì)算相對(duì)誤差rho=1-(1-0.5*u(1)/(1+0.5*u(1)*lamda %計(jì)算級(jí)比偏差值%以深圳人口數(shù)據(jù)得到預(yù)測(cè)模型及預(yù)測(cè)誤差相關(guān)數(shù)據(jù) la

3、mda = Columns 1 through 8 0.9741 0.9611 0.9419 0.8749 0.9311 0.9093 0.9302 0.9254 Columns 9 through 16 0.9245 0.9278 0.9442 0.9376 0.9127 0.9148 0.9332 0.9477 Columns 17 through 24 0.9592 0.9445 0.9551 0.9562 0.9594 0.9461 0.9469 0.9239 Columns 25 through 31 0.9140 0.9077 0.9243 0.9268 0.9312 0.9446

4、 0.9618 range = 0.8749 0.9741 x1 = 1.0e+003 * Columns 1 through 8 0.0313 0.0634 0.0967 0.1322 0.1727 0.2162 0.2641 0.3155 Columns 9 through 16 0.3711 0.4313 0.4961 0.5647 0.6380 0.7182 0.8059 0.8999 Columns 17 through 24 0.9990 1.1024 1.2119 1.3265 1.4463 1.5712 1.7033 1.8427 Columns 25 through 32 1

5、.9936 2.1588 2.3407 2.5375 2.7499 2.9780 3.2194 3.4705 u = -0.0665 31.3737 y = -472.117+503.377*exp(.664533e-1*t)yuce = Columns 1 through 8 31.2600 34.5876 36.9641 39.5040 42.2183 45.1192 48.2194 51.5326 Columns 9 through 16 55.0734 58.8576 62.9017 67.2238 71.8428 76.7792 82.0548 87.6928 Columns 17

6、through 24 93.7183 100.1578 107.0397 114.3945 122.2547 130.6550 139.6324 149.2267 Columns 25 through 32 159.4802 170.4382 182.1492 194.6649 208.0405 222.3352 237.6121 253.9386 epsilon = Columns 1 through 8 0 -2.4976 -3.5741 -4.0540 -1.6983 -1.5992 -0.3594 -0.0826 Columns 9 through 16 0.5266 1.2824 1

7、.9183 1.4262 1.3772 3.4408 5.6352 6.2772 Columns 17 through 24 5.4417 3.2222 2.4203 0.2055 -2.4047 -5.7350 -7.5924 -9.7767 Columns 25 through 32 -8.5502 -5.3082 -0.2192 2.1651 4.3395 5.7348 3.8379 -2.9086 delta = Columns 1 through 8 0 0.0778 0.1070 0.1144 0.0419 0.0367 0.0075 0.0016 Columns 9 throug

8、h 16 0.0095 0.0213 0.0296 0.0208 0.0188 0.0429 0.0643 0.0668 Columns 17 through 24 0.0549 0.0312 0.0221 0.0018 0.0201 0.0459 0.0575 0.0701 Columns 25 through 32 0.0567 0.0321 0.0012 0.0110 0.0204 0.0251 0.0159 0.0116 rho = Columns 1 through 8 -0.0411 -0.0271 -0.0066 0.0650 0.0049 0.0282 0.0058 0.011

9、0 Columns 9 through 16 0.0119 0.0084 -0.0091 -0.0020 0.0245 0.0223 0.0027 -0.0128 Columns 17 through 24 -0.0251 -0.0094 -0.0208 -0.0219 -0.0254 -0.0111 -0.0119 0.0126 Columns 25 through 31 0.0232 0.0300 0.0122 0.0095 0.0048 -0.0095 -0.0280二、遺傳算法程序代碼% Optimizing a function using Simple Genetic Algori

10、thm with elitist preserved%Max f(x1,x2)=100*(x1*x1-x2).2+(1-x1).2; -2.0480=x1,x2=bestvbestv=fmax;%到目前為止最優(yōu)適應(yīng)度值bvalxx=bval(indmax,:);%到目前為止最佳位串optxx=xx(indmax,:);%到目前為止最優(yōu)參數(shù)end Bfit1(ii)=bestv; % 存儲(chǔ)每代的最優(yōu)適應(yīng)度%遺傳操作開始%輪盤賭選擇for i=1:(N-1)r=rand;tmp=find(r=q);newbval(i,:)=bval(tmp(1),:);end newbval(N,:)=bvalx

11、x;%最優(yōu)保留bval=newbval;%單點(diǎn)交叉for i=1:2:(N-1)cc=rand;if ccpcpoint=ceil(rand*(2*L-1);%取得一個(gè)1到2L-1的整數(shù)ch=bval(i,:);bval(i,point+1:2*L)=bval(i+1,point+1:2*L);bval(i+1,point+1:2*L)=ch(1,point+1:2*L);endend bval(N,:)=bvalxx;%最優(yōu)保留%位點(diǎn)變異mm=rand(N,2*L)p_best_fitness(count_x)p_best_fitness(count_x) = current_fitness

12、(count_x);for count_y = 1:dimensionsp_best(count_x,count_y) = particle_position(count_x,count_y);endendend%decide on the global best among all the particlesg_best_val,g_best_index = max(current_fitness);%g_best contains the position of teh global bestfor count_y = 1:dimensionsg_best(count_y) = parti

13、cle_position(g_best_index,count_y);end%update the position and velocity compponentsfor count_x = 1:no_of_particlesfor count_y = 1:dimensionsp_current(count_y) = particle_position(count_x,count_y);endfor count_y = 1:dimensionsparticle_velocity(count_y) = particle_velocity(count_y) + c1*rand*(p_best(c

14、ount_y)-p_current(count_y) + c2*rand*(g_best(count_y)-p_current(count_y);particle_positon(count_x,count_y) = p_current(count_y) +particle_velocity(count_y);endendendg_bestcurrent_fitness(g_best_index)clear all, clc % pso exampleiter = 1000; % number of algorithm iterationsnp = 2; % number of model p

15、arametersns = 10; % number of sets of model parametersWmax = 0.9; % maximum inertial weightWmin = 0.4; % minimum inertial weightc1 = 2.0; % parameter in PSO methodologyc2 = 2.0; % parameter in PSO methodologyPmax = 10 10; % maximum model parameter valuePmin = -10 -10; % minimum model parameter value

16、Vmax = 1 1; % maximum change in model parameterVmin = -1 -1; % minimum change in model parametermodelparameters(1:np,1:ns) = 0; % set all model parameter estimates for all model parameter sets to zeromodelparameterchanges(1:np,1:ns) = 0; % set all change in model parameter estimates for all model pa

17、rameter sets to zerobestmodelparameters(1:np,1:ns) = 0; % set best model parameter estimates for all model parameter sets to zerosetbestcostfunction(1:ns) = 1e6; % set best cost function of each model parameter set to a large numberglobalbestparameters(1:np) = 0; % set best model parameter values fo

18、r all model parameter sets to zerobestparameters = globalbestparameters; % best model parameter values for all model parameter sets (to plot)globalbestcostfunction = 1e6; % set best cost function for all model parameter sets to a large numberi = 0; % indicates ith algorithm iterationj = 0; % indicat

19、es jth set of model parametersk = 0; % indicates kth model parameterfor k = 1:np % initializationfor j = 1:nsmodelparameters(k,j) = (Pmax(k)-Pmin(k)*rand(1) + Pmin(k); % randomly distribute model parametersmodelparameterchanges(k,j) = (Vmax(k)-Vmin(k)*rand(1) + Vmin(k); % randomly distribute change

20、in model parametersendendfor i = 2:iterfor j = 1:nsx = modelparameters(:,j);% calculate cost functioncostfunction = 105*(x(2)-x(1)2)2 + (1-x(1)2;if costfunction =T_min iter_num=1; s_num=1; plot(T,totaldis1,r.) hold on while iter_numiter_max&s_nums_max; order2=exhgpath(order1); %隨機(jī)交換兩個(gè)城市位置 totaldis2=

21、distance(address,order2); R=rand; DeltaDis=totaldis2-totaldis1; %新的距離-原來(lái)的距離 if DeltaDisR)%本算法最核心的思想:以一定概率接受壞的結(jié)果,防止局部最優(yōu) order1=order2; totaldis1=totaldis2; else s_num=s_num+1; end iter_num=iter_num+1; end T=T*0.99; end set(gca,xscale,log);%或者使用semilogx,有相同效果 xlabel(退火溫度);ylabel(總距離); order1 totaldis1

22、 figure(3) plot(address(order1,1),address(order1,2),*b-) title(最終路徑); for n=1:size(address,1) text(address(n,1)+0.01,address(n,2),num2str(n)%標(biāo)號(hào) end text(0.9,0.9,num2str(totaldis1) dstc(d)=totaldis1;% endfunction y=exhgpath(order) while 1 b=size(order,1); r=unidrnd(b,1,2); if r(1)-r(2)=0 break end en

23、d b=order(r(2); order(r(2)=order(r(1); order(r(1)=b; y=order;-function y=distance(address,order)nmb=size(address,1);y=0;for i=1:nmb-1 y=y+sqrt(address(order(i+1),1)-address(order(i),1)2+(address(order(i+1),2)-address(order(i),2)2);endy=y+sqrt(address(order(i+1),1)-address(order(1),1)2+(address(order

24、(i+1),2)-address(order(1),2)2);人工神經(jīng)網(wǎng)絡(luò)程序代碼%產(chǎn)生指定類別的樣本點(diǎn),并在圖中繪出 X = 0 1; 0 1; % 限制類中心的范圍 clusters = 5; % 指定類別數(shù)目 points = 10; % 指定每一類的點(diǎn)的數(shù)目 std_dev = 0.05; % 每一類的標(biāo)準(zhǔn)差 P = nngenc(X,clusters,points,std_dev); plot(P(1,:),P(2,:),+r); title(輸入樣本向量); xlabel(p(1); ylabel(p(2); %建立網(wǎng)絡(luò) net=newc(0 1;0 1,5,0.1); %設(shè)置神經(jīng)

25、元數(shù)目為5 %得到網(wǎng)絡(luò)權(quán)值,并在圖上繪出 figure; plot(P(1,:),P(2,:),+r); w=net.iw1 hold on; plot(w(:,1),w(:,2),ob); hold off; title(輸入樣本向量及初始權(quán)值); xlabel(p(1); ylabel(p(2); figure; plot(P(1,:),P(2,:),+r); hold on; %訓(xùn)練網(wǎng)絡(luò) net.trainParam.epochs=7; net=init(net); net=train(net,P); %得到訓(xùn)練后的網(wǎng)絡(luò)權(quán)值,并在圖上繪出 w=net.iw1 plot(w(:,1),w(

26、:,2),ob); hold off; title(輸入樣本向量及更新后的權(quán)值); xlabel(p(1); ylabel(p(2); a=0; p = 0.6 ;0.8; a=sim(net,p) example8_2 %隨機(jī)生成1000個(gè)二維向量,作為樣本,并繪出其分布 P = rands(2,1000); plot(P(1,:),P(2,:),+r) title(初始隨機(jī)樣本點(diǎn)分布); xlabel(P(1); ylabel(P(2); %建立網(wǎng)絡(luò),得到初始權(quán)值 net=newsom(0 1; 0 1,5 6); w1_init=net.iw1,1 %繪出初始權(quán)值分布圖 figure;

27、plotsom(w1_init,net.layers1.distances) %分別對(duì)不同的步長(zhǎng),訓(xùn)練網(wǎng)絡(luò),繪出相應(yīng)的權(quán)值分布圖 for i=10:30:100 net.trainParam.epochs=i; net=train(net,P); figure; plotsom(net.iw1,1,net.layers1.distances) end %對(duì)于訓(xùn)練好的網(wǎng)絡(luò),選擇特定的輸入向量,得到網(wǎng)絡(luò)的輸出結(jié)果 p=0.5;0.3; a=0; a = sim(net,p) example8_3 %指定輸入二維向量及其類別 P = -3 -2 -2 0 0 0 0 +2 +2 +3; 0 +1 -

28、1 +2 +1 -1 -2 +1 -1 0; C = 1 1 1 2 2 2 2 1 1 1; %將這些類別轉(zhuǎn)換成學(xué)習(xí)向量量化網(wǎng)絡(luò)使用的目標(biāo)向量 T = ind2vec(C) %用不同的顏色,繪出這些輸入向量 plotvec(P,C), title(輸入二維向量); xlabel(P(1); ylabel(P(2); %建立網(wǎng)絡(luò) net = newlvq(minmax(P),4,.6 .4,0.1); %在同一幅圖上繪出輸入向量及初始權(quán)重向量 figure; plotvec(P,C) hold on W1=net.iw1; plot(W1(1,1),W1(1,2),ow) title(輸入以及

29、權(quán)重向量); xlabel(P(1), W(1); ylabel(P(2), W(2); hold off; %訓(xùn)練網(wǎng)絡(luò),并再次繪出權(quán)重向量 figure; plotvec(P,C); hold on; net.trainParam.epochs=150; net.trainParam.show=Inf; net=train(net,P,T); plotvec(net.iw1,vec2ind(net.lw2),o); %對(duì)于一個(gè)特定的點(diǎn),得到網(wǎng)絡(luò)的輸出 p = 0.8; 0.3; a = vec2ind(sim(net,p)五、蟻群算法程序代碼% the procedure of ant co

30、lony algorithm for VRP% % % % % % % % % % %initialize the parameters of ant colony algorithmsload data.txt;d=data(:,2:3);g=data(:,4);m=31; % 螞蟻數(shù)alpha=1;belta=4;% 決定tao和miu重要性的參數(shù)lmda=0;rou=0.9;%衰減系數(shù)q0=0.95;% 概率tao0=1/(31*841.04);%初始信息素Q=1;%螞蟻循環(huán)一周所釋放的信息素defined_phrm=15.0; % initial pheromone level val

31、ue QV=100; % 車輛容量vehicle_best=round(sum(g)/QV)+1;%所完成任務(wù)所需的最少車數(shù)V=40;% 計(jì)算兩點(diǎn)的距離 for i=1:32; for j=1:32; dist(i,j)=sqrt(d(i,1)-d(j,1)2+(d(i,2)-d(j,2)2); end;end;%給tao miu賦初值for i=1:32; for j=1:32; if i=j; %s(i,j)=dist(i,1)+dist(1,j)-dist(i,j); tao(i,j)=defined_phrm; miu(i,j)=1/dist(i,j); end; end;end;fo

32、r k=1:32; for k=1:32; deltao(i,j)=0; end;end; best_cost=10000; for n_gen=1:50; print_head(n_gen); for i=1:m; %best_solution=; print_head2(i); sumload=0; cur_pos(i)=1; rn=randperm(32); n=1; nn=1; part_sol(nn)=1; %cost(n_gen,i)=0.0; n_sol=0; % 由螞蟻產(chǎn)生的路徑數(shù)量 M_vehicle=500; t=0; %最佳路徑數(shù)組的元素?cái)?shù)為0 while sumload

33、=QV; for k=1:length(rn); if sumload+g(rn(k)maxp maxp=p(j); index_max=j; end; end; old_pos=cur_pos(i); if rand(1)q0 cur_pos(i)=A(index_max); else krnd=randperm(na); cur_pos(i)=A(krnd(1); bbb=old_pos cur_pos(i); ccc=1 1; if bbb=ccc; cur_pos(i)=A(krnd(2); end; end; tao(old_pos,cur_pos(i)=taolocalupdate

34、(tao(old_pos,cur_pos(i),rou,tao0);%對(duì)所經(jīng)弧進(jìn)行局部更新 sumload=sumload+g(cur_pos(i); nn=nn+1; part_sol(nn)=cur_pos(i); temp_load=sumload; if cur_pos(i)=1; rn=setdiff(rn,cur_pos(i); n=1; A=; end; if cur_pos(i)=1; % 如果當(dāng)前點(diǎn)為車場(chǎng),將當(dāng)前路徑中的已訪問(wèn)用戶去掉后,開始產(chǎn)生新路徑 if setdiff(part_sol,1)=; n_sol=n_sol+1; % 表示產(chǎn)生的路徑數(shù),n_sol=1,2,3

35、,.5,6.,超過(guò)5條對(duì)其費(fèi)用加上車輛的派遣費(fèi)用 fid=fopen(out_solution.txt,a+); fprintf(fid,%s%i%s,NO.,n_sol,條路徑是:); fprintf(fid,%i ,part_sol); fprintf(fid,n); fprintf(fid,%s,當(dāng)前的用戶需求量是:); fprintf(fid,%in,temp_load); fprintf(fid,-n); fclose(fid); % 對(duì)所得路徑進(jìn)行路徑內(nèi)3-opt優(yōu)化 final_sol=exchange(part_sol); for nt=1:length(final_sol); % 將所有產(chǎn)生的路徑傳給一個(gè)數(shù)組 temp(t+nt)=final_sol(nt); end; t=t+length(final_sol)-1; sumload=0; final_sol=setdiff(final_sol,1); rn=setdiff(rn,final_sol); par

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論