notebook giving examples of use of partial derivatives,...

8
Notebook giving examples of use of partial derivatives, maximization and contour plotting---useful for material in Chapter 4 of Boas Partial derivatives---using "D" Partial deriv w.r.t. x D@Sin@x + y^2D,xD CosAx + y 2 E and w.r.t. y D@Sin@x + y^2D,yD 2yCosAx + y 2 E or both together D@Sin@x + y^2D, 88x, y<,1<D 9CosAx + y 2 E,2yCosAx + y 2 E= Second derivatives (partial w.r.t x first, then mixed partial, then mixed partial again, then second wr.t. y) D@Sin@x + y^2D, 88x, y<,2<D MatrixForm - Sin@x + y 2 D - 2ySin@x + y 2 D - 2ySin@x + y 2 D 2 Cos@x + y 2 D - 4y 2 Sin@x + y 2 D Taylor series with more than one variable Expanding about the origin: The O[y]^6 etc. terms tell us the size of the next terms in the expansion

Upload: others

Post on 26-Aug-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Notebook giving examples of use of partial derivatives, …courses.washington.edu/ph227814/227/nb/Partial.nb.pdf · 2014. 11. 14. · Notebook giving examples of use of partial derivatives,

Notebook giving examples of

use of partial derivatives,

maximization and contour

plotting---useful for material

in Chapter 4 of Boas

Partial derivatives---using "D"

Partial deriv w.r.t. x

D@Sin@x + y^2D, xD

CosAx + y2E

and w.r.t. y

D@Sin@x + y^2D, yD

2 y CosAx + y2E

or both together

D@Sin@x + y^2D, 88x, y<, 1<D

9CosAx + y2E, 2 y CosAx + y

2E=

Second derivatives (partial w.r.t x first, then mixed partial, then mixed partial again, then second wr.t. y)

D@Sin@x + y^2D, 88x, y<, 2<D �� MatrixForm

-Sin@x + y2D -2 y Sin@x + y2D-2 y Sin@x + y2D 2 Cos@x + y2D - 4 y2 Sin@x + y2D

Taylor series with more than one variable

Expanding about the origin:

The O[y]^6 etc. terms tell us the size of the next terms in the expansion

Page 2: Notebook giving examples of use of partial derivatives, …courses.washington.edu/ph227814/227/nb/Partial.nb.pdf · 2014. 11. 14. · Notebook giving examples of use of partial derivatives,

cosexpand = Series@Cos@x + yD, 8x, 0, 5<, 8y, 0, 5<D

1 -y2

2

+y4

24

+ O@yD6+ -y +

y3

6

-y5

120

+ O@yD6x +

-1

2

+y2

4

-y4

48

+ O@yD6x2

+y

6

-y3

36

+y5

720

+ O@yD6x3

+

1

24

-y2

48

+y4

576

+ O@yD6x4

+ -y

120

+y3

720

-y5

14400

+ O@yD6x5

+ O@xD6

If you don’t want to see the missing terms you can truncate using “Normal”

Normal@cosexpandD

1 -y2

2

+y4

24

+ x2

-1

2

+y2

4

-y4

48

+ x4

1

24

-y2

48

+y4

576

+

x -y +y3

6

-y5

120

+ x5

-y

120

+y3

720

-y5

14400

+ x3

y

6

-y3

36

+y5

720

Another example:

Normal@Series@x Log@1 + x y^2D, 8x, 0, 6<, 8y, 0, 6<DD

x2y2

-x3y4

2

+x4y6

3

Expanding about a different point (here (1,1)):

Normal@Series@x Log@1 + x y^2D, 8x, 1, 4<, 8y, 1, 4<DD

-1 + H-1 + xL3-1

12

+1 - y

8

+1

16

H-1 + yL2+

1

24

H-1 + yL3-

3

32

H-1 + yL4+

H-1 + xL23

8

+1

4

H-1 + yL -1

4

H-1 + yL2+1

8

H-1 + yL3+

1

32

H-1 + yL4+

H-1 + xL45

192

+1

16

H-1 + yL -5

96

H-1 + yL3+

5

128

H-1 + yL4-1

6

H-1 + yL3+1

8

H-1 + yL4+

y + Log@2D + H-1 + xL1

2

+3

2

H-1 + yL -1

4

H-1 + yL2-1

6

H-1 + yL3+1

4

H-1 + yL4+ Log@2D

Implicit differentiation

Example of 4.6 #2

We are asked to find dy/dx given that

y Exp[x y]=Sin[x]

This seems to require 2 steps. First, differentiating the implicit definition of y[x]

deriv46 = D@y@xD Exp@x y@xDD � Sin@xD, xDãx y@xD

y¢@xD + ã

x y@xDy@xD Hy@xD + x y

¢@xDL � Cos@xD

and then solving for y':

2 Partial.nb

Page 3: Notebook giving examples of use of partial derivatives, …courses.washington.edu/ph227814/227/nb/Partial.nb.pdf · 2014. 11. 14. · Notebook giving examples of use of partial derivatives,

Solve@deriv46, 8y'@xD<D

::y¢@xD ® -ã-x y@xD H-Cos@xD + ãx y@xD

y@xD2L1 + x y@xD

>>

Finding maxima and minima

Example 4.10 #9: extremizing a function within a region

We are asked to find the maximum of 2x^2-3y^2-2x within the circle of radius 1.

Begin by plotting countours of the function

contplot = ContourPlot@2 x^2 - 3 y^2 - 2 x, 8x, -1, 1<,8y, -1, 1<, FrameLabel ® Automatic, ContourLabels ® TrueD;

boundplot = ContourPlot@x^2 + y^2 � 1, 8x, -1, 1<, 8y, -1, 1<,FrameLabel ® Automatic, ContourStyle ® 8Thick, Black<D;

Show@contplot, boundplotD

-3

-3-2

-2

-1

-1

0

1

2

3

-1.0 -0.5 0.0 0.5 1.0

-1.0

-0.5

0.0

0.5

1.0

x

y

Or, even better, make a 3D plot, using the option “RegionFunction” to restrict the plot to the desired disk

Partial.nb 3

Page 4: Notebook giving examples of use of partial derivatives, …courses.washington.edu/ph227814/227/nb/Partial.nb.pdf · 2014. 11. 14. · Notebook giving examples of use of partial derivatives,

ContourPlot3D@z == 2 x^2 - 3 y^2 - 2 x, 8x, -1, 1<, 8y, -1, 1<, 8z, -4, 4<,RegionFunction ® Function@8x, y, z<, x^2 + y^2 £ 1D, AxesLabel ® AutomaticD

-1.0

-0.5

0.0

0.5

1.0

x

-1.0

-0.5

0.0

0.5

1.0

y

-4

-2

0

2

4

z

It is clear from these figures that the maximum lies at (-1,0), with a local maximum at (1,0).

Also clear is that there are two minima, both on the boundary, with x approximately 0.2 and y either

positive or negative.

Let’s see how Mathematica does using the built in functions FindMaximum and FindMinimum.

Without a starting point FindMaximum finds the local, but not the global maximum.

FindMaximum@82 x^2 - 3 y^2 - 2 x, x^2 + y^2 £ 1<, 8x, y<D91.00688 ´ 10

-8, 9x ® 1., y ® -1.14485 ´ 10

-14==

We also see a small numerical error. The y value is slightly off from zero, the error is then amplified

when it finds functions value. Plugging in y = 0 gives the real minimum value.

2 x^2 - 3 y^2 - 2 x �. x ® 1 �. y ® 0

0

To get the correct global maximum need to input a negative starting x:

FindMaximum@82 x^2 - 3 y^2 - 2 x, x^2 + y^2 £ 1<, 88x, -.5<, 8y, 0<<D84., 8x ® -1., y ® 0.<<

FindMinimum without a starting guess finds one of the minima

4 Partial.nb

Page 5: Notebook giving examples of use of partial derivatives, …courses.washington.edu/ph227814/227/nb/Partial.nb.pdf · 2014. 11. 14. · Notebook giving examples of use of partial derivatives,

FindMinimum@82 x^2 - 3 y^2 - 2 x, x^2 + y^2 £ 1<, 8x, y<D8-3.2, 8x ® 0.2, y ® 0.979796<<

Get other minimum by giving a starting point with negative y:

FindMinimum@82 x^2 - 3 y^2 - 2 x, x^2 + y^2 £ 1<, 88x, 0<, 8y, -1<<D8-3.2, 8x ® 0.2, y ® -0.979796<<

This shows the dependence of the function along the boundary (either the top or the bottom half) show-

ing the minima at x=0.2

Inserting y ^2 = 1 - x2:

Plot@2 x^2 - 3 H1 - x^2L - 2 x, 8x, -1, 1<, PlotStyle ® 8Thick, Red<D

-1.0 -0.5 0.5 1.0

-3

-2

-1

1

2

3

4

Example 4.13 #17 --- constrained minimization

Find position of minimum distance to the origin along the surface x=yz+10

Turns out there are two such positions:

FindMinimum with no starting point gives one of the two solutions:

FindMinimum@8x^2 + y^2 + z^2, x � y z + 10<, 8x, y, z<D819., 8x ® 1., y ® 3., z ® -3.<<

Which is a distance:

19.

4.3589

With a starting guess we can find the other minimum:

FindMinimum@8x^2 + y^2 + z^2, x � y z + 10<, 88x, 1<, 8y, -1<, z<D819., 8x ® 1., y ® -3., z ® 3.<<

Here we solve for x using the constraint and contour the distance vs. y and z. The two minima are clear

and also the saddle point at the origin.

Partial.nb 5

Page 6: Notebook giving examples of use of partial derivatives, …courses.washington.edu/ph227814/227/nb/Partial.nb.pdf · 2014. 11. 14. · Notebook giving examples of use of partial derivatives,

ContourPlot@Sqrt@Hy z + 10L^2 + y^2 + z^2D, 8y, -5, 5<, 8z, -5, 5<,FrameLabel ® Automatic, ContourLabels ® True, Contours ® 15D

5.7

5.7

5.7

5.7

7.6

7.6

7.6

7.6

9.5

9.5

9.5

9.5

11.4

11.4

11.4

11.4

13.3

13.3

13.3

13.3

15.2

15.2

15.2

15.2

17.1

17.1

19

19

20.9

20.9

22.8

22.8

24.7

24.7

26.6

26.6

28.5

28.5

30.4

30.4

32.3

32.3

-4 -2 0 2 4

-4

-2

0

2

4

y

z

We can also visualize the function in 3D, though it’s hard to see exactly where the minumum distance

points are.

constraintplot = ContourPlot3D@x == y z + 10, 8x, -5, 5<, 8y, -5, 5<,8z, -50, 50<, AxesLabel ® Automatic, ViewVector ® 8850, -10, 50<, 80, 0, 0<<D;

6 Partial.nb

Page 7: Notebook giving examples of use of partial derivatives, …courses.washington.edu/ph227814/227/nb/Partial.nb.pdf · 2014. 11. 14. · Notebook giving examples of use of partial derivatives,

ManipulateAShow@constraintplot, Graphics3D@8Red, Sphere@80, 0, 0<, Sqrt@r2DD<DD,8r2, 10, 25, Appearance ® "Labeled"<, LabelStyle ® "R

2"E

r2 10

-5

0

5

x

-5 0 5

y

-50

0

50

z

This may look like a pancake but it is really a sphere. The z axis is compressed by a factor of 10. Here it

is not compressed

Partial.nb 7

Page 8: Notebook giving examples of use of partial derivatives, …courses.washington.edu/ph227814/227/nb/Partial.nb.pdf · 2014. 11. 14. · Notebook giving examples of use of partial derivatives,

Manipulate@Show@ContourPlot3D@x == y z + 10, 8x, -5, 5<, 8y, -5, 5<,8z, -5, 5<, AxesLabel ® Automatic, ViewVector ® 8850, -10, 50<, 80, 0, 0<<D,

Graphics3D@8Red, Sphere@80, 0, 0<, Sqrt@r2DD<DD,8r2, 10, 25, Appearance ® "Labeled"<D

r2 10

-5

0

5

x

-5

0

5y

-5

0

5

z

8 Partial.nb