部分MATLAB函数实现_精品文档.txt
《部分MATLAB函数实现_精品文档.txt》由会员分享,可在线阅读,更多相关《部分MATLAB函数实现_精品文档.txt(18页珍藏版)》请在冰豆网上搜索。
2.��������
C_real=real(C)
C_imag=imag(C)
C_magnitude=abs(C)
3.����������y=exp(-t/3)*sin(3*t)
t=0:
pi/50:
4*pi;
y0=exp(-t/3);
y=exp(-t/3).*sin(3*t);
plot(t,y,'-r',t,y0,':
b',t,-y0,':
b')
grid
4.����������
4.0
clear;
x=-8:
0.5:
8;%�����Ա���x��һλ�̶�����
y=x';%�����Ա���y��һλ�̶�����
X=ones(size(y))*x;%�����Ա���ƽ����ȡֵ��x����Ķ�ά����
Y=y*ones(size(x));%�����Ա���ƽ����ȡֵ��y����Ķ�ά����
R=sqrt(X.^2+Y.^2)+eps;%�����м����R=x,yƽ���͵�ƽ����
Z=sin(R)./R;%�������Ա�����ά������Ӧ�ĺ���ֵz=sinR/R
mesh(Z);%������ά����ͼ
colormap(hot)
4.1Schaffer����
N=10;
n=0.1;
M=2*N/n+1;
x=-N:
n:
N;%�����Ա���x��һλ�̶�����
y=-N:
n:
N;%�����Ա���x��һλ�̶�����
[X,Y]=meshgrid(x,y);
R=sqrt(X.^2+Y.^2)+eps;%�����м����R=x,yƽ���͵�ƽ����
A=sin(R).^2;
B=(1+0.001*R.^2)^2;
z=0.5+(A-0.5)./B;
mesh(z);%������ά����ͼ
colormap(hot)
4.2Shubert����
N=10;
n=0.1;
M=2*N/n+1;
x=-N:
n:
N;%�����Ա���x��һλ�̶�����
y=-N:
n:
N;%�����Ա���x��һλ�̶�����
[X,Y]=meshgrid(x,y);
A=zeros(M,M);B=zeros(M,M);
fori=1:
5
R=(i+1)*X+i;
S=(i+1)*Y+i;
A=A+i*cos(R);
B=B+i*cos(S);
end
z=A.*B;
mesh(z);%������ά����ͼ
colormap(hot)
4.3Griewank����
N=10;
n=0.1;
M=2*N/n+1;
x=-N:
n:
N;%�����Ա���x��һλ�̶�����
y=-N:
n:
N;%�����Ա���x��һλ�̶�����
[X,Y]=meshgrid(x,y);
z=X.^2/4000+Y.^2/4000+cos(X).*cos(Y/2)+1;
mesh(z);%������ά����ͼ
colormap(hot)
4.4Rastrigrin����
N=2.12;
n=0.05;
M=2*N/n+1;
x=-N:
n:
N;%�����Ա���x��һλ�̶�����
y=-N:
n:
N;%�����Ա���x��һλ�̶�����
[X,Y]=meshgrid(x,y);
z=(X.^2-10*cos(2*pi*X)+10)+(Y.^2-10*cos(2*pi*Y)+10);
mesh(z);
colormap(hot)
5.���ݴ�ȡ
mkdir('c:
\','my_dir');%��C���ϴ���Ŀ¼my_dir
cdc:
\my_dir%ʹc:
\my_dir��Ϊ��ǰĿ¼
savesafXYZ%ѡ���ڴ��е�X��Y��Z���浽saf.mat
dir%��ʾĿ¼�ϵ��ļ�
%����ڴ棬װ�ر���Z
clear%����ڴ�
loadsafZ%�ѱ���Z�����ڴ�
who%����ڴ��еı���
�ڶ���
1.��������룺
Time=[12345678910111213]
X_Da=[2.323.43;4.375.98]
vect_a=[12345]
Matrix_B=[123;
234;345]
Null_M=[]%���ɿվ���
2����������
a=2.7;b=13/25;
C=[1,2*a+i*b,b*sqrt(a);sin(pi/4),a+5*b,3.5+1]
����
R=[123;456],M=[111213;141516]
CN=R+i*M
3��sym��������������ž���
sym(������1��������2��������n��
sym_matrix=sym('[ABC;NIHAO,Welcome,tobeijing],')
sym_digits=sym('[123;abc;sin(x)cos(y)tan(z)]')
clc;
clear;
sym_matrix=sym('[a,b,c;Jack,Help,Me!
;NO,WAY,!
]')
����ֵ������Ϊ���ž���
a=[1/3sqrt
(2)3.4234;exp(0.23)log(29)23^(-11.23)]
s=sym(a)
4��cat����
A=cat(n,A1,A2,������,Am)%����������
A1=[1,2,3;4,5,6;7,8,9];A2=A1';A3=A1-A2;
A4=cat(3,A1,A2,A3)
5��zeros()
zeros(n)\zeros(m,n)\zeros([m,n])\zeros(size(A))
n=6;
A=zeros(n)
6��eye���������������
Y=eye(n)��eye��m,n)��eye(size(A))
n=4;
m=5;
Y1=eye(n)
Y2=eye(m,n)
Y3=eye(size(Y1))
7��ones����������ȫ1��
Y=ones(n)��ones(m,n)��ones(size(A))��
n=4;
m=6;
X1=ones(m,n)
d1=2;d2=3;d3=4;
X2=ones(d1,d2,d3)
8��rand���������ɾ��ȷֲ��������
Y=rand(n)\rand(m,n)\rand(size(A))��
s=rand('state')�����������ȷ�������ǰ״̬��35��Ԫ�ص�������
rand('state',s)ʹ״̬����ΪS.
rand('state',0)���÷���������ʼ״̬��
rand('state',j)������j���÷���������j��״̬��
rand('state',sum(100*clock))ÿ�����õ���ͬ��״̬��
R=rand(3,4)
a=10;b=20;
x=a+(b-a)*rand(4)
9��randn������������̬�������
Y=randn(n)%����n*n��̬�ֲ��������
�ȵ�ͬ��
randn%�ޱ�������ʱֻ����һ����̬�ֲ������
s=randn('state')�����������ȷ�������ǰ״̬��2��Ԫ�ص�������
rand('state',s)ʹ״̬����ΪS.
rand('state',0)���÷���������ʼ״̬��
rand('state',j)������j���÷���������j��״̬��
rand('state',sum(100*clock))ÿ�����õ���ͬ��״̬��
������ֵΪ0.6������Ϊ0.1��4����
mu=0.6;sigma=0.1;
x=mu+sqrt(sigma)*randn(4)
10��randperm�����������������
p=randperm(n)%����1~n֮���������������
randperm(6)
11��linspace���������Եȷ�����������
y=linspace(a,b��%�ڣ�a,b)�ϲ���100�����Եȷֵ�
y=linspace(a,b,n)%�ڣ�a,b)�ϲ���n�����Եȷֵ�
��1-200֮������100��\20���ȷֵ�
a=1;b=200;
y=linspace(a,b)
z=linspace(a,b,100)
a=0;b=200;
y=linspace(a,b)
z=linspace(a,b,100)
12��logspace�����������ȷ�����������
y=logspace(a,b��%�ڣ�10^a,10^b)�ϲ���50�������ȷֵ�
y=l0gspace(a,b,n)%�ڣ�10^a,10^b)�ϲ���n�������ȷֵ�
��10^2-10^4֮������50��\30���ȷֵ�
a=2;b=4;
x1=logspace(a,b)
x2=logspace(a,b,30)
13��compan�����������Ѿ��������ĸ���
A=compan(u)
��x-1)��x+2)(x-3)��x^3-8*x+13���Ѿ����
u=[1,0,-8,13];
A=compan(u)
eig(A)
u=[1,-2,-5,6];
A=compan(u)
eig(A)
��һ��ʵ��
��ʵ��
han1=[83.079.878.185.186.688.290.386.793.392.590.996.9
101.785.187.891.693.494.597.499.5104.2102.3101.0123.5
92.2114.093.3101.0103.5105.2109.5109.2109.6111.2121.7131.3
105.0125.7106.6116.0117.6118.0121.7118.7120.2127.8121.8121.9
139.3129.5122.5124.5135.7130.8138.7133.7136.8138.9129.6133.7
137.5135.3133.0133.4142.8141.6142.9147.3159.6162.1153.5155.9
163.2159.7158.4145.2124.0144.1157.0162.6171.8180.7173.5176.5];
han1(end,:
)=[];m=size(han1,2);%�����һ��Ϊ�ա������е�����
x0=mean(han1,2);%��han1��ÿ�е�ƽ��������ÿ��ľ�ֵ
x1=cumsum(x0);%x1(k)=x0(k-1)+x0(k)
alpha=0.4;n=length(x0);%������z1
z1=alpha*x1(2:
n)+(1-alpha)*x1(1:
n-1)%������z1
Y=x0(2:
n);B=[-z1,ones(n-1,1)];
ab=B\Y%�������a��b
k=6;%��ʱ����ǵ�k+1��Ԥ��ֵ
x7hat=(x0
(1)-ab
(2)/ab
(1))*(exp(-ab
(1)*k)-exp(-ab
(1)*(k-1)))%ֱ����������Ԥ��ֵ��������ƽ��ֵ��
z=m*x7hat%����������ֵ
u=sum(han1)/sum(sum(han1))%��ÿ�±���=������ͬ��֮�ͣ�/�������ܺͣ�
v=z*u%�����Ԥ��ֵ
��2���ϸ���
renkou
renkou1=rk(:
1);%renkou���е�һ�У�����ĩ��ס�˿���
renkou2=rk(:
2);%renkou���еڶ��У��������˿�
renkou3=rk(:
3);%renkou���е����У����ǻ����˿�
x0=renkou2';%�����ʼֵ����������ת��
n=length(x0);%�±�
lamda=x0(1:
n-1)./x0(2:
n)%�����еļ���
range=minmax(lamda)%���ȵķ�Χ
x1=cumsum(x0)%���ۻ���x1
fori=2:
n
z(i)=0.5*(x1(i)+x1(i-1));
end%����=0.5ʱ��z1
B=[-z(2:
n)',ones(n-1,1)];%������
Y=x0(2:
n)';%������
u=B\Y%���������u=(a,b)
x=dsolve('Dx+a*x=b','x(0)=x0');%��ⳣ�֣�����x
(1)=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)%��x����5λС��
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%���㼶��ƫ��ֵ
clc,clear
han1=[83.079.878.185.186.688.290.386.793.392.590.996.9
101.785.187.891.693.494.597.499.5104.2102.3101.0123.5
92.2114.093.3101.0103.5105.2109.5109.2109.6111.2121.7131.3
105.0125.7106.6116.0117.6118.0121.7118.7120.2127.8121.8121.9
139.3129.5122.5124.5135.7130.8138.7133.7136.8138.9129.6133.7
137.5135.3133.0133.4142.8141.6142.9147.3159.6162.1153.5155.9
163.2159.7158.4145.2124.0144.1157.0162.6171.8180.7173.5176.5];
han1(end,:
)=[];m=size(han1,2);%�����һ��Ϊ�ա������е�����
x0=mean(han1,2);%��han1��ÿ�е�ƽ��������ÿ��ľ�ֵ
x1=cumsum(x0);%x1(k)=x0(k-1)+x0(k)
alpha=0.4;n=length(x0);%������z1
z1=alpha*x1(2:
n)+(1-alpha)*x1(1:
n-1)%������z1
Y=x0(2:
n);B=[-z1,ones(n-1,1)];
ab=B\Y%�������a��b
k=6;
x7hat=(x0
(1)-ab
(2)/ab
(1))*(exp(-ab
(1)*k)-exp(-ab
(1)*(k-1)))%ֱ����������Ԥ��ֵ��������ƽ��ֵ��
z=m*x7hat%����������ֵ
u=sum(han1)/sum(sum(han1))%��ÿ�±���=������ͬ��֮�ͣ�/�������ܺͣ�
v=z*u%�����Ԥ��ֵ
renkou
renkou1=rk(:
1);%renkou���е�һ�У�����ĩ��ס�˿���
renkou2=rk(:
2);%renkou���еڶ��У��������˿�
renkou3=rk(:
3);%renkou���е����У����ǻ����˿�
x0=renkou2';%�����ʼֵ����������ת��
n=length(x0);%�±�
lamda=x0(1:
n-1)./x0(2:
n)%�����еļ���
range=minmax(lamda)%���ȵķ�Χ
x1=cumsum(x0)%���ۻ���x1
fori=2:
n
z(i)=0.5*(x1(i)+x1(i-1));
end%����=0.5ʱ��z1
B=[-z(2:
n)',ones(n-1,1)];%������
Y=x0(2:
n)';%������
u=B\Y%���������u=(a,b)
x=dsolve('Dx+a*x=b','x(0)=x0');%��ⳣ�֣�����x
(1)=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)%��x����5λС��
yuce
(1)=x0
(1);
fori=2:
n
yuce(i)=yuce1(i)-yuce1(i-1);
end
yuce=vpa(yuce)
epsilon=x0-yuce%����в�
delta=abs(epsilon./x0)%����������
rho=1-(1-0.5*u
(1))/(1+0.5*u
(1))*lamda%���㼶��ƫ��ֵ
2���Ŵ��㷨
��һ�γ��ԣ���
fplot('variable.*variable',[0,31]);%��������variable.*variable��?
[0,31]�ϵ�ͼ��
NIND=4;%��Ⱥ����Ŀ
MAXGEN=10;%����Ŵ���
PRECI=5;%�����Ķ�������
GGAP=1;%����
trace=zeros(2,MAXGEN);%Ѱ�Ž���ij�ʼֵ
FieldD=[5;0;31;1;0;1;1];%����������
Chrom=crtbp(NIND,PRECI);%��ʼ��Ⱥ
gen=0;%������
variable=bs2rv(Chrom,FieldD);%�����ʼ��Ⱥ��ʮ����ת��
ObjV=variable.*variable;%����Ŀ�꺯��ֵ
whilegenFitnV=ranking(-ObjV);%������Ӧ�Ⱥ���ֵ
SelCh=select('sus',Chrom,FitnV,GGAP);%ѡ��
SelCh=recombin('xovsp',SelCh,1);%���飬100%
SelCh=mut(SelCh);%����
variable=bs2rv(SelCh,FieldD);%�Ӵ������ʮ���ƻ�
ObjVSel=variable.*variable;%�����Ӵ���Ŀ�꺯��ֵ
[ChromObjV]=reins(Chrom,SelCh,1,1,ObjV,ObjVSel);%�ز����Ӵ�������Ⱥ
variable=bs2rv(Chrom,FieldD);%�Ӵ������ʮ����ת��
gen=gen+1;%����������1
%������Ž⼰����ţ�����Ŀ�꺯��ͼ���б����YΪ���Ž�,IΪ��Ⱥ�����
[Y,I]=max(ObjV);
holdon;
plot(variable(I),Y,'bo');
trace(1,gen)=max(ObjV);%�Ŵ��㷨���ܸ���
trace(2,gen)=sum(ObjV)/length(ObjV);
end
variable=bs2rv(Chrom,FieldD);%���Ÿ����ʮ����ת��
grid,holdon;
plot(variable,ObjV,'b*');
figure
(2);
plot(trace(1,:
));
holdon;
plot(trace(2,:
),'-.');grid
legend('��ı仯','��Ⱥ��ֵ�ı仯')
�ڶ��γ��ԣ�������
�Ż�����ʹ�ü��Ŵ��㷨�����ű����һ�θ���,һ�ν��棬һ�α��죩
Maxf(x1,x2)=100*(x1*x1-x2).^2+(1-x1).^2;-2.0480<=x1,x2<=2.0480
formatlong;%�趨������ʾ��ʽ
%��ʼ������
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;%������Ӧ�ȳ�ֵ���������
%������ʼ
forii=1:
T
%���룬������Ӧ��
fori=1:
N%���ii��ʱ����������Ŀ�꺯��ֵobj������Ӧx1,x1��xx(��,)
y1=0;y2=0;
forj=1:
1:
L
y1=y1+bval(i,L-j+1)*2^(j-1);%�ѵ�i��ǰ10��Ԫ�صĶ����ƻ�Ϊʮ����y1
end
x1=(umax-umin)*y1/(2^L-1)+umin;%����y1��Ӧ��x1����������*�����С(����ȥ��С)+��Сֵ(��ֵ)
forj=1:
1:
L
y2=y2+bval(i,2*L-j+1)*2^(j-1);%�ѵ�i�к�10��Ԫ�صĶ����ƻ�Ϊʮ����y2
end
x2=(umax-umin)*y2/(2^L-1)+umin;%����y2��Ӧ��x2����������*�����С(����ȥ��С)+��Сֵ(��ֵ)
obj(i)=100*(x1*x1-x2).^2+(1-x1).^2;%Ŀ�꺯������ʵҲ��ָ�꺯��ֵ
xx(i,:
)=[x1,x2];%��i��xx��¼x1,x1
end
func=obj;%Ŀ�꺯��ת��Ϊ��Ӧ�Ⱥ���
p=func./sum(func);%ÿ��������Ӧ��ֵռ�����İٷֱ�
q=cumsum(p);%�����ۼ�ֵ
[fmax,indmax]=max(func);%����Ѹ��壬fmax��ʾ���ֵ��indmax��ʾ���ֵ��Ӧλ�ã���i
iffmax>=bestv%ÿһ������һ�Σ�����ĿǰΪֹ�������Ӧ��ֵ
bestv=fmax;%��ĿǰΪֹ������Ӧ��ֵ
bvalxx=bval(indmax,:
);%��ĿǰΪֹ���λ������ʼ��Ⱥ�Ķ����ƴ���
optxx=xx(indmax,:
);%��ĿǰΪֹ���Ų����������Ÿ����Ӧ��x1,x2��
end
Bfit1(ii)=bestv;%�洢ÿ����������Ӧ��
%%%%�Ŵ�������ʼ
%���̶�ѡ��
fori=1:
(N-1)%�������N-1�����֣��ô���Ӧ�����������ƴ�
r=rand;%���������r
tmp=find(r<=q);
newbval(i,:
)=bval(tmp
(1),:
);%�ҵ�r��Ӧ������
end
newbval(N,:
)=bvalxx;%���ű���
bval=newbval;%�������������ѡ��--����
%���㽻��
fori=1:
2:
(N-1)%ÿ������������һ�ν���
cc=rand;%�������cc
ifccpoint=ceil(rand*(2*L-1));%ȡ��һ��1��2L-1������/ceil��ʾ+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);%ʵ�����������ƴ�i��i+1��point+1��2L�Ľ���
end
end
bval(N,:
)=bvalxx;%���ű���,�������i=79ʱ��������һ���ı䣬�����ٻָ����
%����
mm=rand(N,2*L)mm(N,:
)=zeros(1,2*L);%���һ�в����죬ǿ�Ƹ�0
bval(mm)=1-bval(mm);%��ϡ�����mm����λ��Ӧλ��ͻ��
end
%���
plot(Bfit1);%����������Ӧ�Ƚ�������
bestv%���������Ӧ��ֵ
optxx%������Ų���
������
sjs%���صз�100��Ŀ�������
d1=[70,40];
sj0=[d1;sj;d1];
%�������d
sj=sj0*pi/180;
d=zeros(102);
fori=1:
101
forj=i+1:
102
temp=cos(sj(i,1)-sj(j,1))*cos(sj(i,2))*cos(sj(j,2))+sin(sj(i,2))*sin(sj(j,2));
d(i,j)=6370*acos(temp);
end
end
d=d+d';L=102;w=50;dai=100;
%ͨ������Ȧ�㷨ѡȡ��������A
fork=1:
w
c=randperm(100);
c1=[1,c+1,102];
flag=1;
whileflag>0
flag=0;
form=1:
L-3
forn=m+2:
L-1
ifd(c1(m),c1(n))+d(c1(m+1),c1(n+1))flag=1;
c1(m+1:
n)=c1(n:
-1:
m+1);
end
end
end
end
J(k,c1)=1:
102;
end
J=J/102;
J(:
1)=0;J(:
102)=1;
rand('state',sum(clock));
%�Ŵ��㷨ʵ�ֹ���
A=J;
fork=1:
dai%����0��1�����������
B=A;
c=randperm(w);
%��������Ӵ�B
fori=1:
2:
w
F=2+floor(100*rand
(1));
temp=B(c(i),F:
102);
B(c(i),F:
102)=B(c(i+1),F:
102);
B(c(i+1),F:
102)=temp;
end
%��������Ӵ�C
by=find(rand(1,w)<0.1);
iflength(by)==0
by=floor(w*rand
(1))+1;
end
C=A(by,:
);
L3=length(by);
forj=1:
L3
bw=2+floor(100*rand(1,3));
bw=sort(bw);
C(j,:
)=C(j,[1:
bw
(1)-1,bw
(2)+1:
bw(3),bw
(1):
bw
(2),bw(3)+1:
102]);
end
G=[A;B;C];
TL=size(G,1);
%�ڸ������Ӵ���ѡ������Ʒ����Ϊ�µĸ���
[dd,IX]=sort(G,2);temp(1:
TL)=0;
forj=1:
TL
fori=1:
101
temp(j)=temp(j)+d(IX(j,i),IX(j,i+1));
end
end
[DZ,IZ]=sort(temp);
A=G(IZ(1:
w),:
);
end
path=IX(IZ
(1),:
)
long=DZ
(1)
xx=sj0(path,1);yy=sj0(path,2);
plot(xx,yy,'-o')
3������Ⱥ�㷨
functionDrawRastrigin()%����Rastrigin����ͼ��
x=[-5:
0.05:
5];
y=x;
[X,Y]=meshgrid(x,y);
[row,col]=size(X);
forl=1:
col
forh=1:
row
z(h,l)=Rastrigin([X(h,l),Y(h,l)]);
end
end
surf(X,Y,z);
shadinginterp
�ڶ��γ��ԣ�������
%�����������Ż�
max_iterations=1000;
no_of_particles=50;
dimensions=1;
delta_min=-0.003;
delta_max=0.003;
c1=1.3;
c2=1.3;
%��ʼ�����Ӽ����ٶȷ���
forcount_x=1:
no_of_particles
forcount_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);
end
����end
����%initializethep_best_fitnessarray
����forcount=1:
no_of_particles
����p_best_fitness(count)=-1000;
����end
����%particle_position
����%particle_velocity
����%mainparticleswrmroutine
����forcount=1:
max_iterations
����%findthefitnessofeachparticle
����%changefitnessfunctionasperequationrequiresdanddimensions
����forcount_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;
����ifsoln~=0
����current_fitness(count_x)=1/abs(soln);
����else
����current_fitness=1000;
����end
����end
����%decideonp_bestetcforeachparticle
����forcount_x=1:
no_of_particles
����ifcurrent_fitness(count_x)>p_best_fitness(count_x)
����p_best_fitness(count_x)=current_fitness(count_x);
����forcount_y=1:
dimensions
����p_best(count_x,count_y)=particle_position(count_x,count_y);
����end
����end
����end
����%decideontheglobalbestamongalltheparticles
����[g_best_val,g_best_index]=max(current_fitness);
����%g_bestcontainsthepositionoftehglobalbest
����forcount_y=1:
dimensions
����g_best(count_y)=particle_position(g_best_index,count_y);
����end
����%updatethepositionandvelocitycompponents
����forcount_x=1:
no_of_particles
����forcount_y=1:
dimensions
����p_current(count_y)=particle_position(count_x,count_y);
����end
����forcount_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));
����particle_positon(count_x,count_y)=p_current(count_y)+particle_velocity(count_y);
����end
����end
����end
����g_best
����current_fitness(g_best_index)
����clearall,clc%psoexample
����iter=1000;%numberofalgorithmiterations
����np=2;%numberofmodelparameters
����ns=10;%numberofsetsofmodelparameters
����Wmax=0.9;%maximuminertialweight
����Wmin=0.4;%minimuminertialweight
����c1=2.0;%parameterinPSOmethodology
����c2=2.0;%parameterinPSOmethodology
����Pmax=[1010];%maximummodelparametervalue
����Pmin=[-10-10];%minimummodelparametervalue
����Vmax=[11];%maximumchangeinmodelparameter
����Vmin=[-1-1];%minimumchangeinmodelparameter
����modelparameters(1:
np,1:
ns)=0;%setallmodelparameterestimatesforallmodelparametersetstozero
����modelparameterchanges(1:
np,1:
ns)=0;%setallchangeinmodelparameterestimatesforallmodelparametersetstozero
����bestmodelparameters(1:
np,1:
ns)=0;%setbestmodelparameterestimatesforallmodelparametersetstozero
����setbestcostfunction(1:
ns)=1e6;%setbestcostfunctionofeachmodelparametersettoalargenumber
����globalbestparameters(1:
np)=0;%setbestmodelparametervaluesforallmodelparametersetstozero
����bestparameters=globalbestparameters';%bestmodelparametervaluesforallmodelparametersets(toplot)
����globalbestcostfunction=1e6;%setbestcostfunctionforallmodelparametersetstoalargenumber
����i=0;%indicatesithalgorithmiteration
����j=0;%indicatesjthsetofmodelparameters
����k=0;%indicateskthmodelparameter
����fork=1:
np%initialization
����forj=1:
ns
����modelparameters(k,j)=(Pmax(k)-Pmin(k))*rand
(1)+Pmin(k);%randomlydistributemodelparameters
����modelparameterchanges(k,j)=(Vmax(k)-Vmin(k))*rand
(1)+Vmin(k);%randomlydistributechangeinmodelparameters
����end
����end
����fori=2:
iter
����forj=1:
ns
����x=modelparameters(:
j);
����%calculatecostfunction
����costfunction=105*(x
(2)-x
(1)^2)^2+(1-x
(1))^2;
����ifcostfunction����bestmodelparameters(:
j)=modelparameters(:
j);
����setbestcostfunction(j)=costfunction;
����end
4���˻��㷨
sjs%���صз�100��Ŀ�������
d1=[70,40];
sj=[d1;sj;d1];
sj=sj*pi/180;
%�������d
d=zeros(102);
fori=1:
101
forj=i+1:
102
temp=cos(sj(i,1)-sj(j,1))*cos(sj(i,2))*cos(sj(j,2))+sin(sj(i,2))*sin(sj(j,2));
d(i,j)=6370*acos(temp);
end
end
d=d+d';
S0=[];Sum=inf;
rand('state',sum(clock));
forj=1:
1000
S=[11+randperm(100),102];
temp=0;
fori=1:
101
temp=temp+d(S(i),S(i+1));
end
iftempS0=S;Sum=temp;
end
end
e=0.1^30;L=20000;at=0.999;T=1;
%�˻����
fork=1:
L
%�����½�
c=2+floor(100*rand(1,2));
c=sort(c);
c1=c
(1);c2=c
(2);
%������ۺ���ֵ
df=d(S0(c1-1),S0(c2))+d(S0(c1),S0(c2+1))-d(S0(c1-1),S0(c1))-d(S0(c2),S0(c2+1));
%������
ifdf<0
S0=[S0(1:
c1-1),S0(c2:
-1:
c1),S0(c2+1:
102)];
Sum=Sum+df;
elseifexp(-df/T)>rand
(1)
S0=[S0(1:
c1-1),S0(c2:
-1:
c1),S0(c2+1:
102)];
Sum=Sum+df;
end
T=T*at;
ifTbreak;
end
end
%���Ѳ��·����·������
S0,Sum
path=S0;
xx=sj(path,1);yy=sj(path,2);
plot(xx,yy,'-o')