80 likes | 286 Views
MATLAB 程式設計 Learning Arrays and x-y Plotting. 方煒 台大生機系. Ex4_2 xy Plots, Labels, and Titles. dx=.01; x=.5*dx:dx:10-0.5*dx; y=sin(5*x); plot(x,y, ’ r- ’ ); nhalf=ceil(length(x)/2); plot(x(1:nhalf),y(1:nhalf), ’ b- ’ ) plot(x(nhalf:end),y(nhalf:end), ’ b- ’ ) xlabel( ’ theta ’ )
E N D
Ex4_2 xy Plots, Labels, and Titles • dx=.01; • x=.5*dx:dx:10-0.5*dx; • y=sin(5*x); • plot(x,y,’r-’); • nhalf=ceil(length(x)/2); • plot(x(1:nhalf),y(1:nhalf),’b-’) • plot(x(nhalf:end),y(nhalf:end),’b-’) • xlabel(’\theta’) • ylabel(’F(\theta)’) • title(’F(\theta)=sin(5 \theta)’) • s=sprintf(’F(\\theta)=sin(%i \\theta)’,5) • title(s)
Ex4_3 Overlaying Plots • close • y2=cos(x); % the second function • % plot both • plot(x,y,’r-’,x,y2,’b-’) • close all; • plot(x,y,’r-’) • hold on • plot(x,y2,’b-’) • Hold off
Ex4_4 xyz Plots: Curves in 3-D Space • clear;close all; • dphi=pi/100; % set the spacing in azimuthal angle • N=30; % set the number of azimuthal trips • phi=0:dphi:N*2*pi; • theta=phi/N/2; % go from north to south once • r=1; % sphere of radius 1 • % convert spherical to Cartesian • x=r*sin(theta).*cos(phi); • y=r*sin(theta).*sin(phi); • z=r*cos(theta); • % plot the spiral • plot3(x,y,z,’b-’) • axis equal
Ex4_5 Logarithmic Plots • x=0:.1:8; • y=exp(x); • semilogx(x,y); • title(’Semilogx’) • pause • semilogy(x,y); • title(’Semilogy’) • pause • loglog(x,y); • title(’Loglog’)
Ex4_6 Generating Multiple Plots • x=0:.01:20; • f1=sin(x); • f2=cos(x)./(1+x.^2); • figure • plot(x,f1) • figure • plot(x,f2)
Ex4_7 Controlling the Axes • close all; • x=.01:.01:20; • y=cos(x)./x; • plot(x,y) • axis([0 25 -5 5]) • plot(x,y) • xlim([ 0 25]) • plot(x,y) • ylim([-5 5]) • axis equal
Ex4_8 Greek Letters, Subscripts, and Superscripts • \alpha \beta \gamma \delta \epsilon \phi • \theta \kappa \lambda \mu \nu \pi • \rho \sigma \tau \xi \zeta • Θ1 is coded with \theta_1 • Θ12 is coded with \theta {12} • Θ10is coded with \theta ^{10} • text(10,.5,’Hi’);