Plotting in 3D and animation Dr. Mihail October 2, 2018 (Dr. - - PowerPoint PPT Presentation

plotting in 3d and animation
SMART_READER_LITE
LIVE PREVIEW

Plotting in 3D and animation Dr. Mihail October 2, 2018 (Dr. - - PowerPoint PPT Presentation

Plotting in 3D and animation Dr. Mihail October 2, 2018 (Dr. Mihail) Plots October 2, 2018 1 / 15 3D Plots Plots of 1D functions (e.g., f ( x ) = x 2 ) are trivially extended to 2D by using a second input y : f ( x , y ). When the outputs of


slide-1
SLIDE 1

Plotting in 3D and animation

  • Dr. Mihail

October 2, 2018

(Dr. Mihail) Plots October 2, 2018 1 / 15

slide-2
SLIDE 2

3D Plots

Plots of 1D functions (e.g., f (x) = x2) are trivially extended to 2D by using a second input y: f (x, y). When the outputs of these functions is a scalar, we can visualize it in several different ways.

(Dr. Mihail) Plots October 2, 2018 2 / 15

slide-3
SLIDE 3

MATLAB function

peaks

We will use use a built-in MATLAB function useful for demonstrating 3D plots called peaks. In particular, the version of peaks with three outputs: [x, y, z] = peaks(n); will generate an output (z) for every pair of x and y, on a grid of size n. [x, y, z] = peaks(50);

(Dr. Mihail) Plots October 2, 2018 3 / 15

slide-4
SLIDE 4

Surface plot

[x, y, z] = peaks(50);surf(x, y, z);

(Dr. Mihail) Plots October 2, 2018 4 / 15

slide-5
SLIDE 5

Contour plot

[x, y, z] = peaks(50);contour(x, y, z);

(Dr. Mihail) Plots October 2, 2018 5 / 15

slide-6
SLIDE 6

Contour plot with more contour levels

[x, y, z] = peaks(50);contour(x, y, z, 40);

(Dr. Mihail) Plots October 2, 2018 6 / 15

slide-7
SLIDE 7

Mesh

Mesh plot

[x, y, z] = peaks(50);mesh(x, y, z);

(Dr. Mihail) Plots October 2, 2018 7 / 15

slide-8
SLIDE 8

Mesh with contour plot

Mesh with contour

[x, y, z] = peaks(50);meshc(x, y, z);

(Dr. Mihail) Plots October 2, 2018 8 / 15

slide-9
SLIDE 9

3D line plot

[x, y, z] = peaks(50);plot3(x(:), y(:), z(:));

(Dr. Mihail) Plots October 2, 2018 9 / 15

slide-10
SLIDE 10

Color codes

Color coded image

[x, y, z] = peaks(50);imagesc(z); We know relative shape, but each color represents a number. We need to add the colorbar.

(Dr. Mihail) Plots October 2, 2018 10 / 15

slide-11
SLIDE 11

Color codes

Color coded image

[x, y, z] = peaks(50);imagesc(z);colorbar;

(Dr. Mihail) Plots October 2, 2018 11 / 15

slide-12
SLIDE 12

Animations

Basic idea

Plot several times a second with slightly different parameters (the ones you want to animate), cleaning the figure each frame. This naturally leads to the use of loops. The quadratic family of functions is: f (x) = ax2 + bx + c Let’s pick the values 2, 3 and 0 for a, b and c: f (x) = 2x2 + 3x

(Dr. Mihail) Plots October 2, 2018 12 / 15

slide-13
SLIDE 13

Quadratic

x = linspace(-5, 5, 100); y = 2*x.^2 + 3*x; plot(x, y); Let’s animate a = 2 above, from 1 to 3.

(Dr. Mihail) Plots October 2, 2018 13 / 15

slide-14
SLIDE 14

Animating a

a = linspace(1, 3, 100); % 100 choices for a, between 1 and 3 x = linspace(-5, 5, 100); % x never changes for one_a = a figure(1);clf; % create and clear figure y = one_a*x.^2 + 3*x; % new function for a specific a plot(x, y); % plot xlim([-5, 5]); % set x-limits ylim([-10, 100]); % set y-limits title([’a = ’ num2str(one_a)]); % set title pause(0.1); % pause one 10th of second each frame end

(Dr. Mihail) Plots October 2, 2018 14 / 15

slide-15
SLIDE 15

Animating c in f (x) = 2x2 + 3x + c

c = linspace(-4, 15, 100); x = linspace(-5, 5, 100); % x never changes for one_c = c figure(1);clf; % create and clear figure y = 2*x.^2 + 3*x + one_c; % new function for a specific b plot(x, y); % plot xlim([-5, 5]); % set x-limits ylim([-10, 100]); % set y-limits title([’c = ’ num2str(one_b) ’; in f(x) = 2*x^2 + 3*x + c’]); pause(0.1); % pause each frame end

(Dr. Mihail) Plots October 2, 2018 15 / 15