1 advanced plotting ผศ. ดร. อนันต์ ผลเพิ่ม anan phonphoem anan...

26
1 Advanced Plotting ผผ.ผผ.ผผผผผผ ผผผผผผผ Anan Phonphoem http://www.cpe.ku.ac.th/ ~anan [email protected]

Upload: oscar-fletcher

Post on 27-Dec-2015

254 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Advanced Plotting ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

1

Advanced Plotting

ผศ.ดร.อนั�นัต์ ผลเพิ่��มAnan Phonphoem

http://www.cpe.ku.ac.th/[email protected]

Page 2: 1 Advanced Plotting ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

2

Outline

xy plot Grid and Axis fplot polynomial plot subplot

Page 3: 1 Advanced Plotting ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

3

Plotting

Two-dimensional plot (xy plot) help graph2d

Three-dimensional plot (xyz plot, surface plot) help graph3d

Page 4: 1 Advanced Plotting ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

4

xy Plotting Functions

y = f(x) x-axis (abscissa) y-axis (ordinate)

abscissa

ordinate

Page 5: 1 Advanced Plotting ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

5

1 2 3 4 5 6 7 8 9 101000

1200

1400

1600

1800

2000

2200

2400

2600

2800

3000

Distance (Km)

Cos

t (B

aht)

Cost of travelling

data1

The Anatomy of a PlotPlot Title

Y-Axis Label

X-Axis Label

Tick Mark

Tick-Mark Label

Legend Data Symbol

Page 6: 1 Advanced Plotting ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

6

Plot Essential Features

Axis label with unit Regular spaced tick mark (0.5, 1.0, 1.5,

…) More than one curve use Legend Use Title Plot measured data using Data Symbol Careful about connecting line

Page 7: 1 Advanced Plotting ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

7

Plotting example

y = 0.4 √1.8x for 0 ≤x ≤ 52 y = height of a rocket after launch (miles) x = horizontal (downrange) distance (miles)

>>x = [0:0.1:52];>>y = 0.4*sqrt(1.8*x);>>plot(x,y)>>xlabel(’Distance (miles)’)>>ylabel(’Height (miles)’)>>title(’Rocket Height as a Function of Downrange Distance’)

Page 8: 1 Advanced Plotting ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

8

Plotting example

Page 9: 1 Advanced Plotting ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

9

Grid and axis Commands

grid display gridlines toggle by grid on or grid off

axis override MATLAB axis syntax axis([xmin xmax ymin ymax])

Page 10: 1 Advanced Plotting ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

10

Plotting example II

>>x = [0:0.1:52];>>y = 0.4*sqrt(1.8*x);>>plot(x,y)>>xlabel(’Distance (miles)’)>>ylabel(’Height (miles)’)>>title(’Rocket Height as a Function of Downrange Distance’)

>>grid on>>axis([0 52 0 5])

Page 11: 1 Advanced Plotting ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

11

Plotting example II

Page 12: 1 Advanced Plotting ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

12

Plots of Complex Numbers

plot(y) plot(real(y),imag(y))

>>z = 0.1 + 0.9i;>>n = [0:0.01:10];>>plot(z.^n)>>xlabel(‘Real’), ylabel(‘Imaginary’)

Page 13: 1 Advanced Plotting ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

13

Plots of Complex Numbers

Page 14: 1 Advanced Plotting ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

14

fplot command

Automatically analyzes the function Then decides the plotting points Syntax fplot(‘string’,[xmin xmax])

string describe function [xmin xmax] value of variable

Page 15: 1 Advanced Plotting ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

15

fplot example

>>a_func = ‘cos(tax(x))-tan(sin(x))’;>>fplot(a_func,[1 2])>>xlabel(‘x’),ylabel(‘function’)>>title(‘using fplot’)

Page 16: 1 Advanced Plotting ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

16

fplot example I

Page 17: 1 Advanced Plotting ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

17

Compare to fplot

>>x=[1:0.01:2];>>y=cos(tax(x))-tan(sin(x));>>plot(x,y)>>xlabel(‘x’),ylabel(‘function’)>>title(‘using fplot’)

Page 18: 1 Advanced Plotting ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

18

Compare to fplot

Page 19: 1 Advanced Plotting ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

19

Compare to fplot

fplot plot

Page 20: 1 Advanced Plotting ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

20

Polynomial Plot

>>x=[-6:0.01:6];>>p=[3,2,-100,2,-7,90];>>plot(x,polyval(p,x))>>xlabel(‘x’),ylabel(‘p’)>>title(‘polynomial plot’)

p(x) = 3x5 + 2x4 – 100x3 +2x2 – 7x+90

Page 21: 1 Advanced Plotting ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

21

Polynomial Plot

Page 22: 1 Advanced Plotting ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

22

Subplots

Several smaller plots in one figure Syntax subplot(m,n,p)

m m rows n n columns p output plot

Page 23: 1 Advanced Plotting ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

23

Subplots example I

>>x = [0:0.01:5];>>y = exp(-1.2*x).*sin(10*x+5);>>subplot(1,2,1)>>plot(x,y),axis([0 5 -1 1])>>x = [-6:0.01:6];>>y = abs(x.^3-100);>>subplot(1,2,2)>>plot(x,y),axis([-6 6 0 350])

y = e-1.2x sin(10x + 5) for 0 ≤x ≤ 5y = |x3 - 100| for -6 ≤ x ≤ 6.

Page 24: 1 Advanced Plotting ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

24

Subplots example I

Page 25: 1 Advanced Plotting ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

25

Subplots example II

>>x = [0:0.01:5];>>y = exp(-1.2*x).*sin(10*x+5);>>subplot(2,1,1)>>plot(x,y),axis([0 5 -1 1])>>title(‘figure(a)’)>>x = [-6:0.01:6];>>y = abs(x.^3-100);>>subplot(2,2,2)>>plot(x,y),axis([-6 6 0 350])>>title(‘figure(b)’)

Page 26: 1 Advanced Plotting ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem anan anan@cpe.ku.ac.th

26

Subplots example II