Lecture4: Plotting Lecture4: Plotting 1 Plotting in MATLAB 2D - - PowerPoint PPT Presentation

lecture4 plotting
SMART_READER_LITE
LIVE PREVIEW

Lecture4: Plotting Lecture4: Plotting 1 Plotting in MATLAB 2D - - PowerPoint PPT Presentation

Lecture4: Plotting Lecture4: Plotting 1 Plotting in MATLAB 2D Plots Plotting Scalar functions Plot f ( x ) = x 2 on [ 2 , 2 ]. Define a discrete set of values uniformly distributed on the domain of f . 1 Evaluate f at each point in


slide-1
SLIDE 1

Lecture4: Plotting

Lecture4: Plotting 1

slide-2
SLIDE 2

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 domain

2

x=linspace(-2*pi,2*pi,100);

3 4

%step2: Evaluate each element of the vector x

5

%component-wise as:

6

yvals = x.ˆ2;

7 8

%step 3: call plot with an option to specify line width

9

plot(x,yvals,'LineWidth',3);

10

title('$y=xˆ2$ \quad ($n=100$)', 'Interpreter','latex')

11

xlabel('x'); ylabel('y');

Lecture4: Plotting 2

slide-3
SLIDE 3

Plotting in MATLAB 2D Plots

Plotting Scalar functions

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

Lecture4: Plotting 3

slide-4
SLIDE 4

Plotting in MATLAB 2D Plots

Plotting Scalar functions: axis tight

1

%step 1 : define discrete domain

2

x=linspace(-2*pi,2*pi,100);

3 4

%step2: Evaluate each element of the vector x

5

%component-wise as:

6

yvals = x.ˆ2;

7 8

%step 3: call plot with an option to specify line width

9

plot(x,yvals,'LineWidth',3);

10

title('$y=xˆ2$ \quad ($n=100$)', 'Interpreter','latex')

11

xlabel('x') ylabel('y')

12

axis tight;

Lecture4: Plotting 4

slide-5
SLIDE 5

Plotting in MATLAB 2D Plots

Plotting Scalar function: axis tight

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

Lecture4: Plotting 5

slide-6
SLIDE 6

Plotting in MATLAB 2D Plots

Caution

It is important to choose a large enough number of sample points in the domain. If n is too small, e.g. n = 10, the quality of the plots is poor.

Lecture4: Plotting 6

slide-7
SLIDE 7

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 plot

5

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

Lecture4: Plotting 7

slide-8
SLIDE 8

Plotting in MATLAB 2D Plots

Multiple plots

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

Lecture4: Plotting 8

slide-9
SLIDE 9

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 1

5

hold on % retains the current axis and to add more plots

6

plot(x,y2,'LineWidth',2) % plot 2

7

plot(x,y3,'LineWidth',2) % plot 3

8

title('Mutiple plots')

9

xlabel('x')

10

ylabel('y')

11

%add legend with option to specify its location

12

legend('sin(x)','2cos(x)','3cos(2x)', ... 'Location','SouthWest')

13

%make the plot box fit tightly around the data

14

axis tight

15

hold off

Lecture4: Plotting 9

slide-10
SLIDE 10

Plotting in MATLAB 2D Plots

Multiple plots

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

Lecture4: Plotting 10

slide-11
SLIDE 11

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 plots

6

plot(x,y2,'g--*','LineWidth',2) % plot 2: green dashed + *

7

plot(x,y3,'b:d','LineWidth',2) %plot 3: blue dotted + ... diamond

8

title('Mutiple plots')

9

xlabel('x')

10

ylabel('y')

11

%add legend with option to specify its location

12

legend('sin(x)','2cos(x)','3cos(2x)', ... 'Location','SouthWest')

13

%make the plot box fit tightly around the data

14

axis tight;

15

hold off

Lecture4: Plotting 11

slide-12
SLIDE 12

Plotting in MATLAB 2D Plots

Multiple plots - customized

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

Lecture4: Plotting 12

slide-13
SLIDE 13

Plotting in MATLAB 2D Plots

subplot - creates array of plots

1

x=linspace(-5*pi,5*pi,200);

2

% Evaluate your functions on x

3

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 grid

6

subplot(2,2,1) % subplot 1

7

plot(x,y1)

8

title('Subplot 1: sin(x)');

9 10

subplot(2,2,2)

11

plot(x,y2,'g') % subplot2

12

title('Subplot 2: 2sin(x)');

13 14

subplot(2,2,3) % subplot 3

15

plot(x,y3,'k')

16

title('Subplot 3: sin(x/2)');

17 18

subplot(2,2,4) %subplot 4

19

plot(x,y4,'r')

20

title('Subplot 4: 2sin(2x)');

Lecture4: Plotting 13

slide-14
SLIDE 14

Plotting in MATLAB 2D Plots

subplot - creates array of plots

Lecture4: Plotting 14

slide-15
SLIDE 15

Plotting in MATLAB 2D Plots

subplot - axis properties

1

ax1=subplot(2,2,1) % subplot 1

2

plot(x,y1)

3

title('Subplot 1: sin(x)');

4 5

ax2=subplot(2,2,2)

6

plot(x,y2,'g') % subplot2

7

title('Subplot 2: 2sin(x)');

8 9

ax3=subplot(2,2,3) % subplot 3

10

plot(x,y3,'k')

11

title('Subplot 3: sin(x/2)');

12 13

ax4=subplot(2,2,4) %subplot 4

14

plot(x,y4,'r')

15

title('Subplot 4: 2sin(2x)');

16 17

%I can then spacify common axis properties

18

axis([ax1 ax2 ax3 ax4], [-5*pi, 5*pi, -2, 2])

Lecture4: Plotting 15

slide-16
SLIDE 16

Plotting in MATLAB 2D Plots

subplot - axis properties

Lecture4: Plotting 16

slide-17
SLIDE 17

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 axis

9

axis([-6 6 -6 6])

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

Lecture4: Plotting 17

slide-18
SLIDE 18

Plotting in MATLAB 2D Plots

Parametric plot

Lecture4: Plotting 18

slide-19
SLIDE 19

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 axis

5

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 axis

10

axis([-6 6 -6 6])

11

%set other axis properties

12

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';

Lecture4: Plotting 19

slide-20
SLIDE 20

Plotting in MATLAB 2D Plots

Parametric plot

Lecture4: Plotting 20

slide-21
SLIDE 21

Plotting in MATLAB 3D Plots

Vector functions and Space curves

A vector-valued function or vector function is a function whose domain 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 scalar functions, 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 an interval 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.

Lecture4: Plotting 21

slide-22
SLIDE 22

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

Lecture4: Plotting 22

slide-23
SLIDE 23

Plotting in MATLAB 3D Plots

Space curve

Lecture4: Plotting 23

slide-24
SLIDE 24

Plotting in MATLAB 3D Plots

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

Step 1: Define two vectors containing the x and y coordinates of the discrete 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.

Lecture4: Plotting 24

slide-25
SLIDE 25

Plotting in MATLAB 3D Plots

meshgrid

1 >

> x=1:4

2

x =

3

1 2 3 4

4 >

> y=2:6

5

y =

6

2 3 4 5 6

7 >

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

8

X =

9

1 2 3 4

10

1 2 3 4

11

1 2 3 4

12

1 2 3 4

13

1 2 3 4

14

Y =

15

2 2 2 2

16

3 3 3 3

17

4 4 4 4

18

5 5 5 5

19

6 6 6 6

Lecture4: Plotting 25

slide-26
SLIDE 26

Plotting in MATLAB 3D Plots

2D domain

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

Lecture4: Plotting 26

slide-27
SLIDE 27

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 domain

4

Z =X+Y; % evaluate f(x,y)

5

mesh(X,Y,Z); %plot

6

title('z=x+y');

7

xlabel('x')

8

ylabel('y')

9

zlabel('z')

Lecture4: Plotting 27

slide-28
SLIDE 28

Plotting in MATLAB 3D Plots

f (x, y) = x + y

Lecture4: Plotting 28

slide-29
SLIDE 29

Plotting in MATLAB 3D Plots

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

1

[X,Y]=meshgrid(linspace(-10,10,200)); %create square grid

2

Z = sin(X)-sin(Y); % evaluate z

3

surf(X,Y,Z); %plot

Lecture4: Plotting 29

slide-30
SLIDE 30

Plotting in MATLAB 3D Plots

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

1

[X,Y]=meshgrid(linspace(-10,10,200)); %create square grid

2

Z = sin(X)-sin(Y); % evaluate z

3

surf(X,Y,Z); %plot

4

axis equal

This gives a better perception of the 3D plot

Lecture4: Plotting 30

slide-31
SLIDE 31

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

Lecture4: Plotting 31

slide-32
SLIDE 32

Plotting in MATLAB 3D Plots

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

1

[X,Y]=meshgrid(linspace(-10,10,200)); %create square grid

2

Z = sin(X)-sin(Y); % evaluate z

3

mesh(X,Y,Z); %plot

4

axis equal

Lecture4: Plotting 32

slide-33
SLIDE 33

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 sphere

2

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.

Lecture4: Plotting 33

slide-34
SLIDE 34

Plotting in MATLAB 3D Plots

>>sphere (default)

Lecture4: Plotting 34

slide-35
SLIDE 35

Plotting in MATLAB 3D Plots

>>sphere(10)

Lecture4: Plotting 35

slide-36
SLIDE 36

Plotting in MATLAB 3D Plots

>>sphere(50)

Lecture4: Plotting 36

slide-37
SLIDE 37

Plotting in MATLAB 3D Plots

Transformations

1

[X,Y,Z] = sphere(50); % sphere r=1, center = origin

2

r=0.5;

3

%scale the radius

4

new X = 0.5*X; new Y = 0.5*Y; new Z = 0.5*Z;

5 6

%plot 1

7

%sphere of r=0.5

8

surf(new X,new Y,new Z);

9

hold on

10

title('Transformations');

11

xlabel('x'),ylabel('y'),zlabel('z')

12

%plot 2

13

%move sphere 3 units to the right in the x direction

14

surf(new X+3,new Y, new Z);

15

axis equal

16

% move sphere 1.5 units in x and 1 unit in the z

17

surf(new X+1.5,new Y, new Z +1);

18

hold off

Lecture4: Plotting 37

slide-38
SLIDE 38

Plotting in MATLAB 3D Plots

Transformations

Lecture4: Plotting 38

slide-39
SLIDE 39

Plotting in MATLAB 3D Plots

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

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

Lecture4: Plotting 39

slide-40
SLIDE 40

Plotting in MATLAB 3D Plots

>>cylinder (default)

Lecture4: Plotting 40

slide-41
SLIDE 41

Plotting in MATLAB 3D Plots

>>cylinder(0.5,50)

Lecture4: Plotting 41

slide-42
SLIDE 42

Plotting in MATLAB 3D Plots

>>cylinder(0:10,50)

Lecture4: Plotting 42

slide-43
SLIDE 43

Plotting in MATLAB 3D Plots

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

Lecture4: Plotting 43

slide-44
SLIDE 44

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 plot command has been issued. Always remember to call hold off Always include appropriate titles. Use different styles for multiple lines on the same plots.

Lecture4: Plotting 44