资源描述:
《matlab经典的小程序.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、Num2str变数值为字符串Int2str变整数为字符串Str2num变字符串为数值Sprintf变数值为格式控制下的字符串Sscanf变字符串为格式控制下的数值1.分段函数forx=-pi*6:pi/10:6*piy=sin(x);ify<0y=0;endfprintf('x=%f,y=%f',x,y);end2.计算距离x1=input('enterthex1:');y1=input('enterthey1:');x2=input('enterthex2:');y2=input('enterthey2:');d=sqrt((x1-x2).^2+(y1-y
2、2).^2);fprintf('thedistanceofthetwopointsis:%f',d);v0=input('Enterthe初速度v0:');h0=input('Enterthe离地高度h0:');t=0:0.01:53.重力加速度h=-0.5*9.81*t.^2+v0*t+h0;v=-9.81*t+v0;plot(t,h,'k-',t,v,'b--');title('Plotofh(t)andv(t)');xlabel('t');ylabel('h(t)andv(t)');legend('h(t)','v(t)');4.坐标转换function[
3、x,y]=polar2rect(r,theta)x=r*cos(theta*pi/180);y=r*sin(theta*pi/180);5.坐标转换,带参数检验function[mag,angle]=polar_value(x,y)msg=nargchk(1,2,nargin);error(msg);%Iftheyargumentismissing,setitto0.ifnargin<2y=0;end%Checkfor(0,0)inputargument,andprintout%awarningmessage.ifx==0&y==0msg='Bothxandya
4、rezero:angleismeaningless!';warning(msg);end%Nowcalculatethemagnitudemag=sqrt(x.^2+y.^2);%Ifthesecondoutputargumentispresent,calculate%angleindegreesifnargout==2angle=atan2(y,x)*180/pi;end6.三维螺旋线t=0:pi/50:10*pi;plot3(sin(t),cos(t),t)gridonaxissquare7.subplotfigure(1);subplot(2,1,1);x
5、=-pi:pi/20:pi;y=sin(x);plot(x,y);title('Subplot1title');subplot(2,1,2);x=-pi:pi/20:pi;y=cos(x);plot(x,y);title('Subplot2title');8.Figurefigure(1);x=x:0.05:2;y1=exp(x);plot(x,y1);figure(2);y2=exp(-x);plot(x,y2);9.保持x=-pi:pi/20:pi;y1=sin(x);y2=cos(x);plot(x,y1,'b-');holdon;plot(x,y2,'k
6、--');holdoff;legend('sinx','cosx');10.极坐标绘图g=0.5;theta=0:pi/20:2*pi;gain=2*g*(1+cos(theta));%Plotgainpolar(theta,gain,'r-');title('Gainversusangleittheta');11.色彩控制x=0:pi/15:4*pi;y=exp(2*sin(x));plot(x,y,'-ko','LineWidth',3.0,'Markersize',6,...'MarkerEdgeColor','r','MarkerFaceColor'
7、,'g');