270 likes | 285 Views
Learn how to create arrays and plot X-Y graphs in MATLAB programming. This tutorial covers topics such as plotting curves, adding labels and titles, and overlaying multiple plots.
E N D
Ex4_2a xy Plots, Labels, and Titles • dx=.01; • x=.5*dx:dx:10-0.5*dx; • y=sin(5*x); • plot(x,y,’r-’); • xlabel(’\theta’); • ylabel(’F(\theta)’); • title(’F(\theta)=sin(5 \theta)’);
Ex4_2b • dx=.01; • x=.5*dx:dx:10-0.5*dx; • y=sin(5*x); • nhalf=ceil(length(x)/2); • plot(x(1:nhalf),y(1:nhalf),’b-’); • hold on • plot(x(nhalf:end),y(nhalf:end),’k-’); • xlabel(’\theta’); • ylabel(’F(\theta)’); • s=sprintf(’F(\\theta)=sin(%i \\theta)’,5); • title(s);
Ex4_3 Overlaying Plots • dx=.01; x=.5*dx:dx:10-0.5*dx; • y=sin(5*x); y2=cos(x); • % plot both • plot(x,y,'r-',x,y2,'b-') • dx=.01; x=.5*dx:dx:10-0.5*dx; • y=sin(5*x); y2=cos(x); • plot(x,y,’r-’) • hold on • plot(x,y2,’b-’) • Hold off
Ex4_4a 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 N=30 N=10
Ex4_4b • function ex4_4b(N) • close all; • % N is the number of azimuthal trips • dphi=pi/100; % set the spacing in azimuthal angle • 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; • title(['N= ',int2str(N)]); Ex4_4b(15) Ex4_4b(5)
Ex4_5 Logarithmic Plots • x=0:.1:8; • y=exp(x); • semilogx(x,y); • title(’Semilogx for y=exp(x)’) • pause • semilogy(x,y); • title(’Semilogy for y=exp(x)’) • pause • loglog(x,y); • title(’Loglog for y=exp(x)’)
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]) • xlim([ 0 20]); • ylim([-5 5]);
Ex4_8 • close all; • x=.01:.01:20; • y=cos(x)./x; • plot(x,y); • axis([0 25 -5 5]); • str1='\alpha \beta \gamma \delta \epsilon \phi '; • str2='\theta \kappa \lambda \mu \nu \pi '; • str3='\rho \sigma \tau \xi \zeta'; • xlabel([str1 str2 str3]); • text(10,1,'\theta_1'); • text(10,2,'\theta_{12}'); • text(10,3,'\theta^{10}');
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}