算法设计与分析大作业答案.doc

上传人:s****u 文档编号:12809797 上传时间:2020-05-25 格式:DOC 页数:11 大小:903KB
返回 下载 相关 举报
算法设计与分析大作业答案.doc_第1页
第1页 / 共11页
算法设计与分析大作业答案.doc_第2页
第2页 / 共11页
算法设计与分析大作业答案.doc_第3页
第3页 / 共11页
点击查看更多>>
资源描述
算法设计技术与方法大作业 学 院 电子工程学院 专 业 电路与系统 姓 名 学 号 导师姓名 作业1.分别实现多项式求值的四种运算,若针对不同规模的输入值a,各算法的运行时间,问题规模n分别取10,50,100,150,200,300,400,500,10000,20000,50000,100000时绘制四种算法运行时间的比较图。2. 分别实现矩阵相乘的3种算法,比较三种算法在矩阵大小分别为,时的运行时间与MATLAB自带的矩阵相乘的运行时间,绘制时间对比图。3. 利用遗传算法求解下面的问题:1、 分析题意可知,该题要用四种不同的方法实现对多项式的求值计算,每种方法取从10-100000不同的规模。本文采用了以下方法进行求值:直接代入法和递归法。而其中递归法分三类不同思路进行递归: ; ,; 。本文对上述四种方法进行了编程,具体代码如下:程序1.1文件名poly.m% 主程序:实现不同规模下多项式求值的四种运算clc;close all;clear all;n=10 50 100 150 200 300 400 500 10000 20000 50000 100000;x=2;for i=1:12 a=rand(1,(n(i)+1); % 产生多项式,最高次幂为n(i)+1 tic; p1(i)=polyval(a,x); % 直接代入法 t1(i)=toc; tic; p2(i)=0; for j=1:(n(i)+1) p2(i)=p2(i)+a(j)*x(j-1); % 递归法1 end t2(i)=toc; tic; p3(i)=0; q=1; for j=2:(n(i)+1) q=q*x; p3(i)=p3(i)+a(j)*q; % 递归法2 end t3(i)=toc; tic; p4(i)=0; for j=1:n(i); p4(i)=x*p4(i)+a(n(i)+1-j); % 递归法3 end t4(i)=toc;endfigure(1);subplot(2,2,1);h=semilogx(n,t1); % 这里不能用plot,横轴需要取对数,下同set(h,linestyle,-,linewidth,1.8,marker,*,color,g,markersize,6);xlabel(The scale of the problem:n);ylabel(time for first method(s);title(the relationship between time and scale);grid on;subplot(2,2,2);h=semilogx(n,t2); set(h,linestyle,-,linewidth,1.8,marker,*,color,b,markersize,6);xlabel(The scale of the problem:n);ylabel(time for second method(s);title(the relationship between time and scale);grid on;subplot(2,2,3);h=semilogx(n,t2); set(h,linestyle,-,linewidth,1.8,marker,*,color,k,markersize,6);xlabel(The scale of the problem:n);ylabel(time for third method(s);title(the relationship between time and scale);grid on;subplot(2,2,4);h=semilogx(n,t2); set(h,linestyle,-,linewidth,1.8,marker,*,color,r,markersize,6);xlabel(The scale of the problem:n);ylabel(time for forth method(s);title(the relationship between time and scale);grid on;figure(2);g=semilogx(n,t1,g+,n,t2,bx,n,t3,k*,n,t4,ro);legend(the first method,the second method,the third method,the forth method);set(g,linestyle,-,linewidth,2.0,markersize,8);xlabel(n=10, 50, 100, 150, 200, 300, 400, 500, 10000, 20000, 50000, 100000);ylabel(time);title(The comparison chart of four different methods for polyval);grid on;运行结果如下:图 1.1 四种方法所用时间随规模不同而变化的结果图图 2.2 四种方法所用时间随规模不同而变化的对比图 由理论分析可知,四种算法的时间复杂度分别为、,由图1.2分析可知,直接带入计算和递归法所用时间相差无几,这与理论分析一直。而第三种方法与第四种方法的差异可能是由于每次加法所用时间与每次乘法所用时间不同所导致。 另外,在问题规模较小(n12.1|(x(2)5.8) f=inf;elseif(x(1)-3.0|x(2)=bestv bestv=fmax; % 到目前为止最优适应度值 bvalxx=bval(indmax,:); % 到目前为止最佳位串 optxx=xx(indmax,:); % 到目前为止最优参数 end Bestfit(k)=bestv; % 记录每代的最优适应度 for i=1:(N-1) r=rand; tmp=find(r=q); newbval(i,:)=bval(tmp(1),:); end newbval(N,:)=bvalxx; % 最优保留 bval=newbval; for i=1:2:(N-1) cc=rand; if ccpc point=ceil(rand*(2*L-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); end end bval(N,:)=bvalxx; % 最优保留 mm=rand(N,2*L)pm; bval(mm)=1-bval(mm);endplot(Bestfit); % 绘制最优适应度进化曲线xlabel(Generation); ylabel(FitnessValue);title(The relationship between FitnessValue and Generation);Optxxbestv 运行结果如下:optxx = 11.627663734115348 5.525806451612903bestv = 38.639864218199662 从而可知原函数在(11.627663734115348,5.525806451612903)取得最大值38.639864218199662。
展开阅读全文
相关资源
相关搜索

当前位置:首页 > 图纸专区 > 考试试卷


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!