matlab:如何用matlab对立体声进行分段,插入,删除等编辑,能从鼠标处进行播放?求代码,各
MATLAB\u7f16\u9057\u4f20\u7b97\u6cd5\u6e90\u7a0b\u5e8f\u9057\u4f20\u7b97\u6cd5\u5b9e\u4f8b:
\u4e5f\u662f\u81ea\u5df1\u627e\u6765\u7684\uff0c\u539f\u4ee3\u7801\u6709\u5c11\u8bb8\u9519\u8bef\uff0c\u672c\u4eba\u90fd\u5df2\u66f4\u6b63\u4e86\uff0c\u8c03\u8bd5\u8fd0\u884c\u90fd\u901a\u8fc7\u4e86\u7684\u3002
\u5bf9\u4e8e\u521d\u5b66\u8005\uff0c\u5c24\u5176\u662f\u8fd8\u6ca1\u6709\u7f16\u7a0b\u7ecf\u9a8c\u7684\u975e\u5e38\u6709\u7528\u7684\u4e00\u4e2a\u6587\u4ef6
\u9057\u4f20\u7b97\u6cd5\u5b9e\u4f8b
% \u4e0b\u9762\u4e3e\u4f8b\u8bf4\u660e\u9057\u4f20\u7b97\u6cd5 %
% \u6c42\u4e0b\u5217\u51fd\u6570\u7684\u6700\u5927\u503c %
% f(x)=10*sin(5x)+7*cos(4x) x\u2208[0,10] %
% \u5c06 x \u7684\u503c\u7528\u4e00\u4e2a10\u4f4d\u7684\u4e8c\u503c\u5f62\u5f0f\u8868\u793a\u4e3a\u4e8c\u503c\u95ee\u9898\uff0c\u4e00\u4e2a10\u4f4d\u7684\u4e8c\u503c\u6570\u63d0\u4f9b\u7684\u5206\u8fa8\u7387\u662f\u6bcf\u4e3a (10-0)/(2^10-1)\u22480.01 \u3002 %
% \u5c06\u53d8\u91cf\u57df [0,10] \u79bb\u6563\u5316\u4e3a\u4e8c\u503c\u57df [0,1023], x=0+10*b/1023, \u5176\u4e2d b \u662f [0,1023] \u4e2d\u7684\u4e00\u4e2a\u4e8c\u503c\u6570\u3002 %
% %
%--------------------------------------------------------------------------------------------------------------%
%--------------------------------------------------------------------------------------------------------------%
% \u7f16\u7a0b
%-----------------------------------------------
% 2.1\u521d\u59cb\u5316(\u7f16\u7801)
% initpop.m\u51fd\u6570\u7684\u529f\u80fd\u662f\u5b9e\u73b0\u7fa4\u4f53\u7684\u521d\u59cb\u5316\uff0cpopsize\u8868\u793a\u7fa4\u4f53\u7684\u5927\u5c0f\uff0cchromlength\u8868\u793a\u67d3\u8272\u4f53\u7684\u957f\u5ea6(\u4e8c\u503c\u6570\u7684\u957f\u5ea6)\uff0c
% \u957f\u5ea6\u5927\u5c0f\u53d6\u51b3\u4e8e\u53d8\u91cf\u7684\u4e8c\u8fdb\u5236\u7f16\u7801\u7684\u957f\u5ea6(\u5728\u672c\u4f8b\u4e2d\u53d610\u4f4d)\u3002
%\u9057\u4f20\u7b97\u6cd5\u5b50\u7a0b\u5e8f
%Name: initpop.m
%\u521d\u59cb\u5316
function pop=initpop(popsize,chromlength)
pop=round(rand(popsize,chromlength)); % rand\u968f\u673a\u4ea7\u751f\u6bcf\u4e2a\u5355\u5143\u4e3a {0,1} \u884c\u6570\u4e3apopsize\uff0c\u5217\u6570\u4e3achromlength\u7684\u77e9\u9635\uff0c
% roud\u5bf9\u77e9\u9635\u7684\u6bcf\u4e2a\u5355\u5143\u8fdb\u884c\u5706\u6574\u3002\u8fd9\u6837\u4ea7\u751f\u7684\u521d\u59cb\u79cd\u7fa4\u3002
% 2.2 \u8ba1\u7b97\u76ee\u6807\u51fd\u6570\u503c
% 2.2.1 \u5c06\u4e8c\u8fdb\u5236\u6570\u8f6c\u5316\u4e3a\u5341\u8fdb\u5236\u6570(1)
%\u9057\u4f20\u7b97\u6cd5\u5b50\u7a0b\u5e8f
%Name: decodebinary.m
%\u4ea7\u751f [2^n 2^(n-1) ... 1] \u7684\u884c\u5411\u91cf\uff0c\u7136\u540e\u6c42\u548c\uff0c\u5c06\u4e8c\u8fdb\u5236\u8f6c\u5316\u4e3a\u5341\u8fdb\u5236
function pop2=decodebinary(pop)
[px,py]=size(pop); %\u6c42pop\u884c\u548c\u5217\u6570
for i=1:py
pop1(:,i)=2.^(py-i).*pop(:,i);
end
pop2=sum(pop1,2); %\u6c42pop1\u7684\u6bcf\u884c\u4e4b\u548c
% 2.2.2 \u5c06\u4e8c\u8fdb\u5236\u7f16\u7801\u8f6c\u5316\u4e3a\u5341\u8fdb\u5236\u6570(2)
% decodechrom.m\u51fd\u6570\u7684\u529f\u80fd\u662f\u5c06\u67d3\u8272\u4f53(\u6216\u4e8c\u8fdb\u5236\u7f16\u7801)\u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\uff0c\u53c2\u6570spoint\u8868\u793a\u5f85\u89e3\u7801\u7684\u4e8c\u8fdb\u5236\u4e32\u7684\u8d77\u59cb\u4f4d\u7f6e
% (\u5bf9\u4e8e\u591a\u4e2a\u53d8\u91cf\u800c\u8a00\uff0c\u5982\u6709\u4e24\u4e2a\u53d8\u91cf\uff0c\u91c7\u752820\u4e3a\u8868\u793a\uff0c\u6bcf\u4e2a\u53d8\u91cf10\u4e3a\uff0c\u5219\u7b2c\u4e00\u4e2a\u53d8\u91cf\u4ece1\u5f00\u59cb\uff0c\u53e6\u4e00\u4e2a\u53d8\u91cf\u4ece11\u5f00\u59cb\u3002\u672c\u4f8b\u4e3a1)\uff0c
% \u53c2\u65701ength\u8868\u793a\u6240\u622a\u53d6\u7684\u957f\u5ea6\uff08\u672c\u4f8b\u4e3a10\uff09\u3002
%\u9057\u4f20\u7b97\u6cd5\u5b50\u7a0b\u5e8f
%Name: decodechrom.m
%\u5c06\u4e8c\u8fdb\u5236\u7f16\u7801\u8f6c\u6362\u6210\u5341\u8fdb\u5236
function pop2=decodechrom(pop,spoint,length)
pop1=pop(:,spoint:spoint+length-1);
pop2=decodebinary(pop1);
% 2.2.3 \u8ba1\u7b97\u76ee\u6807\u51fd\u6570\u503c
% calobjvalue.m\u51fd\u6570\u7684\u529f\u80fd\u662f\u5b9e\u73b0\u76ee\u6807\u51fd\u6570\u7684\u8ba1\u7b97\uff0c\u5176\u516c\u5f0f\u91c7\u7528\u672c\u6587\u793a\u4f8b\u4eff\u771f\uff0c\u53ef\u6839\u636e\u4e0d\u540c\u4f18\u5316\u95ee\u9898\u4e88\u4ee5\u4fee\u6539\u3002
%\u9057\u4f20\u7b97\u6cd5\u5b50\u7a0b\u5e8f
%Name: calobjvalue.m
%\u5b9e\u73b0\u76ee\u6807\u51fd\u6570\u7684\u8ba1\u7b97
function [objvalue]=calobjvalue(pop)
temp1=decodechrom(pop,1,10); %\u5c06pop\u6bcf\u884c\u8f6c\u5316\u6210\u5341\u8fdb\u5236\u6570
x=temp1*10/1023; %\u5c06\u4e8c\u503c\u57df \u4e2d\u7684\u6570\u8f6c\u5316\u4e3a\u53d8\u91cf\u57df \u7684\u6570
objvalue=10*sin(5*x)+7*cos(4*x); %\u8ba1\u7b97\u76ee\u6807\u51fd\u6570\u503c
% 2.3 \u8ba1\u7b97\u4e2a\u4f53\u7684\u9002\u5e94\u503c
%\u9057\u4f20\u7b97\u6cd5\u5b50\u7a0b\u5e8f
%Name:calfitvalue.m
%\u8ba1\u7b97\u4e2a\u4f53\u7684\u9002\u5e94\u503c
function fitvalue=calfitvalue(objvalue)
global Cmin;
Cmin=0;
[px,py]=size(objvalue);
for i=1:px
if objvalue(i)+Cmin>0
temp=Cmin+objvalue(i);
else
temp=0.0;
end
fitvalue(i)=temp;
end
fitvalue=fitvalue';
% 2.4 \u9009\u62e9\u590d\u5236
% \u9009\u62e9\u6216\u590d\u5236\u64cd\u4f5c\u662f\u51b3\u5b9a\u54ea\u4e9b\u4e2a\u4f53\u53ef\u4ee5\u8fdb\u5165\u4e0b\u4e00\u4ee3\u3002\u7a0b\u5e8f\u4e2d\u91c7\u7528\u8d4c\u8f6e\u76d8\u9009\u62e9\u6cd5\u9009\u62e9\uff0c\u8fd9\u79cd\u65b9\u6cd5\u8f83\u6613\u5b9e\u73b0\u3002
% \u6839\u636e\u65b9\u7a0b pi=fi/\u2211fi=fi/fsum \uff0c\u9009\u62e9\u6b65\u9aa4\uff1a
% 1\uff09 \u5728\u7b2c t \u4ee3\uff0c\u7531\uff081\uff09\u5f0f\u8ba1\u7b97 fsum \u548c pi
% 2\uff09 \u4ea7\u751f {0,1} \u7684\u968f\u673a\u6570 rand( .)\uff0c\u6c42 s=rand( .)*fsum
% 3\uff09 \u6c42 \u2211fi\u2265s \u4e2d\u6700\u5c0f\u7684 k \uff0c\u5219\u7b2c k \u4e2a\u4e2a\u4f53\u88ab\u9009\u4e2d
% 4\uff09 \u8fdb\u884c N \u6b212\uff09\u30013\uff09\u64cd\u4f5c\uff0c\u5f97\u5230 N \u4e2a\u4e2a\u4f53\uff0c\u6210\u4e3a\u7b2c t=t+1 \u4ee3\u79cd\u7fa4
%\u9057\u4f20\u7b97\u6cd5\u5b50\u7a0b\u5e8f
%Name: selection.m
%\u9009\u62e9\u590d\u5236
function [newpop]=selection(pop,fitvalue)
totalfit=sum(fitvalue); %\u6c42\u9002\u5e94\u503c\u4e4b\u548c
fitvalue=fitvalue/totalfit; %\u5355\u4e2a\u4e2a\u4f53\u88ab\u9009\u62e9\u7684\u6982\u7387
fitvalue=cumsum(fitvalue); %\u5982 fitvalue=[1 2 3 4]\uff0c\u5219 cumsum(fitvalue)=[1 3 6 10]
[px,py]=size(pop);
ms=sort(rand(px,1)); %\u4ece\u5c0f\u5230\u5927\u6392\u5217
fitin=1;
newin=1;
while newin<=px
if(ms(newin))<fitvalue(fitin)
newpop(newin)=pop(fitin);
newin=newin+1;
else
fitin=fitin+1;
end
end
% 2.5 \u4ea4\u53c9
% \u4ea4\u53c9(crossover)\uff0c\u7fa4\u4f53\u4e2d\u7684\u6bcf\u4e2a\u4e2a\u4f53\u4e4b\u95f4\u90fd\u4ee5\u4e00\u5b9a\u7684\u6982\u7387 pc \u4ea4\u53c9\uff0c\u5373\u4e24\u4e2a\u4e2a\u4f53\u4ece\u5404\u81ea\u5b57\u7b26\u4e32\u7684\u67d0\u4e00\u4f4d\u7f6e
% \uff08\u4e00\u822c\u662f\u968f\u673a\u786e\u5b9a\uff09\u5f00\u59cb\u4e92\u76f8\u4ea4\u6362\uff0c\u8fd9\u7c7b\u4f3c\u751f\u7269\u8fdb\u5316\u8fc7\u7a0b\u4e2d\u7684\u57fa\u56e0\u5206\u88c2\u4e0e\u91cd\u7ec4\u3002\u4f8b\u5982\uff0c\u5047\u8bbe2\u4e2a\u7236\u4ee3\u4e2a\u4f53x1\uff0cx2\u4e3a\uff1a
% x1=0100110
% x2=1010001
% \u4ece\u6bcf\u4e2a\u4e2a\u4f53\u7684\u7b2c3\u4f4d\u5f00\u59cb\u4ea4\u53c9\uff0c\u4ea4\u53c8\u540e\u5f97\u52302\u4e2a\u65b0\u7684\u5b50\u4ee3\u4e2a\u4f53y1\uff0cy2\u5206\u522b\u4e3a\uff1a
% y1\uff1d0100001
% y2\uff1d1010110
% \u8fd9\u68372\u4e2a\u5b50\u4ee3\u4e2a\u4f53\u5c31\u5206\u522b\u5177\u6709\u4e862\u4e2a\u7236\u4ee3\u4e2a\u4f53\u7684\u67d0\u4e9b\u7279\u5f81\u3002\u5229\u7528\u4ea4\u53c8\u6211\u4eec\u6709\u53ef\u80fd\u7531\u7236\u4ee3\u4e2a\u4f53\u5728\u5b50\u4ee3\u7ec4\u5408\u6210\u5177\u6709\u66f4\u9ad8\u9002\u5408\u5ea6\u7684\u4e2a\u4f53\u3002
% \u4e8b\u5b9e\u4e0a\u4ea4\u53c8\u662f\u9057\u4f20\u7b97\u6cd5\u533a\u522b\u4e8e\u5176\u5b83\u4f20\u7edf\u4f18\u5316\u65b9\u6cd5\u7684\u4e3b\u8981\u7279\u70b9\u4e4b\u4e00\u3002
%\u9057\u4f20\u7b97\u6cd5\u5b50\u7a0b\u5e8f
%Name: crossover.m
%\u4ea4\u53c9
function [newpop]=crossover(pop,pc)
[px,py]=size(pop);
newpop=ones(size(pop));
for i=1:2:px-1
if(rand<pc)
cpoint=round(rand*py);
newpop(i,:)=[pop(i,1:cpoint),pop(i+1,cpoint+1:py)];
newpop(i+1,:)=[pop(i+1,1:cpoint),pop(i,cpoint+1:py)];
else
newpop(i,:)=pop(i);
newpop(i+1,:)=pop(i+1);
end
end
% 2.6 \u53d8\u5f02
% \u53d8\u5f02(mutation)\uff0c\u57fa\u56e0\u7684\u7a81\u53d8\u666e\u904d\u5b58\u5728\u4e8e\u751f\u7269\u7684\u8fdb\u5316\u8fc7\u7a0b\u4e2d\u3002\u53d8\u5f02\u662f\u6307\u7236\u4ee3\u4e2d\u7684\u6bcf\u4e2a\u4e2a\u4f53\u7684\u6bcf\u4e00\u4f4d\u90fd\u4ee5\u6982\u7387 pm \u7ffb\u8f6c\uff0c\u5373\u7531\u201c1\u201d\u53d8\u4e3a\u201c0\u201d\uff0c
% \u6216\u7531\u201c0\u201d\u53d8\u4e3a\u201c1\u201d\u3002\u9057\u4f20\u7b97\u6cd5\u7684\u53d8\u5f02\u7279\u6027\u53ef\u4ee5\u4f7f\u6c42\u89e3\u8fc7\u7a0b\u968f\u673a\u5730\u641c\u7d22\u5230\u89e3\u53ef\u80fd\u5b58\u5728\u7684\u6574\u4e2a\u7a7a\u95f4\uff0c\u56e0\u6b64\u53ef\u4ee5\u5728\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u6c42\u5f97\u5168\u5c40\u6700\u4f18\u89e3\u3002
%\u9057\u4f20\u7b97\u6cd5\u5b50\u7a0b\u5e8f
%Name: mutation.m
%\u53d8\u5f02
function [newpop]=mutation(pop,pm)
[px,py]=size(pop);
newpop=ones(size(pop));
for i=1:px
if(rand<pm)
mpoint=round(rand*py);
if mpoint<=0
mpoint=1;
end
newpop(i)=pop(i);
if any(newpop(i,mpoint))==0
newpop(i,mpoint)=1;
else
newpop(i,mpoint)=0;
end
else
newpop(i)=pop(i);
end
end
% 2.7 \u6c42\u51fa\u7fa4\u4f53\u4e2d\u6700\u5927\u5f97\u9002\u5e94\u503c\u53ca\u5176\u4e2a\u4f53
%\u9057\u4f20\u7b97\u6cd5\u5b50\u7a0b\u5e8f
%Name: best.m
%\u6c42\u51fa\u7fa4\u4f53\u4e2d\u9002\u5e94\u503c\u6700\u5927\u7684\u503c
function [bestindividual,bestfit]=best(pop,fitvalue)
[px,py]=size(pop);
bestindividual=pop(1,:);
bestfit=fitvalue(1);
for i=2:px
if fitvalue(i)>bestfit
bestindividual=pop(i,:);
bestfit=fitvalue(i);
end
end
% 2.8 \u4e3b\u7a0b\u5e8f
%\u9057\u4f20\u7b97\u6cd5\u4e3b\u7a0b\u5e8f
%Name:genmain05.m
clear
clf
popsize=20; %\u7fa4\u4f53\u5927\u5c0f
chromlength=10; %\u5b57\u7b26\u4e32\u957f\u5ea6\uff08\u4e2a\u4f53\u957f\u5ea6\uff09
pc=0.6; %\u4ea4\u53c9\u6982\u7387
pm=0.001; %\u53d8\u5f02\u6982\u7387
pop=initpop(popsize,chromlength); %\u968f\u673a\u4ea7\u751f\u521d\u59cb\u7fa4\u4f53
for i=1:20 %20\u4e3a\u8fed\u4ee3\u6b21\u6570
[objvalue]=calobjvalue(pop); %\u8ba1\u7b97\u76ee\u6807\u51fd\u6570
fitvalue=calfitvalue(objvalue); %\u8ba1\u7b97\u7fa4\u4f53\u4e2d\u6bcf\u4e2a\u4e2a\u4f53\u7684\u9002\u5e94\u5ea6
[newpop]=selection(pop,fitvalue); %\u590d\u5236
[newpop]=crossover(pop,pc); %\u4ea4\u53c9
[newpop]=mutation(pop,pc); %\u53d8\u5f02
[bestindividual,bestfit]=best(pop,fitvalue); %\u6c42\u51fa\u7fa4\u4f53\u4e2d\u9002\u5e94\u503c\u6700\u5927\u7684\u4e2a\u4f53\u53ca\u5176\u9002\u5e94\u503c
y(i)=max(bestfit);
n(i)=i;
pop5=bestindividual;
x(i)=decodechrom(pop5,1,chromlength)*10/1023;
pop=newpop;
end
fplot('10*sin(5*x)+7*cos(4*x)',[0 10])
hold on
plot(x,y,'r*')
hold off
[z index]=max(y); %\u8ba1\u7b97\u6700\u5927\u503c\u53ca\u5176\u4f4d\u7f6e
x5=x(index)%\u8ba1\u7b97\u6700\u5927\u503c\u5bf9\u5e94\u7684x\u503c
y=z
\u3010\u95ee\u9898\u3011\u6c42f(x)=x 10*sin(5x) 7*cos(4x)\u7684\u6700\u5927\u503c\uff0c\u5176\u4e2d0<=x<=9
\u3010\u5206\u6790\u3011\u9009\u62e9\u4e8c\u8fdb\u5236\u7f16\u7801\uff0c\u79cd\u7fa4\u4e2d\u7684\u4e2a\u4f53\u6570\u76ee\u4e3a10\uff0c\u4e8c\u8fdb\u5236\u7f16\u7801\u957f\u5ea6\u4e3a20\uff0c\u4ea4\u53c9\u6982\u7387\u4e3a0.95,\u53d8\u5f02\u6982\u7387\u4e3a0.08
\u3010\u7a0b\u5e8f\u6e05\u5355\u3011
%\u7f16\u5199\u76ee\u6807\u51fd\u6570
function[sol,eval]=fitness(sol,options)
x=sol(1);
eval=x 10*sin(5*x) 7*cos(4*x);
%\u628a\u4e0a\u8ff0\u51fd\u6570\u5b58\u50a8\u4e3afitness.m\u6587\u4ef6\u5e76\u653e\u5728\u5de5\u4f5c\u76ee\u5f55\u4e0b
initPop=initializega(10,[0 9],'fitness');%\u751f\u6210\u521d\u59cb\u79cd\u7fa4\uff0c\u5927\u5c0f\u4e3a10
[x endPop,bPop,trace]=ga([0 9],'fitness',[],initPop,[1e-6 1 1],'maxGenTerm',25,'normGeomSelect',...
[0.08],['arithXover'],[2],'nonUnifMutation',[2 25 3]) %25\u6b21\u9057\u4f20\u8fed\u4ee3
\u8fd0\u7b97\u501f\u8fc7\u4e3a\uff1ax =
7.8562 24.8553(\u5f53x\u4e3a7.8562\u65f6\uff0cf\uff08x\uff09\u53d6\u6700\u5927\u503c24.8553)
\u6ce8\uff1a\u9057\u4f20\u7b97\u6cd5\u4e00\u822c\u7528\u6765\u53d6\u5f97\u8fd1\u4f3c\u6700\u4f18\u89e3\uff0c\u800c\u4e0d\u662f\u6700\u4f18\u89e3\u3002
\u9057\u4f20\u7b97\u6cd5\u5b9e\u4f8b2
\u3010\u95ee\u9898\u3011\u5728\uff0d5<=Xi<=5,i=1,2\u533a\u95f4\u5185\uff0c\u6c42\u89e3
f(x1,x2)=-20*exp(-0.2*sqrt(0.5*(x1.^2 x2.^2)))-exp(0.5*(cos(2*pi*x1) cos(2*pi*x2))) 22.71282\u7684\u6700\u5c0f\u503c\u3002
\u3010\u5206\u6790\u3011\u79cd\u7fa4\u5927\u5c0f10\uff0c\u6700\u5927\u4ee3\u65701000\uff0c\u53d8\u5f02\u73870.1,\u4ea4\u53c9\u73870.3
\u3010\u7a0b\u5e8f\u6e05\u5355\u3011
\uff05\u6e90\u51fd\u6570\u7684matlab\u4ee3\u7801
function [eval]=f(sol)
numv=size(sol,2);
x=sol(1:numv);
eval=-20*exp(-0.2*sqrt(sum(x.^2)/numv)))-exp(sum(cos(2*pi*x))/numv) 22.71282;
%\u9002\u5e94\u5ea6\u51fd\u6570\u7684matlab\u4ee3\u7801
function [sol,eval]=fitness(sol,options)
numv=size(sol,2)-1;
x=sol(1:numv);
eval=f(x);
eval=-eval;
%\u9057\u4f20\u7b97\u6cd5\u7684matlab\u4ee3\u7801
bounds=ones(2,1)*[-5 5];
[p,endPop,bestSols,trace]=ga(bounds,'fitness')
\u6ce8\uff1a\u524d\u4e24\u4e2a\u6587\u4ef6\u5b58\u50a8\u4e3am\u6587\u4ef6\u5e76\u653e\u5728\u5de5\u4f5c\u76ee\u5f55\u4e0b\uff0c\u8fd0\u884c\u7ed3\u679c\u4e3a
p =
0.0000 -0.0000 0.0055
\u5927\u5bb6\u53ef\u4ee5\u76f4\u63a5\u7ed8\u51faf(x)\u7684\u56fe\u5f62\u6765\u5927\u6982\u770b\u770bf\uff08x\uff09\u7684\u6700\u503c\u662f\u591a\u5c11\uff0c\u4e5f\u53ef\u662f\u4f7f\u7528\u4f18\u5316\u51fd\u6570\u6765\u9a8c\u8bc1\u3002matlab\u547d\u4ee4\u884c\u6267\u884c\u547d\u4ee4\uff1a
fplot('x 10*sin(5*x) 7*cos(4*x)',[0,9])
evalops\u662f\u4f20\u9012\u7ed9\u9002\u5e94\u5ea6\u51fd\u6570\u7684\u53c2\u6570\uff0copts\u662f\u4e8c\u8fdb\u5236\u7f16\u7801\u7684\u7cbe\u5ea6\uff0ctermops\u662f\u9009\u62e9maxGenTerm\u7ed3\u675f\u51fd\u6570\u65f6\u4f20\u9012\u4e2amaxGenTerm\u7684\u53c2\u6570\uff0c\u5373\u9057\u4f20\u4ee3\u6570\u3002xoverops\u662f\u4f20\u9012\u7ed9\u4ea4\u53c9\u51fd\u6570\u7684\u53c2\u6570\u3002mutops\u662f\u4f20\u9012\u7ed9\u53d8\u5f02\u51fd\u6570\u7684\u53c2\u6570\u3002
\u3010\u95ee\u9898\u3011\u6c42f(x)=x+10*sin(5x)+7*cos(4x)\u7684\u6700\u5927\u503c\uff0c\u5176\u4e2d0<=x<=9
\u3010\u5206\u6790\u3011\u9009\u62e9\u4e8c\u8fdb\u5236\u7f16\u7801\uff0c\u79cd\u7fa4\u4e2d\u7684\u4e2a\u4f53\u6570\u76ee\u4e3a10\uff0c\u4e8c\u8fdb\u5236\u7f16\u7801\u957f\u5ea6\u4e3a20\uff0c\u4ea4\u53c9\u6982\u7387\u4e3a0.95,\u53d8\u5f02\u6982\u7387\u4e3a0.08
\u3010\u7a0b\u5e8f\u6e05\u5355\u3011
%\u7f16\u5199\u76ee\u6807\u51fd\u6570
function[sol,eval]=fitness(sol,options)
x=sol(1);
eval=x+10*sin(5*x)+7*cos(4*x);
%\u628a\u4e0a\u8ff0\u51fd\u6570\u5b58\u50a8\u4e3afitness.m\u6587\u4ef6\u5e76\u653e\u5728\u5de5\u4f5c\u76ee\u5f55\u4e0b
initPop=initializega(10,[0 9],'fitness');%\u751f\u6210\u521d\u59cb\u79cd\u7fa4\uff0c\u5927\u5c0f\u4e3a10
[x endPop,bPop,trace]=ga([0 9],'fitness',[],initPop,[1e-6 1 1],'maxGenTerm',25,'normGeomSelect',...
[0.08],['arithXover'],[2],'nonUnifMutation',[2 25 3]) %25\u6b21\u9057\u4f20\u8fed\u4ee3
\u8fd0\u7b97\u501f\u8fc7\u4e3a\uff1ax =
7.8562 24.8553(\u5f53x\u4e3a7.8562\u65f6\uff0cf\uff08x\uff09\u53d6\u6700\u5927\u503c24.8553)
\u6ce8\uff1a\u9057\u4f20\u7b97\u6cd5\u4e00\u822c\u7528\u6765\u53d6\u5f97\u8fd1\u4f3c\u6700\u4f18\u89e3\uff0c\u800c\u4e0d\u662f\u6700\u4f18\u89e3\u3002
\u9057\u4f20\u7b97\u6cd5\u5b9e\u4f8b2
\u3010\u95ee\u9898\u3011\u5728\uff0d5<=Xi<=5,i=1,2\u533a\u95f4\u5185\uff0c\u6c42\u89e3
f(x1,x2)=-20*exp(-0.2*sqrt(0.5*(x1.^2+x2.^2)))-exp(0.5*(cos(2*pi*x1)+cos(2*pi*x2)))+22.71282\u7684\u6700\u5c0f\u503c\u3002
\u3010\u5206\u6790\u3011\u79cd\u7fa4\u5927\u5c0f10\uff0c\u6700\u5927\u4ee3\u65701000\uff0c\u53d8\u5f02\u73870.1,\u4ea4\u53c9\u73870.3
\u3010\u7a0b\u5e8f\u6e05\u5355\u3011
\uff05\u6e90\u51fd\u6570\u7684matlab\u4ee3\u7801
function [eval]=f(sol)
numv=size(sol,2);
x=sol(1:numv);
eval=-20*exp(-0.2*sqrt(sum(x.^2)/numv)))-exp(sum(cos(2*pi*x))/numv)+22.71282;
%\u9002\u5e94\u5ea6\u51fd\u6570\u7684matlab\u4ee3\u7801
function [sol,eval]=fitness(sol,options)
numv=size(sol,2)-1;
x=sol(1:numv);
eval=f(x);
eval=-eval;
%\u9057\u4f20\u7b97\u6cd5\u7684matlab\u4ee3\u7801
bounds=ones(2,1)*[-5 5];
[p,endPop,bestSols,trace]=ga(bounds,'fitness')
\u6ce8\uff1a\u524d\u4e24\u4e2a\u6587\u4ef6\u5b58\u50a8\u4e3am\u6587\u4ef6\u5e76\u653e\u5728\u5de5\u4f5c\u76ee\u5f55\u4e0b\uff0c\u8fd0\u884c\u7ed3\u679c\u4e3a
p =
0.0000 -0.0000 0.0055
\u5927\u5bb6\u53ef\u4ee5\u76f4\u63a5\u7ed8\u51faf(x)\u7684\u56fe\u5f62\u6765\u5927\u6982\u770b\u770bf\uff08x\uff09\u7684\u6700\u503c\u662f\u591a\u5c11\uff0c\u4e5f\u53ef\u662f\u4f7f\u7528\u4f18\u5316\u51fd\u6570\u6765\u9a8c\u8bc1\u3002matlab\u547d\u4ee4\u884c\u6267\u884c\u547d\u4ee4\uff1a
fplot('x+10*sin(5*x)+7*cos(4*x)',[0,9])
evalops\u662f\u4f20\u9012\u7ed9\u9002\u5e94\u5ea6\u51fd\u6570\u7684\u53c2\u6570\uff0copts\u662f\u4e8c\u8fdb\u5236\u7f16\u7801\u7684\u7cbe\u5ea6\uff0ctermops\u662f\u9009\u62e9maxGenTerm\u7ed3\u675f\u51fd\u6570\u65f6\u4f20\u9012\u4e2amaxGenTerm\u7684\u53c2\u6570\uff0c\u5373\u9057\u4f20\u4ee3\u6570\u3002xoverops\u662f\u4f20\u9012\u7ed9\u4ea4\u53c9\u51fd\u6570\u7684\u53c2\u6570\u3002mutops\u662f\u4f20\u9012\u7ed9\u53d8\u5f02\u51fd\u6570\u7684\u53c2\u6570\u3002
\u6211\u7406\u89e3\u662f\u8fd9\u6837\u5b50\u7684\uff1a
clc;clear
t=-1:0.001:1;
f=(250/3*sin(t+pi/3)).*(t=0);
plot(t,f,'.');
{
mqd_t mqid = mq_open("/test", O_RDONLY);
if (mqid == -1)
err_exit("mq_open error");
struct Student buf;
int nrcv;
unsigned prio;
struct mq_attr attr;
if (mq_getattr(mqid, &attr) == -1)
err_exit("mq_getattr error");
if ((nrcv = mq_receive(mqid, (char *)&buf, attr.mq_msgsize, &prio)) == -1)
err_exit("mq_receive error");
cout << "receive " << nrcv << " bytes, priority: " << prio << ", name: "
<< buf.name << ", age: " << buf.age << endl;
mq_close(mqid);
return 0;
}
绛旓細MATLA鏄疢ATrixLABoratory鐨勭缉鍐欙紝鏄竴娆剧敱缇庡浗The MathWorks鍏徃鍑哄搧鐨勫晢涓氭暟瀛﹁蒋浠躲MATLAB杞欢鏄竴绉嶇敤浜庣畻娉曞紑鍙戙佹暟鎹彲瑙嗗寲銆佹暟鎹垎鏋愪互鍙婃暟鍊艰绠楃殑璁$畻鏈鸿蒋浠躲侻ATLAB杞欢闄ょ煩闃佃繍绠楋紝缁樺埗鍑芥暟锛屾暟鎹浘鍍忕瓑甯哥敤鍔熻兘澶栵紝MATLAB杞欢杩樺彲浠ョ敤鏉ュ垱寤虹敤鎴风晫闈㈠強涓庤皟鐢ㄥ叾瀹冭瑷缂栧啓鐨勭▼搴忋侻ATLAB杞欢涓昏鐢ㄤ簬鏁板艰繍绠楋紝...
绛旓細MATLAB7.0鍦╳in8.1涓婂畨瑁呴棶棰樿В鍐虫柟娉曞涓嬶細鍦ㄧ▼搴忎笂鐐瑰嚮鍙抽敭閫夋嫨鈥滃睘鎬р濓紝鍏煎鎬ц缃噷閫夋嫨鈥淲indows Vista(Service Pack1)鈥溿matla7.0鍦╳in8涓婂畨瑁呰繃绋嬪涓嬶細1.棣栧厛鎵撳紑瀹夎鍖咃紝杩愯setup.exe鏂囦欢锛2.缃戜笂鎼滀竴涓敞鍐岀爜锛岄殢渚胯緭涓猲ame鍜宑ompany锛岀偣鍑讳笅涓姝ワ紱3.鐐瑰嚮yes涓鐩村埌瀹夎finish锛屽畨瑁呭畬鎴...
绛旓細涓よ呭湪鍥惧舰鍖栫晫闈㈠拰鏄撲簬璋冭瘯鐩告瘮锛宻imulink鏇寸畝鍗曘1銆佸浘褰㈠寲鐣岄潰锛歴imulink閲囩敤鍥惧舰鍖栫晫闈紝鐢ㄦ埛鍙互閫氳繃鎷栨嫿妯″潡銆佺粯鍒舵鍥剧瓑鏂瑰紡杞绘澗鎼缓鍜屼慨鏀圭郴缁熸ā鍨嬨傝matla鐣岄潰鐩稿澶嶆潅锛屾搷浣滆捣鏉ヨ緝涓哄洶闅俱2銆佹槗浜庤皟璇曪細simulink浼氳嚜鍔ㄦ鏌ユā鍨嬩腑鏄惁瀛樺湪閿欒锛屾搷浣滆捣鏉ユ洿绠鍗曘傝宮atla闇瑕佽嚜琛岃皟璇曪紝鎿嶄綔璧锋潵杈冧负澶嶆潅銆
绛旓細matlab涓鐨勬嫙鍚堝垎涓哄椤瑰紡鎷熷悎涓庨潪绾挎ф嫙鍚堜袱绫 浣犳墍璇寸殑璺熸嫙鍚堟鏁版寕閽╃殑搴旇鏄椤瑰紡鎷熷悎锛宮atlab涓殑鍛戒护鏄痯olyfit 鑷充簬楠岃瘉姝ゆ嫙鍚堝嚱鏁版槸鍚﹀悎閫傦紝鍙互閫氳繃娈嬪樊鏉ュ垽鏂紝娈嬪樊灏辨槸鎷熷悎鏇茬嚎涓婄殑鐐逛笌鍘熸暟鎹偣鐨勫樊 浠ヤ笅鏄竴娈典唬鐮侊細t=[0 0.3 0.8 1.1 1.6 2.3]';y=[0.5 0.82 1.14 1.25 1...
绛旓細閲囩敤浜嗛,浣犲彲浠ヤ綋浼氫笅.寤虹珛control.m鐨凪鏂囦欢 function y=control(x)if x==1 y=1;else y=x*control(x-1);end 鐒跺悗鍦ㄥ懡浠ょ獥鍙d腑杈撳叆 >> control(5)ans = 120 杩欐牱灏卞彲浠鐢∕ATLAB瀹炵幇閫掑綊,涓昏鐨勮繕鏄▼搴忔濇兂,濡傛灉鎬濇兂瀵逛簡,鐢ㄧ▼搴忓疄鐜拌嚜宸辩殑鎯虫硶閭e氨鍙互浜 ...
绛旓細鐢ㄦ瀬鍊煎嚱鏁癴minsearch锛堬級姹傚緱锛屽叾鏋佸皬鍊笺倄 = 1.99999078443153 4.00001086059484 鍗 x=1.99999078443153锛泍=4.00001086059484 fval = -18.9999999969252 鏋佸皬鍊,z=18.9999999969252 鍦ㄥ懡浠ょ獥鍙d笅鎵ц涓嬪垪浠g爜锛歝lc,clear all,close all syms x fun=@(x)(x(1)^4-8*x(1).*x(2)+2.*x(...
绛旓細鍒欏彲浠ョ敾鍑哄閲嶇嚎銆傚彟涓绉嶇敾娉曟槸鍒╃敤hold鍛戒护銆傚湪宸茬粡鐢诲ソ鐨勫浘褰笂锛岃嫢璁剧疆hold on锛MATLA灏嗘妸鏂扮殑plot鍛戒护浜х敓鐨勫浘褰㈢敾鍦ㄥ師鏉ョ殑鍥惧舰涓娿傝屽懡浠old off 灏嗙粨鏉熻繖涓繃绋嬨備緥濡傦細>> x=linspace(0,2*pi,30); y=sin(x); plot(x,y)>> hold on >> z=cos(x); plot(x,z)>> hold off 绾...
绛旓細MATLAB锛岃繖鏄竴涓笓涓哄伐绋嬭绠楀拰缂栫▼璁捐鐨勮蒋浠讹紝鍏跺彂闊充负[mætlæb]銆傚畠涓昏鐢ㄤ簬杩涜鍚勭鏁板艰绠楀拰鏁版嵁鍒嗘瀽锛岃骞挎硾搴旂敤浜庢暀瀛﹀拰瀹為檯椤圭洰涓傞氳繃MATLAB鐨宸ュ叿绠憋紝鐢ㄦ埛鍙互鏂逛究鍦扮紪鍐欏拰鎵ц澶嶆潅鐨勭畻娉曪紝杩涜浠跨湡鍜屾ā鍨嬫瀯寤恒備竴涓叿浣撶殑渚嬪瓙鏄"Single-Phase Active Power Filter and Its ...
绛旓細棰樹富鐨勯棶棰樺氨鏄凡鐭銆亂鏁版嵁锛屾牴鎹ā鍨嬶紝鎷熷悎鍏禷銆乥銆乧绯绘暟銆傛绫婚棶棰樻眰瑙h繃绋嬶細1銆佸凡鐭ユ暟鎹 x=[銆傘傘俔y=[銆傘傘俔2銆佸畾涔夋嫙鍚堝嚱鏁 fun=@(p,x)p(1)*exp(x +p(2))+p(3) %a鈥攑(1)锛宐鈥攑(2)锛宑鈥攑(3)3銆佽瀹歛銆乥銆乧鐨勫垵濮嬪 p0=[0,0,0]4銆鍒╃敤nlinfit锛堬級闈炵嚎鎬у洖褰...
绛旓細杩欓噷鐨勭偣涔樹笉鏄唬琛ㄦ寚鏁帮紝鎸囨暟鏄痎 鐐逛箻浠h〃鍏冪礌绉紝鍗虫妸鐩镐箻鐨勪袱涓暟鐪嬫垚鍚戦噺锛岀劧鍚庢瘡涓悜閲忎腑瀵瑰簲鐨勫厓绱犻愪釜鐩镐箻銆傛瘮濡傝繖涓涓matlab鐢熸垚鐨勬槸涓涓叧浜巟鐨勫悜閲忓拰涓涓叧浜巗inx鐨勫悜閲忥紝浣犲簲璇ュ仛鍚戦噺涔樻硶锛屾墍浠ョ敤鐐逛箻銆傛爣閲忕殑璇濆氨鐢