《MATLAB的基本操作实验报告.doc》由会员分享,可在线阅读,更多相关《MATLAB的基本操作实验报告.doc(12页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、1. Plot the following graph(1) Plotting the two curves under one coordinate system with different color代码:clear;x=-pi:pi/1000:pi;y=sin(x);plot(x,y,r:);hold on;x=-pi:pi/1000:pi;y=sin(3*x);plot(x,y,b);hold off;xlabel(x轴); ylabel(y轴);legend(y=sinx,y=sin(3x);运行结果:(2) Plotting the standard normal distrib
2、ution density function:代码:clear;x=-5:0.05:5;y=1/sqrt(2*pi)*exp(-x.2/2);plot(x,y)运行结果:(3)Using subplot command to plot 6 curves :代码:clear;x=-5*pi:0.05:5*pi;for i=1:6 subplot(2,3,i); y=sin(i*x); plot(x,y);end运行结果:(4)Plotting the curve: 代码:clear;x=-2*pi:0.01:2*pi;y=sin(2*x);polar(x,y)运行结果:(5)Plotting a
3、 series of curves under one polar coordinate system:代码:clear;x=-2*pi:0.01:2*pi;for a=1:2:5 y=a.*cos(2*x); polar(x,y); hold on;end运行结果:2Plotting the 3-D curves: 代码: cleart=-12*pi:0.05:12*pi;x=t.*sin(t);y=t.*cos(t);z=t;plot3(x,y,z)运行结果3. Plotting the surface: (mesh surf contour3) (1)代码:clearX=-5:0.01:
4、5;Y=X;x,y=meshgrid(X,Y);z=x.*y;mesh(x,y,z)运行结果:(2)* 代码:clear;close;X=-1:0.01:1;Y=X;x,y=meshgrid(X,Y);z=x.2/3+y.2/2;mesh(x,y,z);pausesurf(x,y,z);pausecontour3(x,y,z);pausemesh(x,y,z);运行结果:(3)*代码:clearX=-12:0.01:12;Y=X;x,y=meshgrid(X,Y);z=sin(sqrt(x.2+y.2)./sqrt(x.2+y.2);mesh(x,y,z)运行结果: (4) 代码: clear
5、;X=-12:0.05:12;Y=X;x,y=meshgrid(X,Y);z=0*x+0*y+6;mesh(x,y,z)运行结果:(5) 代码:clear;X=-5:0.05:5;Y=X;x,y=meshgrid(X,Y);z=cos(y).*sin(x);mesh(x,y,z)运行结果: 4.Plotting curves under a polar coordinate system:(1)*代码:clear;x=-10:0.01:10;y=5*cos(x)+4;polar(x,y)运行结果:(2) 代码:clear;x=-1:0.01:1;y=5./cos(x)-7;polar(x,y)
6、运行结果:5 (1)Write a function M-file to define the function:代码:function y=eg6_1a(t) if t=0 y=-3*t.2+5; else y=3*t.2+5; end (2)Plot the graph of y(t) using the function defined above. Where 代码:clear;t=-9:0.5:9;n=length(t);for i=1:n y(i)=eg6_1a(t(i);endplot(t,y)运行结果:6. Write function M-files to calculate
7、 (1)n! (2)Assume the name of the function is macl_fun and the variable is n.(1)代码: function y=fun(n); x=1:n; y=prod(n)(2)代码:function y=mal_fun(m,n)if n=round(n)|m=round(m)|mn beep; error(incorrect input please input a positive number);else matrix=1:m; factorM=prod(matrix); matrix=1:n; factorN=prod(m
8、atrix); matrix=1:n-m; factor=prod(matrix); y=factorN/(factorM*factor);end7. Write a function to generate a random integer matrix between 1 and 100.代码:function y=randomInteger()y=fix(rand()*100);8. Write a m-file to perform:a) input one of the numbers of 1,2,3,4 from keyboardb) input two numbers from
9、 the keyboard, say x,yc) do addition, subtraction, multiplication or division respectively if your corresponding choice in (1) is 1,2,3 or 4.d) display the output in this format:x=y=x+y= or x-y= so forth代码:clear choice=input(input one of the numbers of 1,2,3,4 from keyboard)matrix=input(input two nu
10、mber from keyboard);x=matrix(1);y=matrix(2);disp(strcat(x=,num2str(x);disp(strcat(y=,num2str(y);if choice=1 disp(strcat(x+y=,num2str(x+y);elseif choice=2 disp(strcat(x-y=,num2str(x-y);elseif choice=3 disp(strcat(x/y=,num2str(x/y);else disp(strcat(x*y=,num2str(x*y);end9. Write a function to plot a series of graphs Pn(x) and :代码:function y=fun(n,xmin,xmax)x=xmin:0.2 xmax;m=length(x);for j=1:m sum=1;an=1; for i=1:n an=an*x(j)/i; sum=sum+an;end y2(j)=sum;endy1=exp(x);plot(x,y1,r.,x,y2,bo);