资源描述:
《算法设计与分析大作业答案》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、算法设计技术与方法大作业学院电子工程学院专业电路与系统姓名学号导师姓名作业1.分别实现多项式求值的四种运算,若针对不同规模的输入值a,各算法的运行时间,问题规模n分别取10,50,100,150,200,300,400,500,10000,20000,50000,100000时绘制四种算法运行时间的比较图。2.分别实现矩阵相乘的3种算法,比较三种算法在矩阵大小分别为,,,,,,,,,,时的运行时间与MATLAB自带的矩阵相乘的运行时间,绘制时间对比图。3.利用遗传算法求解下面的问题:1、分析题意可知,该题要用四种不同的方法实现对多项式的求值计算,每种方
2、法取从10-100000不同的规模。本文采用了以下方法进行求值:直接代入法和递归法。而其中递归法分三类不同思路进行递归:①;②,,;③。本文对上述四种方法进行了编程,具体代码如下:程序1.1文件名poly.m%主程序:实现不同规模下多项式求值的四种运算clc;closeall;clearall;n=[1050100150200300400500100002000050000100000];x=2;fori=1:12a=rand(1,(n(i)+1));%产生多项式,最高次幂为n(i)+1tic;p1(i)=polyval(a,x);%直接代入法t1(i
3、)=toc;tic;p2(i)=0;forj=1:(n(i)+1)p2(i)=p2(i)+a(j)*x^(j-1);%递归法1endt2(i)=toc;tic;p3(i)=0;q=1;forj=2:(n(i)+1)q=q*x;p3(i)=p3(i)+a(j)*q;%递归法2endt3(i)=toc;tic;p4(i)=0;forj=1:n(i);p4(i)=x*p4(i)+a(n(i)+1-j);%递归法3endt4(i)=toc;endfigure(1);subplot(2,2,1);h=semilogx(n,t1);%这里不能用plot,横轴需要取
4、对数,下同set(h,'linestyle','-','linewidth',1.8,'marker','*','color','g','markersize',6);xlabel('Thescaleoftheproblem:n');ylabel('timeforfirstmethod(s)');title('therelationshipbetweentimeandscale');gridon;subplot(2,2,2);h=semilogx(n,t2);set(h,'linestyle','-','linewidth',1.8,'marker',
5、'*','color','b','markersize',6);xlabel('Thescaleoftheproblem:n');ylabel('timeforsecondmethod(s)');title('therelationshipbetweentimeandscale');gridon;subplot(2,2,3);h=semilogx(n,t2);set(h,'linestyle','-','linewidth',1.8,'marker','*','color','k','markersize',6);xlabel('Thescaleoft
6、heproblem:n');ylabel('timeforthirdmethod(s)');title('therelationshipbetweentimeandscale');gridon;subplot(2,2,4);h=semilogx(n,t2);set(h,'linestyle','-','linewidth',1.8,'marker','*','color','r','markersize',6);xlabel('Thescaleoftheproblem:n');ylabel('timeforforthmethod(s)');title(
7、'therelationshipbetweentimeandscale');gridon;figure(2);g=semilogx(n,t1,'g+',n,t2,'bx',n,t3,'k*',n,t4,'ro');legend('thefirstmethod','thesecondmethod','thethirdmethod','theforthmethod');set(g,'linestyle','-','linewidth',2.0,'markersize',8);xlabel('n=10,50,100,150,200,300,400,500,1
8、0000,20000,50000,100000');ylabel('time');title(