Plotting - Loyola University Marylandmath.loyola.edu/~chidyagp/sp19/plotting.pdf · Plotting in MATLAB 2D Plots Plotting Scalar functions Plot f(x) = x2 on [ 2ˇ;2ˇ]. 1 De ne a discrete

Post on 08-Jan-2020

35 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

Transcript

Plotting

Spring 2019

Plotting 1

Plotting in MATLAB 2D Plots

Plotting Scalar functions

Plot f (x) = x2 on [−2π, 2π].

1 Define a discrete set of values uniformly distributed on the domain of f .

2 Evaluate f at each point in the discrete domain.

3 Plot!

1 %step 1 : define discrete domain2 x=linspace(-2*pi,2*pi,100);3

4 %step2: Evaluate each element of the vector x5 %component-wise as:6 yvals = x.ˆ2;7

8 %step 3: call plot with an option to specify line width9 plot(x,yvals,'LineWidth',3);

10 title('$y=xˆ2$ \quad ($n=100$)', 'Interpreter','latex')11 xlabel('x'); ylabel('y');

Plotting 2

Plotting in MATLAB 2D Plots

Plotting Scalar functions

Plot of f (x) = x2 on [−2π, 2π].

Plotting 3

Plotting in MATLAB 2D Plots

Plotting Scalar functions: axis tight

1 %step 1 : define discrete domain2 x=linspace(-2*pi,2*pi,100);3

4 %step2: Evaluate each element of the vector x5 %component-wise as:6 yvals = x.ˆ2;7

8 %step 3: call plot with an option to specify line width9 plot(x,yvals,'LineWidth',3);

10 title('$y=xˆ2$ \quad ($n=100$)', 'Interpreter','latex')11 xlabel('x') ylabel('y')12 axis tight;

Plotting 4

Plotting in MATLAB 2D Plots

Plotting Scalar function: axis tight

Plot of f (x) = x2 on [−2π, 2π].

Plotting 5

Plotting in MATLAB 2D Plots

Caution

It is important to choose a large enough number of sample points in thedomain.

If n is too small, e.g. n = 10, the quality of the plots is poor.

Plotting 6

Plotting in MATLAB 2D Plots

Multiple plots

Plot y1 = sin(x), y2 = 2cos(x), y3 = 3cos(2x) on the same axis

1 y1 = sin(x);2 y2 = 2*cos(x);3 y3 = 3*cos(2*x);4 %pass the "domain" and "range" for each function to plot5 plot(x,y1,x,y2,x,y3,'LineWidth',2)6 title('Mutiple plots')7 xlabel('x')8 ylabel('y')9 %add legend

10 legend('sin(x)','2cos(x)','3cos(2x)')11 axis tight

Plotting 7

Plotting in MATLAB 2D Plots

Multiple plots

Plot of y1 = sin(x) y2 = 2cos(x) y3 = 3cos(2x)

Plotting 8

Plotting in MATLAB 2D Plots

Multiple plots: hold function

Plot of y1 = sin(x) y2 = 2cos(x) y3 = 3cos(2x)

1 y1 = sin(x);2 y2 = 2*cos(x);3 y3 = 3*cos(2*x);4 plot(x,y1,'LineWidth',2) %plot 15 hold on % retains the current axis and to add more plots6 plot(x,y2,'LineWidth',2) % plot 27 plot(x,y3,'LineWidth',2) % plot 38 title('Mutiple plots')9 xlabel('x')

10 ylabel('y')11 %add legend with option to specify its location12 legend('sin(x)','2cos(x)','3cos(2x)', ...

'Location','SouthWest')13 %make the plot box fit tightly around the data14 axis tight15 hold off

Plotting 9

Plotting in MATLAB 2D Plots

Multiple plots

Plot of y1 = sin(x) y2 = 2cos(x) y3 = 3cos(2x)

Plotting 10

Plotting in MATLAB 2D Plots

Customizing plots

2D Plot options

1 y1 = sin(x);2 y2 = 2*cos(x);3 y3 = 3*cos(2*x);4 plot(x,y1,'rv','LineWidth',2) % red triangles (no line)5 hold on % retains the current axis and to add more plots6 plot(x,y2,'g--*','LineWidth',2) % plot 2: green dashed + *7 plot(x,y3,'b:d','LineWidth',2) %plot 3: blue dotted + ...

diamond8 title('Mutiple plots')9 xlabel('x')

10 ylabel('y')11 %add legend with option to specify its location12 legend('sin(x)','2cos(x)','3cos(2x)', ...

'Location','SouthWest')13 %make the plot box fit tightly around the data14 axis tight;15 hold off

Plotting 11

Plotting in MATLAB 2D Plots

Multiple plots - customized

Plot of y1 = sin(x) y2 = 2cos(x) y3 = 3cos(2x)

Plotting 12

Plotting in MATLAB 2D Plots

subplot - creates array of plots1 x=linspace(-5*pi,5*pi,200);2 % Evaluate your functions on x3 y1 = sin(x); y2 = 2*sin(x); y3 = sin(x/2); y4 = sin(2*x);4

5 % We want to plot 4 functions on a 2 by 2 grid6 subplot(2,2,1) % subplot 17 plot(x,y1)8 title('Subplot 1: sin(x)');9

10 subplot(2,2,2)11 plot(x,y2,'g') % subplot212 title('Subplot 2: 2sin(x)');13

14 subplot(2,2,3) % subplot 315 plot(x,y3,'k')16 title('Subplot 3: sin(x/2)');17

18 subplot(2,2,4) %subplot 419 plot(x,y4,'r')20 title('Subplot 4: 2sin(2x)');

Plotting 13

Plotting in MATLAB 2D Plots

subplot - creates array of plots

Plotting 14

Plotting in MATLAB 2D Plots

subplot - axis properties

1 ax1=subplot(2,2,1) % subplot 12 plot(x,y1)3 title('Subplot 1: sin(x)');4

5 ax2=subplot(2,2,2)6 plot(x,y2,'g') % subplot27 title('Subplot 2: 2sin(x)');8

9 ax3=subplot(2,2,3) % subplot 310 plot(x,y3,'k')11 title('Subplot 3: sin(x/2)');12

13 ax4=subplot(2,2,4) %subplot 414 plot(x,y4,'r')15 title('Subplot 4: 2sin(2x)');16

17 %I can then spacify common axis properties18 axis([ax1 ax2 ax3 ax4], [-5*pi, 5*pi, -2, 2])

Plotting 15

Plotting in MATLAB 2D Plots

subplot - axis properties

Plotting 16

Plotting in MATLAB 2D Plots

Other plots

Parametric plot: x = 5cos(t), y = 3sin(t), t ∈ [0, 2π].

1 t = linspace(0,2*pi);2 x = 5*cos(t);3 y = 3*sin(t);4 plot(x,y,'LineWidth',2)5 xlabel('x')6 ylabel('y')7 title('Parametric plot');8 axis equal % same length for the units along axis9 axis([-6 6 -6 6])

Polar curves of the from r , θ - use polarplot(theta, r).

Plotting 17

Plotting in MATLAB 2D Plots

Parametric plot

Plotting 18

Plotting in MATLAB 2D Plots

Parmateric plot - customize axis properties

axis properties

1 t = linspace(0,2*pi);2 x = 5*cos(t);3 y = 3*sin(t);4 ax=gca; % returns current axis5 plot(x,y,'LineWidth',2)6 xlabel('x')7 ylabel('y')8 title('Parametric plot');9 axis equal % use the same length for the data units ...

along each axis10 axis([-6 6 -6 6])11 %set other axis properties12 ax.FontSize =15;13 ax.LineWidth =2;14 ax.XTick =-6:2:6;15 ax.YTick = -6:2:6;16 ax.XMinorTick='on';17 ax.YMinorTick='on';

Plotting 19

Plotting in MATLAB 2D Plots

Parametric plot

Plotting 20

Plotting in MATLAB 3D Plots

Vector functions and Space curves

A vector-valued function or vector function is a function whosedomain is the set of real numbers and whose range is a set of vectors.

If we let t be in the domain and f (t), g(t) and h(t) be scalarfunctions, we can write a vector valued function

r(t) = 〈f (t), g(t), h(t)〉 = f (t)i + g(t)j + h(t)k

Suppose that f , g and h are continuous real-valued functions on aninterval I . The set C of all points (x , y , z) where

x = f (t) y = g(t)z = h(t)

and t varies on I is called a space curve.

Plotting 21

Plotting in MATLAB 3D Plots

Space curve

Example

x = t cos t, y = t, z = t sin t, t ∈ [0, 10π]

1 t =linspace(0,10*pi,1000);2 %define x, y, z (component-wise)3 x= t.*cos(t);4 y= t;5 z = t.*sin(t);6 plot3(x,y,z,'LineWidth',2);7 xlabel('x'),ylabel('y'),zlabel('z')8 title('Space curve');9 grid on

Plotting 22

Plotting in MATLAB 3D Plots

Space curve

Plotting 23

Plotting in MATLAB 3D Plots

Plotting surfaces (z = f (x , y))

Step 1: Define two vectors containing the x and y coordinates of thediscrete domain.

Step 2: Create the 2D grid coordinates X and Y using mesh grid

[X,Y] = meshgrid(x,y).

Step 3: Evaluate z = f (x , y) at all points on the mesh grid.

Step 4: Plot the surface using mesh or surf.

Plotting 24

Plotting in MATLAB 3D Plots

meshgrid

1 >> x=1:42 x =3 1 2 3 44 >> y=2:65 y =6 2 3 4 5 67 >> [X,Y]=meshgrid(x,y)8 X =9 1 2 3 4

10 1 2 3 411 1 2 3 412 1 2 3 413 1 2 3 414 Y =15 2 2 2 216 3 3 3 317 4 4 4 418 5 5 5 519 6 6 6 6

Plotting 25

Plotting in MATLAB 3D Plots

2D domain

>>plot(X,Y,'r*')

Plotting 26

Plotting in MATLAB 3D Plots

Plot a plane z = x + y

1 x=1:4;2 y=2:6;3 [X,Y]=meshgrid(x,y); %create discrete domain4 Z =X+Y; % evaluate f(x,y)5 mesh(X,Y,Z); %plot6 title('z=x+y');7 xlabel('x')8 ylabel('y')9 zlabel('z')

Plotting 27

Plotting in MATLAB 3D Plots

f (x , y) = x + y

Plotting 28

Plotting in MATLAB 3D Plots

f (x , y) = sin(x)− sin(y)

1 [X,Y]=meshgrid(linspace(-10,10,200)); %create square grid2 Z = sin(X)-sin(Y); % evaluate z3 surf(X,Y,Z); %plot

Plotting 29

Plotting in MATLAB 3D Plots

f (x , y) = sin(x)− sin(y) axis equal

1 [X,Y]=meshgrid(linspace(-10,10,200)); %create square grid2 Z = sin(X)-sin(Y); % evaluate z3 surf(X,Y,Z); %plot4 axis equal

This gives a better perception of the 3D plotPlotting 30

Plotting in MATLAB 3D Plots

surf vs mesh

surf - turns face coloring on by default and uses black edges

mesh - turns off face coloring and uses colored edges

Plotting 31

Plotting in MATLAB 3D Plots

f (x , y) = sin(x)− sin(y) mesh

1 [X,Y]=meshgrid(linspace(-10,10,200)); %create square grid2 Z = sin(X)-sin(Y); % evaluate z3 mesh(X,Y,Z); %plot4 axis equal

Plotting 32

Plotting in MATLAB 3D Plots

Other 3D plots - [X,Y,Z] = sphere(N)

Recall that

(x − a)2 + (y − b)2 + (z − c)2 = r2

is the equation of a sphere of radius r with centre (a, b, c).

1 >> help sphere2 sphere Generate sphere.3 [X,Y,Z] = sphere(N) generates three (N+1)-by-(N+1)4 matrices so that SURF(X,Y,Z) produces a unit sphere.5 [X,Y,Z] = sphere uses N = 20.

Plotting 33

Plotting in MATLAB 3D Plots

>>sphere (default)

Plotting 34

Plotting in MATLAB 3D Plots

>>sphere(10)

Plotting 35

Plotting in MATLAB 3D Plots

>>sphere(50)

Plotting 36

Plotting in MATLAB 3D Plots

Transformations

1 [X,Y,Z] = sphere(50); % sphere r=1, center = origin2 r=0.5;3 %scale the radius4 new X = 0.5*X; new Y = 0.5*Y; new Z = 0.5*Z;5

6 %plot 17 %sphere of r=0.58 surf(new X,new Y,new Z);9 hold on

10 title('Transformations');11 xlabel('x'),ylabel('y'),zlabel('z')12 %plot 213 %move sphere 3 units to the right in the x direction14 surf(new X+3,new Y, new Z);15 axis equal16 % move sphere 1.5 units in x and 1 unit in the z17 surf(new X+1.5,new Y, new Z +1);18 hold off

Plotting 37

Plotting in MATLAB 3D Plots

Transformations

Plotting 38

Plotting in MATLAB 3D Plots

Other 3D plots - [X,Y,Z] = cylinder(R,N)

Forms unit cylinder based on the radius specified at equally spacedpoints along the unit height in R and N points around thecircumference

Plotting 39

Plotting in MATLAB 3D Plots

>>cylinder (default)

Plotting 40

Plotting in MATLAB 3D Plots

>>cylinder(0.5,50)

Plotting 41

Plotting in MATLAB 3D Plots

>>cylinder(0:10,50)

Plotting 42

Plotting in MATLAB 3D Plots

>>cylinder((0:10).ˆ2,100)

Plotting 43

Plotting in MATLAB 3D Plots

General plotting tips

Use clf between plots to clear any axis settings.

Always include axis labels that have, when appropriate, units.

When using hold on, you should only use it after the first plotcommand has been issued.

Always remember to call hold off

Always include appropriate titles.

Use different styles for multiple lines on the same plots.

Plotting 44

top related