matlab常用演算法大全

2021-09-12 12:17:53 字數 7790 閱讀 2802

matlab 高階演算法程式**彙總

% renkou1=renkou(:,1);%年末常住人口數

% renkou2=renkou(:,2);%戶籍人口

% renkou3=renkou(:,3);%非戶籍人口

% shjian=1979:2010;

%以上資料自己給

x0=renkou2';

n=length(x0);

lamda=x0(1:n-1)./x0(2:n)

range=minmax(lamda)

x1=cumsum(x0)

for i=2:n

z(i)=0.5*(x1(i)+x1(i-1));

endb=[-z(2:n)',ones(n-1,1)];

y=x0(2:n)';

u=b\y

x=dsolve('dx+a*x=b','x(0)=x0');

x=subs(x,,);

yuce1=subs(x,'t',[0:n-1]);

digits(6),y=vpa(x) %為提高**精度,先計算**值,再顯示微分方程的解

yuce=[x0(1),diff(yuce1)]

epsilon=x0-yuce %計算殘差

delta=abs(epsilon./x0) %計算相對誤差

rho=1-(1-0.5*u(1))/(1+0.5*u(1))*lamda %計算級比偏差值

%以深圳人口資料得到**模型及**誤差相關資料

lamda =

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 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.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 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.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 through 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.0110

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 ****** genetic algorithm with elitist preserved

%max f(x1,x2)=100*(x1*x1-x2).^2+(1-x1).^2; -2.0480<=x1,x2<=2.0480

% author: wang yonglin (

clc;clear all;

format long;%設定資料顯示格式

%初始化引數

t=100;%**代數

n=80;% 群體規模

pm=0.05;pc=0.8;%交叉變異概率

umax=2.048;umin=-2.048;%引數取值範圍

l=10;%單個引數字串長度,總編碼長度2l

bval=round(rand(n,2*l));%初始種群

bestv=-inf;%最優適應度初值

%迭代開始

for ii=1:t

%解碼,計算適應度

for i=1:n

y1=0;y2=0;

for j=1:1:l

y1=y1+bval(i,l-j+1)*2^(j-1);

endx1=(umax-umin)*y1/(2^l-1)+umin;

for j=1:1:l

y2=y2+bval(i,2*l-j+1)*2^(j-1);

endx2=(umax-umin)*y2/(2^l-1)+umin;

obj(i)=100*(x1*x1-x2).^2+(1-x1).^2; %目標函式

xx(i,:)=[x1,x2];

endfunc=obj;%目標函式轉換為適應度函式

p=func./sum(func);

q=cumsum(p);%累加

[fmax,indmax]=max(func);%求當代最佳個體

if fmax>=bestv

bestv=fmax;%到目前為止最優適應度值

bvalxx=bval(indmax,:);%到目前為止最佳位串

optxx=xx(indmax,:);%到目前為止最優引數

endbfit1(ii)=bestv; % 儲存每代的最優適應度

%%%%遺傳操作開始

%輪盤賭選擇

for i=1:(n-1)

r=rand;

tmp=find(r<=q);

newbval(i,:)=bval(tmp(1),:);

endnewbval(n,:)=bvalxx;%最優保留

bval=newbval;

%單點交叉

for i=1:2:(n-1)

cc=rand;

if ccpoint=ceil(rand*(2*l-1));%取得乙個1到2l-1的整數

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;%最優保留

%位點變異

mm=rand(n,2*l)mm(n,:)=zeros(1,2*l);%最後一行不變異,強制賦0

bval(mm)=1-bval(mm);

end%輸出

plot(bfit1);% 繪製最優適應度進化曲線

bestv %輸出最優適應度值

optxx %輸出最優引數

%declare the parameters of the optimization

max_iterations = 1000;

no_of_particles = 50;

dimensions = 1;

delta_min = -0.003;

delta_max = 0.003;

c1 = 1.3;

c2 = 1.3;

%initialise the particles and teir velocity components

for count_x = 1:no_of_particles

for count_y = 1:dimensions

particle_position(count_x,count_y) = rand*10;

particle_velocity(count_x,count_y) = rand;

p_best(count_x,count_y) = particle_position(count_x,count_y);

endend%initialize the p_best_fitness array

for count = 1:no_of_particles

p_best_fitness(count) = -1000;

end%particle_position

%particle_velocity

%main particle swrm routine

for count = 1:max_iterations

%find the fitness of each particle

%change fitness function as per equation requiresd and dimensions

for count_x = 1:no_of_particles

%x = particle_position(count_x,1);

%y = particle_position(count_x,2);

%z = particle_position(count_x,3);

%soln = x^2 - 3*y*x + z;

%x = particle_position(count_x);

%soln = x^2-2*x+1;

x = particle_position(count_x);

soln = x-7;

if soln~=0

current_fitness(count_x) = 1/abs(soln);

else

current_fitness =1000;

endend%decide on p_best etc for each particle

for count_x = 1:no_of_particles

if current_fitness(count_x) >p_best_fitness(count_x)

p_best_fitness(count_x) = current_fitness(count_x);

for count_y = 1:dimensions

p_best(count_x,count_y) = particle_position(count_x,count_y);

endendend%decide on the global best among all the particles

[g_best_val,g_best_index] = max(current_fitness);

%g_best contains the position of teh global best

for count_y = 1:dimensions

g_best(count_y) = particle_position(g_best_index,count_y);

end%update the position and velocity compponents

for count_x = 1:no_of_particles

for count_y = 1:dimensions

p_current(count_y) = particle_position(count_x,count_y);

endfor count_y = 1:dimensions

particle_velocity(count_y) = particle_velocity(count_y) + c1*rand*(p_best(count_y)-p_current(count_y)) + c2*rand*(g_best(count_y)-p_current(count_y));

C語言常用演算法大全

1 非數值計算常用經典演算法 1 窮舉 也稱為 列舉法 即將可能出現的每一種情況一一測試,判斷是否滿足條件,一般採用迴圈來實現。例1 用窮舉法輸出所有的水仙花數 即這樣的三位正整數 其每位數字上的數字的立方和與該數相等,比如 13 53 33 153 法一 main 解析 此方法是將100到999所...

matlab函式大全

matlab函式參考 附錄1 常用命令 附錄1.1 管理用命令 附錄1.2管理變數與工作空間用命令 附錄1.3檔案與作業系統處理命令 附錄1.4視窗控制命令 附錄1.5啟動與退出命令 附錄2 運算符號與特殊字元附錄 2.1運算符號與特殊字元 附錄2.2邏輯函式 附錄3 語言結構與除錯 附錄3.1程式...

VB 常用演算法總結

一 變數值的交換 演算法思想 若交換兩個變數的值,必須引入第三個新的變數進行傳遞。以下 是錯誤的 x 12 y 34 x y y x 正確的 是 x 12 y 23 t x x y y t 二 判斷乙個數是否能被另乙個數整除 演算法思想 可以用整除的定義 餘數為0 或x除以y等於x整除y等表示式進行...