l10. using arrays to plot continuous functions

39
L10. Using Arrays to Plot Continuous Functions Linspace Array Operations

Upload: norm

Post on 06-Jan-2016

39 views

Category:

Documents


0 download

DESCRIPTION

L10. Using Arrays to Plot Continuous Functions. Linspace Array Operations. Table  Plot. x sin(x) 0.00 0.0 1.57 1.0 3.14 0.0 4.71 -1.0 6.28 0.0. Plot based on 5 points. Table  Plot. x sin(x) 0.000 0.000 0.784 0.707 1.571 1.000 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: L10. Using Arrays to Plot Continuous Functions

L10. Using Arrays to Plot Continuous Functions

LinspaceArray Operations

Page 2: L10. Using Arrays to Plot Continuous Functions

Table Plot

x sin(x)0.00 0.01.57 1.03.14 0.04.71 -1.06.28 0.0

Plot based on 5 points

Page 3: L10. Using Arrays to Plot Continuous Functions

Table Plot

x sin(x)0.000 0.0000.784 0.7071.571 1.0002.357 0.7073.142 0.0003.927 -0.707 4.712 -1.0005.498 -0.7076.283 0.000

Plot based on 9 points

Page 4: L10. Using Arrays to Plot Continuous Functions

Table Plot

Plot based on 200 points—looks smooth

Page 5: L10. Using Arrays to Plot Continuous Functions

Generating Tables and Plots

x sin(x)0.000 0.0000.784 0.7071.571 1.0002.357 0.7073.142 0.0003.927 -0.707 4.712 -1.0005.498 -0.7076.283 0.000

x = linspace(0,2*pi,9);y = sin(x);plot(x,y)

Page 6: L10. Using Arrays to Plot Continuous Functions

linspace

x = linspace(1,3,5)

1.0 1.5 2.0 2.5 3.0x :

“x is a table of values”

“x is an array”

“x is a vector”

Page 7: L10. Using Arrays to Plot Continuous Functions

linspace

x = linspace(0,1,101)

0.00 0.01 0.02 0.99 1.00…x :

Page 8: L10. Using Arrays to Plot Continuous Functions

Linspace Syntax

linspace( , , )

Left Endpoint

Right Endpoint

Number of

Points

Page 9: L10. Using Arrays to Plot Continuous Functions

Built-In Functions Accept Arrays

x sin(x)0.00 0.01.57 1.03.14 0.04.71 -1.06.28 0.0

0.00 1.57 3.14 4.71 6.28 sin

And…

Page 10: L10. Using Arrays to Plot Continuous Functions

Return Array of Function-evals

x sin(x)0.00 0.01.57 1.03.14 0.04.71 -1.06.28 0.0

0.00 1.00 0.00 -1.00 0.00sin

Page 11: L10. Using Arrays to Plot Continuous Functions

x = linspace(0,1,200);

y = exp(x);

plot(x,y)

x = linspace(1,10,200);

y = log(x);

plot(x,y)

Examples

Page 12: L10. Using Arrays to Plot Continuous Functions

Can We Plot This?

21

)2/exp()5sin()(

x

xxxf

-2 <= x <= 3

Page 13: L10. Using Arrays to Plot Continuous Functions

Can We Plot This?

21

)2/exp()5sin()(

x

xxxf

-2 <= x <= 3

x = linspace(-2,3,200);y = sin(5*x).*exp(-x/2)./(1 + x.^2)plot(x,y)

Array operations

Yes!

Page 14: L10. Using Arrays to Plot Continuous Functions

Must LearnHow to Operate on Arrays

Look at four simpler plottingchallenges.

Page 15: L10. Using Arrays to Plot Continuous Functions

Example 1

)20sin(*1.)3cos()sin(2)( xxxxf

Page 16: L10. Using Arrays to Plot Continuous Functions

Scale (*)

10 8 -5

2

20 16 -10

a:

s:

c:

c = s*a

Page 17: L10. Using Arrays to Plot Continuous Functions

Addition

10 8 -5

2 4 1

12 12 -4

a:

b:

c:

c = a + b

Page 18: L10. Using Arrays to Plot Continuous Functions

Subtraction

10 8 -5

2 4 1

8 4 -6

a:

b:

c:

c = a - b

Page 19: L10. Using Arrays to Plot Continuous Functions

E.g.1 Sol’n

x = linspace(0,4*pi,200);y1 = sin(x);y2 = cos(3*x);y3 = sin(20*x);y = 2*y1 - y2 + .1*y3;plot(x,y)

Page 20: L10. Using Arrays to Plot Continuous Functions

Example 2.

21

5)(

xxf

Page 21: L10. Using Arrays to Plot Continuous Functions

Exponentiation

10 8 -5

2

100 64 25

a:

s:

c:

c = a.^s

.^

Page 22: L10. Using Arrays to Plot Continuous Functions

Shift

10 8 -5

2

12 10 -3

a:

s:

c:

c = a + s

Page 23: L10. Using Arrays to Plot Continuous Functions

Reciprocation

10 8 -5

.1 .125 -.2

a:

c:

c = 1./a

Page 24: L10. Using Arrays to Plot Continuous Functions

E.g.2 Sol’n

x = linspace(-5,5,200);y = 5./(1+ x.^2);plot(x,y)

Page 25: L10. Using Arrays to Plot Continuous Functions

Example 3.

)10sin()2/exp()( xxxf

Page 26: L10. Using Arrays to Plot Continuous Functions

Negation

10 8 -5

-10 -8 5

a:

c:

c = -a

Page 27: L10. Using Arrays to Plot Continuous Functions

Scale (/)

10 8 -5

2

5 4 -2.5

a:

s:

c:

c = a/s

Page 28: L10. Using Arrays to Plot Continuous Functions

Multiplication

10 8 -5

2 4 1

20 32 -5

a:

b:

c:

c = a .* b

.*

Page 29: L10. Using Arrays to Plot Continuous Functions

E.g.3 Sol’n

x = linspace(0,3,200);y = exp(-x/2).*sin(10*x);plot(x,y)

Page 30: L10. Using Arrays to Plot Continuous Functions

)cos(1.1

2.)(

3

x

xxxf

Example 4.

Page 31: L10. Using Arrays to Plot Continuous Functions

Division

10 8 -5

2 4 1

5 2 -5

a:

b:

c:

c = a ./ b

./

Page 32: L10. Using Arrays to Plot Continuous Functions

E.g.4 Sol’n

x = linspace(-2*pi,2*pi,200);y = (.2*x.^3 - x)./(1.1 + cos(x));plot(x,y)

Page 33: L10. Using Arrays to Plot Continuous Functions

Question Time

How many errors in the followingstatement given that x = linspace(0,1,100):

Y = (3*x .+ 1)/(1 + x^2)

A. 0 B. 1 C. 2 D. 3 E. 4

Page 34: L10. Using Arrays to Plot Continuous Functions

Question Time

How many errors in the followingstatement given that x = linspace(0,1,100):

Y = (3*x .+ 1)/(1 + x^2)

A. 0 B. 1 C. 2 D. 3 E. 4

Y = (3*x + 1) ./ (1 + x.^2)

Page 35: L10. Using Arrays to Plot Continuous Functions

Question Time

Does this assign to y the values sin(0o), sin(1o),…,sin(90o)?

x = linspace(0,pi/2,90);

y = sin(x);

A. Yes B. No

Page 36: L10. Using Arrays to Plot Continuous Functions

Question Time

Does this assign to y the values sin(0o), sin(1o),…,sin(90o)?

%x = linspace(0,pi/2,90);

x = linspace(0,pi/2,91);

y = sin(x);

A. Yes B. No

Page 37: L10. Using Arrays to Plot Continuous Functions

Plotting an Ellipse

122

b

y

a

x

)sin(),cos( tbta

Better:

20 t

Page 38: L10. Using Arrays to Plot Continuous Functions

Solution

a = input(‘Major semiaxis:’);b = input(‘Minor semiaxis:’);

t = linspace(0,2*pi,200);x = a*cos(t);y = b*sin(t);plot(x,y)axis equal off

Page 39: L10. Using Arrays to Plot Continuous Functions

a = 5, b = 3