clc clear all fprintf('Nguyen Trung Do\n '); fprintf('bai tap genetic\n'); lamdamin=input('Nhap vao buoc song min (nm):'); while isempty(lamdamin)|lamdamin<=0 lamdamin=input('Nhap vao buoc song min khoang 1000 (nm):'); end lamdamax=input('Nhap vao buoc song max (nm):'); while isempty(lamdamax)|lamdamax<=lamdamin lamdamax=input('Nhap vao buoc song max khoang 2000(nm):'); end disp('So 1:MgF2 So 2:Ta2O5 So 3:SiO2 So 4:TiO2'); n1=input('Nhap vao so thu NHAT (1;2;3 hoac 4):'); while isempty(n1)|(n1~=1&n1~=2&n1~=3&n1~=4) n1=input('Nhap vao so thu NHAT (1;2;3 hoac 4):'); end n2=input('Nhap vao so thu HAI (1;2;3 hoac 4, khac voi so thu NHAT):'); while isempty(n2)|(n2~=1&n2~=2&n2~=3&n2~=4)|n2==n1 n2=input('Nhap vao so thu HAI (1;2;3 hoac 4, khac voi so thu NHAT):'); end solop=input('so lop ma ban muon phu solop= '); while isempty(solop)|(solop<3) solop=input('Nhap so lop >=3:'); end fprintf('Cho biet lop mang dau tien la lop cua chat co chiet suat cao hay chiet suat thap.\n') fprintf(' Neu la chat co chiet suat cao thi nhap vao so 1.\n') fprintf(' Neu la chat co chiet suat thap thi nhap vao so 0.\n') HL=input(' Nhap 0 hay 1:'); while isempty(HL)|(HL~=1&HL~=0) HL=input('chon chiet suat dau la cao "1" hay thap "0"'); end switch n1 case 1 n1=1.42; case 2 n1=2.12; case 3 n1=1.45; case 4 n1=2.38; end switch n2 case 1 n2=1.42; case 2 n2=2.12; case 3 n2=1.45; case 4 n2=2.38; end cs=zeros(1,solop); lopmin=input('Nhap do day nho nhat :(>1nm):'); while isempty(lopmin)|lopmin<=1 lopmin=input('Nhap do day nho nhat :(>1nm):'); end lopmax=input('Nhap do day lon nhat:(>lop nho nhat va <1000000):'); while isempty(lopmax)|lopmax>=1000000|lopmax<=lopmin lopmax=input('Nhap do day lon nhat :(>lop nho nhat va <1000000):'); end bn=input('Nhap vao buoc nhay (bn<lop lon nhat-lop cao nhat):'); while isempty(bn)|bn>=(lopmax-lopmin) bn=input('Nhap vao buoc nhay (bn<lop lon nhat-lop cao nhat):'); end Ro=input('Nhap do phan xa cua mang ma ban mong muon dat duoc:'); while isempty(Ro)||Ro<=0 fprintf('BAN DA NHAP SAI XIN VUI LONG NHAP LAI GIA TRI\n'); Ro=input('Nhap do phan xa cua mang ma ban mong muon dat duoc:'); end cs=zeros(1,solop); if xor(n2>n1,HL==1)==0 for i=1:solop if (-1)^i==-1 cs(i)=n2; else cs(i)=n1; end end else for i=1:solop if (-1)^i==-1 cs(i)=n1; else cs(i)=n2; end end end bs=[lamdamin:lamdamax]; s=lamdamax-lamdamin+1; sobit=ceil(log2((lopmax-lopmin)/bn+1)); g=(lopmax-lopmin)/(2^sobit+1); for i=1:100 for i1=1:2 cathe(i,:)=randint(1,solop*sobit); end end for j=1:solop beday(1,j)=lopmin+g*decimal(cathe(1,(sobit*(j-1)+1):(sobit*j))); end R=taomang(beday,solop,cs,bs); hold off plot(bs,R) hold on for i=1:100 cathe(3,:)=randint(1,solop*sobit); for j=1:solop beday(1,j)=lopmin+g*decimal(cathe(1,(sobit*(j-1)+1):(sobit*j))); beday(2,j)=lopmin+g*decimal(cathe(2,(sobit*(j-1)+1):(sobit*j))); beday(3,j)=lopmin+g*decimal(cathe(3,(sobit*(j-1)+1):(sobit*j))); end for j=1:3 b=beday(j,:); R=taomang(b,solop,cs,bs); f(j)=0; for k=1:s f(j)=f(j)+(Ro-R(k))^2; end f(j)=sqrt(f(j)/s); end if f(1)==max(f) cathe(1,:)=cathe(3,:); elseif f(2)==max(f) cathe(2,:)=cathe(3,:); end end for i=1:50 for j=1:solop beday(1,j)=lopmin+g*decimal(cathe(1,(sobit*(j-1)+1):(sobit*j))); beday(2,j)=lopmin+g*decimal(cathe(2,(sobit*(j-1)+1):(sobit*j))); z1=ceil((sobit-1)*rand); cathe(4*j-1,:)=cathe(1,:); cathe(4*j-1,(sobit*(j-1)+1):(sobit*(j-1)+z1))=cathe(2,(sobit*(j-1)+1): (sobit*(j-1)+z1)); cathe(4*j,:)=cathe(2,:); cathe(4*j,(sobit*(j-1)+1):(sobit*(j-1)+z1))=cathe(1,(sobit*(j-1)+1): (sobit*(j-1)+z1)); cathe(4*j+1,:)=cathe(1,:); cathe(4*j+1,(sobit*(j-1)+1+z1):(sobit*j))=cathe(2,(sobit*(j-1)+1+z1): (sobit*j)); cathe(4*j+2,:)=cathe(2,:); cathe(4*j+2,(sobit*(j-1)+1+z1):(sobit*j))=cathe(1,(sobit*(j-1)+1+z1): (sobit*j)); for k=1:solop beday(4*j-1,k)=lopmin+g*decimal(cathe(4*j-1,(sobit*(k-1)+1): (sobit*k))); beday(4*j,k)=lopmin+g*decimal(cathe(4*j,(sobit*(k-1)+1): (sobit*k))); beday(4*j+1,k)=lopmin+g*decimal(cathe(4*j+1,(sobit*(k-1)+1): (sobit*k))); beday(4*j+2,k)=lopmin+g*decimal(cathe(4*j+2,(sobit*(k-1)+1): (sobit*k))); end end for j=1:(solop*4+2) b=beday(j,:); R=taomang(b,solop,cs,bs); f(j)=0; for k=1:s f(j)=f(j)+(Ro-R(k))^2; end f(j)=sqrt(f(j)/s); end [f1 a1]=min(f(1:2:(solop*4+1))); [f2 a2]=min(f(2:2:solop*4)); cathe(1,:)=cathe(2*a1-1,:); cathe(2,:)=cathe(2*a2,:); if rem(i,11)==0 for j=3:2:(4*solop+1) z1=ceil(solop*sobit*rand); cathe(j,:)=cathe(1,:); cathe(j+1,:)=cathe(2,:); switch cathe(1,z1) case 0 cathe(j,z1)=1; case 1 cathe(j,z1)=0; end switch cathe(2,z1) case 0 cathe(j+1,z1)=1; case 1 cathe(j+1,z1)=0; end for k=1:solop beday(j,k)=lopmin+g*decimal(cathe(j,(sobit*(k-1)+1): (sobit*k))); end end for j=1:(4*solop+2) b=beday(j,:); R=taomang(b,solop,cs,bs); f(j)=0; for k=1:s f(j)=f(j)+(Ro-R(k))^2; end f(j)=sqrt(f(j)/s); end [f1 a1]=min(f(1:2:(solop*4+1))); [f2 a2]=min(f(2:2:solop*4)); cathe(1,:)=cathe(2*a1-1,:); cathe(2,:)=cathe(2*a2,:); end f end for j=1:solop beday(1,j)=lopmin+g*decimal(cathe(1,(sobit*(j-1)+1):(sobit*j))); end R=taomang(beday(1,:),solop,cs,bs); plot(bs,R,' ') %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function b=decimal(a) b=0; c=length(a); for i=1:c b=b+a(i)*2^(c-i); end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function R=taomang(beday,solop,cs,bs) s=length(bs); for q=1:s a=eye(2); for k=1:solop sigma=2*pi*cs(k)*beday(k)/bs(q); a=a*[cos(sigma) sqrt(-1)*sin(sigma)/cs(k);sqrt(-1)*cs(k)*sin(sigma) cos(sigma)]; end a=a*[1;1.52]; tiso=(1.52*a(1)-a(2))/(1.52*a(1)+a(2)); tiso=abs(tiso); R(q)=tiso^2; end . beday(1,j)=lopmin+g*decimal(cathe(1,(sobit*(j-1)+1):(sobit*j))); beday(2,j)=lopmin+g*decimal(cathe(2,(sobit*(j-1)+1):(sobit*j))); z1=ceil((sobit-1)*rand); cathe(4*j-1,:)=cathe(1,:); cathe(4*j-1,(sobit*(j-1)+1):(sobit*(j-1)+z1))=cathe(2,(sobit*(j-1)+1): (sobit*(j-1)+z1));. cathe(4*j-1,(sobit*(j-1)+1):(sobit*(j-1)+z1))=cathe(2,(sobit*(j-1)+1): (sobit*(j-1)+z1)); cathe(4*j,:)=cathe(2,:); cathe(4*j,(sobit*(j-1)+1):(sobit*(j-1)+z1))=cathe(1,(sobit*(j-1)+1): (sobit*(j-1)+z1)); cathe(4*j+1,:)=cathe(1,:); . for i=1:solop if (-1 )^i= =-1 cs(i)=n2; else cs(i)=n1; end end else for i=1:solop if (-1 )^i= =-1 cs(i)=n1; else cs(i)=n2; end end end bs=[lamdamin:lamdamax]; s=lamdamax-lamdamin+1; sobit=ceil(log2((lopmax-lopmin)/bn+1)); g=(lopmax-lopmin)/(2^sobit+1); for