maltab graphics

Upload: oloyede-isiaka

Post on 04-Apr-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/29/2019 maltab graphics

    1/16

    [Year]

    Research Embassy

    Oloyede

    [MATLAB GRAPHICS]

  • 7/29/2019 maltab graphics

    2/16

    2

    * * * * * * * being the TEXT presented at the workshop organized by the

    Department of Industrial and Production Engineering, University of Ibadan.

    Nigeria.

    Facilitator: Oloyede I,

    Department of Industrial and Production Engineering,

    University of Ibadan. Nigeria. Contact:08053049890

    All Praises, Adoration, Holiness,

    Greeting are due to none but GOD

    who taught MEN by PEN, who

    taught MEN what he knows not. The

    Omnipotent, The Omniscience.

  • 7/29/2019 maltab graphics

    3/16

    3

    Contents:

    Plot

    Surface

    Contour

    Cylinder

    Sphere

  • 7/29/2019 maltab graphics

    4/16

    4

    GRAPHS

    clear; % clear all variables from memoryclose all; % close all figure windowsh=input('Enter the step-size h - ') ;x=0:h:20; % build an array of points [0,h,2h,...,20]

    f=exp(-x/6).*cos(x); % build the array [f(0),f(h),...f(20)]plot(x,f)fprintf(' Plot completed, h = %g \n',h)

    clear;close all;dphi=pi/100; % set the spacing in azimuthal angleN=30; % set the number of azimuthal tripsphi=0:dphi:N*2*pi;theta=phi/N/2; % go from north to south oncer=1; % sphere of radius 1% convert spherical to Cartesianx=r*sin(theta).*cos(phi);y=r*sin(theta).*sin(phi);z=r*cos(theta);% plot the spiralplot3(x,y,z,'b-')axis equal

    Surface, Contour, and Vector Field Plots

    Meshgrid and NdgridMatlab displays functions of the type f(x, y), either by making a contour plot (like atopographic map) or by displaying the function as height above the xy plane like aperspective drawing. Start by defining arrays x and y that span the region that you wantto plot, then create the function f(x, y) over the plane, and finally either use contour, surf,ormesh.%begin plots

    -0.5

    0

    0.5

    1

    -0.5

    0

    0.5

    -1

    -0.5

    0

    0.5

    1

  • 7/29/2019 maltab graphics

    5/16

    5

    %*********************************************************% Define the arrays x and y% Warning: dont make the step size too small or you will% kill the system%*********************************************************clear;close all;x=-1:.1:1;y=0:.1:1.5;%*********************************************************% Use meshgrid to convert these 1-d arrays into 2-d matrices of% x and y values over the plane%*********************************************************[X,Y]=meshgrid(x,y);%*********************************************************% Get f(x,y) by using f(X,Y). Note the use of .* with X and Y% rather than with x and y%*********************************************************

    f=(2-cos(pi*X)).*exp(Y);%*********************************************************% Note that this function is uphill in y between x=-1 and x=1

    % and has a valley at x=0%*********************************************************surf(X,Y,f);%end plots

    Matlab has another command called ndgridwhich is similar to meshgridbut does theconversion to two dimensions the other way round. The plots look the same, but f(i, j)is now in the f(x, y) order:

    x=-1:.1:1;y=0:.1:1.5;[X,Y]=ndgrid(x,y);f=(2-cos(pi*X)).*exp(Y);surf(X,Y,f);length(x)length(y)size(f)

    -1

    -0.5

    0

    0.5

    1

    0

    0.5

    1

    1.50

    5

    10

    15

  • 7/29/2019 maltab graphics

    6/16

    6

    Contour Plots and Surface Plots

    close all;%begin contour%*********************************************************% make a contour plot by asking Matlab to evenly space N contours

    % between the minimum of f(x,y) and the maximum f(x,y) (the default)%*********************************************************N=40;contour(X,Y,f,N);title('Contour Plot')xlabel('x')ylabel('y')pause

    %*********************************************************% You can also tell Matlab which contour levels you want to plot.% To do so load an array (V in this case) with the values of the

    % levels you want to see. In this example they will start at% the minimum of f, end at the maximum of f, and there will% be 21 contours.%% You can even print contour labels on the plot, which is% a big help, by assigning the plot to a variable name% and using the clabel command, as shown below. Only% every other contour is labeled in this example%*********************************************************close all;top=max(max(f)); % find the max and min of fbottom=min(min(f));dv=(top-bottom)/20; % interval for 21 equally spaced contoursV=bottom:dv:top;cs=contour(X,Y,f,V);clabel(cs,V(1:2:21)) % give clabel the name of the plot and% an array of the contours to labeltitle(Contour Plot)xlabel(x)ylabel(y)pause%*********************************************************% Now make a surface plot of the function with the viewing% point rotated by AZ degrees from the x-axis and% elevated by EL degrees above the xy plane%*********************************************************close all;

    surf(X,Y,f); % or you can use mesh(X,Y,f) to make a wire plotAZ=30;EL=45;view(AZ,EL);title(Surface Plot)xlabel(x)ylabel(y)pause%*********************************************************% If you want to manually change the viewing angle of% a surface plot, click on the circular arrow icon

  • 7/29/2019 maltab graphics

    7/16

    7

    % on the figure window, then click and move the% pointer on the graph. Try it until you get the% hang of it.%% Heres a piece of code that lets you fly around the% surface plot by continually changing the viewing angles% and using the pause command; I think youll be impressed%*********************************************************close all;surf(X,Y,f);title(Surface Plot)xlabel(x)ylabel(y)EL=45;for m=1:100AZ=30+m/100*360;view(AZ,EL);pause(.1); % pause units are in secondsendpause

    %*********************************************************% This same trick will let you make animations of% both xy and surface plots. To make this surface% oscillate up and down like a manta ray you could% do this.%*********************************************************dt=.1;for m=1:100t=m*dt;g=f*cos(t);surf(X,Y,g);AZ=30;EL=45;view(AZ,EL);title(Surface Plot)xlabel(x)ylabel(y)axis([-1 1 -1 1 min(min(f)) max(max(f))])pause(.1)end

    for n=1:.2:5n10 = 10*n;x = linspace(-2,2,n10);y = x./(1+x.^2);plot(x,y,'r')title(sprintf('Graph %g. Plot based upon n = %g points.'..., (n+1)/2, n10))

    axis([-2,2,-.8,.8])xlabel('x')ylabel('y')gridpause(3)end

  • 7/29/2019 maltab graphics

    8/16

    8

    close all;clear;% Curve r(t) = < t*cos(t), t*sin(t), t >.t = -10*pi:pi/100:10*pi;x = t.*cos(t);

    y = t.*sin(t);h = plot3(x,y,t);set(h,'LineWidth',1.25)title('Curve u(t) = < t*cos(t), t*sin(t), t >')h = get(gca,'Title');set(h,'FontSize',12)xlabel('x')h = get(gca,'xlabel');set(h,'FontSize',12)ylabel('y')h = get(gca,'ylabel');set(h,'FontSize',12)zlabel('z')h = get(gca,'zlabel');set(h,'FontSize',12)grid

    % Surface plot of the hyperbolic paraboloid z = y^2 - x^2

    x = -1:0.05:1;y = x;[xi, yi] = meshgrid(x,y);zi = yi.^2 - xi.^2;mesh(xi, yi, zi)axis offsurf(xi,yi,zi);

    x = -1:.05:1;

    y = x;

    [xi,yi] = meshgrid(x,y);

    zi = yi.^2 - xi.^2;

    surfc(xi,yi,zi)

    -1

    -0.5

    0

    0.5

    1

    -1

    -0.5

    0

    0.5

    1-1

    -0.5

    0

    0.5

    1

  • 7/29/2019 maltab graphics

    9/16

    9

    colormap copper

    shading interp

    view([25,15,20])

    grid off

    title('Hyperbolic paraboloid z = y^2 x^2')

    h = get(gca,'Title');

    set(h,'FontSize',12)

    xlabel('x')

    h = get(gca,'xlabel');

    set(h,'FontSize',12)

    ylabel('y')

    h = get(gca,'ylabel');

    set(h,'FontSize',12)

    zlabel('z')

    h = get(gca,'zlabel');

    set(h,'FontSize',12)

    pause(5)

    figure

    contourf(zi), hold on, shading flat

    [c,h] = contour(zi,'k-'); clabel(c,h)

    title('The level curves of z = y^2 - x^2.')h = get(gca,'Title');

    set(h,'FontSize',12)

    xlabel('x')

    h = get(gca,'xlabel');

    set(h,'FontSize',12)

    ylabel('y')

    h = get(gca,'ylabel');

    set(h,'FontSize',12)

    -1

    -0.5

    0

    0.5

    1

    -1

    -0.50

    0.51

    -1

    -0.5

    0

    0.5

    1

    x

    Hyperbolic paraboloid z = y2

    x2

    y

    z

  • 7/29/2019 maltab graphics

    10/16

    10

    Sphere and cylinder

    MATLAB has some functions for generating special surfaces. We will be concerned mostly with twofunctions- sphere and cylinder.The command sphere(n) generates a unit sphere with center at the origin using (n+1)2points. If functionsphere is called without the input parameter, MATLAB uses the default value n = 20. You can translatethe center of the sphere easily. In the following example we will plot graph of the unit sphere with center

    at (2, -1, 1)[x,y,z] = sphere(30);

    surf(x+2, y-1, z+1)

    cylinder([1 0])title('Unit cone')

    -0

    .8 -0.8

    -0.6

    -0.6

    -0.6

    -0.6

    -0.4

    -0

    .4

    -0.4

    -0.4

    -0.2

    -0.2

    -0.2

    -0.2

    -0.2

    -0.2

    0

    0

    0

    0

    0

    0

    0

    0

    0.2

    0.2

    0.2

    0.2

    0.2

    0.2

    0.4

    0.4

    0.4

    0.4

    0.4

    0.4

    0.6

    0.6

    0.60.6

    0.8 0.8

    0.80.8

    The level curves of z = y2

    - x2.

    5 10 15 20 25 30 35 40

    5

    10

    15

    20

    25

    30

    35

    40

    11.5

    2

    2.5

    3

    -2

    -1.5

    -1

    -0.5

    0

    0

    0.5

    1

    1.5

    2

  • 7/29/2019 maltab graphics

    11/16

    11

    t = 0:pi/100:pi;r = sin(t);plot(r,t)cylinder(r,15)shading interp

    -1

    -0.5

    0

    0.5

    1

    -1

    -0.5

    0

    0.5

    1

    0

    0.2

    0.4

    0.6

    0.8

    1

    Unit cone

    0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

    0.5

    1

    1.5

    2

    2.5

    3

    3.5

  • 7/29/2019 maltab graphics

    12/16

    12

    Create a Series of Polar Plots.

    % An Mfile script to produce "flower petal" plotstheta = -(pi):0.01:pi;rho(1,:) = 2*sin(5*theta).^2;rho(2,:) = cos(10*theta).^3;rho(3,:) = sin(theta).^2;rho(4,:) = 5*cos(3.5*theta).^3;for i = 1:4

    polar(theta,rho(i,:))pause

    end

    -1

    -0.5

    0

    0.5

    1

    -1

    -0.5

    0

    0.5

    1

    0

    0.2

    0.4

    0.6

    0.8

    1

  • 7/29/2019 maltab graphics

    13/16

    13

    Try entering these commands in an M-file called oloyede.m. This file is now a MATLAB

    script. Typing oloyede at the MATLAB command line executes the statements in the

    script.

    After the script displays a plot, press Return to move to the next plot. There are no input

    or output arguments; petals creates the variables it needs in the MATLAB workspace.

    When execution completes, the variables (i, theta, and rho) remain in the workspace. To

    see a listing of them, enter whos at the command prompt.

    Inline FunctionsMatlab will let you define expressions inside a script for use as functions within thatscript only. For instance, if you wanted to use repeated references to the function

    you would use the following syntax (to make both a line plot in x with y = 2 and to makea surface plot)

    0.5

    1

    1.5

    2

    30

    210

    60

    240

    90

    270

    120

    300

    150

    330

    180 0

  • 7/29/2019 maltab graphics

    14/16

    14

    clear;close all;f=inline(sin(x.*y)./(x.^2+y.^2),x,y);x=-8:.1:8;y=x;plot(x,f(x,2))[X,Y]=meshgrid(x,y);surf(X,Y,f(X,Y))

    The expression that defines the function is in the first string argument to inline and theother string entries tell inline which argument goes in which input slot when you use thefunction.

    -10

    -5

    0

    5

    10

    -10

    -5

    0

    5

    10

    -0.5

    0

    0.5

  • 7/29/2019 maltab graphics

    15/16

    15

    Comparing Interpolation Methods

    This example compares two-dimensional interpolation methods on a 7-by-7 matrix of

    data.

    1 Generate the peaks function at low resolution:[x,y] = meshgrid(-3:1:3);z = peaks(x,y);surf(x,y,z)2 Generate afinermeshforinterpolation:[xi,yi] = meshgrid(-3:0.25:3);3 Interpolate usingnearestneighborinterpolation:zi1 = interp2(x,y,z,xi,yi,'nearest');4 Interpolate usingbilinearinterpolation:zi2 = interp2(x,y,z,xi,yi,'bilinear');5 Interpolate usingbicubicinterpolation:zi3 = interp2(x,y,z,xi,yi,'bicubic');

    for2 x1 0, 2 x2 4, and 0 x3 2. To evaluate and plot this function:

    -4

    -2

    0

    2

    4

    -4

    -2

    0

    2

    4

    -6

    -4

    -2

    0

    2

    4

    6

  • 7/29/2019 maltab graphics

    16/16

    16

    x1 =- 2:0.2:2;x2 = -2:0.25:2;x3 = -2:0.16:2;[X1,X2,X3] = ndgrid(x1,x2,x3);z = X2.*exp(-X1.^2 -X2.^2- X3.^2);slice(X2,X1,X3,z,[-1.28 2],2,[-2 0.2])

    References

    Ross L. Spencer,2000 : introduction to Matlab, Department of Physics and Astronomy

    Brigham Young University

    Edward Neuman Department of Mathematics Southern Illinois University at Carbondale

    [email protected]

    D. Hanselman and B. Littlefield, Mastering MATLAB 5. A Comprehensive Tutorial and

    Reference, Prentice Hall, Upper Saddle River, NJ, 1998.

    P. Marchand, Graphics and GUIs with MATLAB, Second edition, CRC Press, Boca

    Raton,1999.

    K. Sigmon, MATLAB Primer, Fifth edition, CRC Press, Boca Raton, 1998.