using mathematica to solve odes (part...

8
Using Mathematica to solve ODEs (part 1) Basic tool is “DSolve” Note that in DSolve the dependent variable (usually y below) must be written y[x] or y’[x] (for the derivative) or y’’[x] (for the second derivative) etc.. You cannot write just “y”. Mathematica needs to know that y is dependent. Also don’t forget to use a double equals sign: == First-order linear ODEs Apply to separable equation discussed in class: Sec. 8.2 #3 General solution. C[1] is the integration constant. In[239]:= DSolve@y'@xD Sin@xD ã y@xD Log@y@xDD,y@xD,xD Out[239]= ::y@xD ؉ C@1D TanA x 2 E >> Solution given BC: In[240]:= DSolve@8y'@xD Sin@xD ã y@xD Log@y@xDD,y@Pi ê 3D ã E<,y@xD,xD Out[240]= ::y@xD ؉ 3 TanA x 2 E >> Sec. 8.3 #11 (another example discussed in class) General linear first-order but non-separable In[241]:= DSolve@y'@xD + y@xD Cos@xD ã Sin@2xD,y@xD,xD Out[241]= 99y@xD Ø- 2 +‰ -Sin@xD C@1D + 2 Sin@xD== Note that this solution differs from that derived in class only in that the constant C[1] here corresponds to (2+y(0)) in the expresssion in lecture notes. This comparison is what Boas calls “reconciling”.

Upload: others

Post on 27-Aug-2021

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Using Mathematica to solve ODEs (part 1)courses.washington.edu/ph227814/228/W14/notes/ODE1.nb.pdf · 2014. 7. 29. · 6 ODE1.nb. These are curves orthogonal to the solutions at all

Using Mathematica to solve ODEs (part 1)

Basic tool is “DSolve”

Note that in DSolve the dependent variable (usually y below) must be written y[x] ory’[x] (for the derivative) or y’’[x] (for the second derivative) etc.. You cannot write just “y”.Mathematica needs to know that y is dependent.Also don’t forget to use a double equals sign: ==

First-order linear ODEs

Apply to separable equation discussed in class: Sec. 8.2 #3

General solution.C[1] is the integration constant.

In[239]:= DSolve@y'@xD Sin@xD ã y@xD Log@y@xDD, y@xD, xD

Out[239]= ::y@xD Ø ‰‰C@1D TanAx

2E>>

Solution given BC:

In[240]:= DSolve@8y'@xD Sin@xD ã y@xD Log@y@xDD, y@Pi ê 3D ã E<, y@xD, xD

Out[240]= ::y@xD Ø ‰ 3 TanAx

2E>>

Sec. 8.3 #11 (another example discussed in class)General linear first-order but non-separable

In[241]:= DSolve@y'@xD + y@xD Cos@xD ã Sin@2 xD, y@xD, xD

Out[241]= 99y@xD Ø -2 + ‰-Sin@xD C@1D + 2 Sin@xD==

Note that this solution differs from that derived in class only in that the constant C[1] here corresponds to (2+y(0)) in the expresssion in lecture notes.This comparison is what Boas calls “reconciling”.

Page 2: Using Mathematica to solve ODEs (part 1)courses.washington.edu/ph227814/228/W14/notes/ODE1.nb.pdf · 2014. 7. 29. · 6 ODE1.nb. These are curves orthogonal to the solutions at all

Useful plots---methods are needed for HW2Will use example of Sec. 8.3 #11

Family of solution curvesHere is a plot of solutions for 3 values of y(0). These are refered to as a “family” of solution curves (depending, in this case, on 1 parameter).Notice that the solutions are periodic with period 2˛, as makes sense from the ODE given the periodicity of Cos[x] and Sin[2x].

In[242]:= yy@x_, y0_D := 2 Sin@xD - 2 + H2 + y0L Exp@-Sin@xDD

In[249]:= solnplot = Plot@8yy@x, -1D, yy@x, 0D, yy@x, 1D<, 8x, 0, 7<,PlotStyle Ø 88Thick, Red<, 8Thick, Purple<, 8Thick, Green<<D

Out[249]=

1 2 3 4 5 6 7

-1

1

2

3

4

Slope plotHere is the “slope plot”, with the slope arrows normalized to unit length.Note that the normalized slope vector is given by {1, y’ } / Sqrt[1+(y’)^2].

2 ODE1.nb

Page 3: Using Mathematica to solve ODEs (part 1)courses.washington.edu/ph227814/228/W14/notes/ODE1.nb.pdf · 2014. 7. 29. · 6 ODE1.nb. These are curves orthogonal to the solutions at all

In[244]:= slopeplot = VectorPlot@81, Sin@2 xD - y Cos@xD< ê Sqrt@1 + HSin@2 xD - y Cos@xDL^2D, 8x, 0, 7<, 8y, -1.5, 4.5<D

Out[244]=

0 1 2 3 4 5 6 7

-1

0

1

2

3

4

Overlaying the solutions on the slope plot shows how the slope plot allows you to visualize the solutions by following arrows (head to tail to head to tail etc.):

In[250]:= Show@slopeplot, solnplotD

Out[250]=

0 1 2 3 4 5 6 7

-1

0

1

2

3

4

ODE1.nb 3

Page 4: Using Mathematica to solve ODEs (part 1)courses.washington.edu/ph227814/228/W14/notes/ODE1.nb.pdf · 2014. 7. 29. · 6 ODE1.nb. These are curves orthogonal to the solutions at all

Stream PlotHere’s an alternative, using “StreamPlot[]” in which Mathematica does thework of connecting the arrows “head to tail” and curving them so that one can see the “flows” (which are the solutions to the original differential equation).Note that in this case one does not need to normalize the slope vectors

In[255]:= streamplot = StreamPlot@81, Sin@2 xD - y Cos@xD<, 8x, 0, 7<, 8y, -1.5, 4.5<D

Out[255]=

0 1 2 3 4 5 6 7

-1

0

1

2

3

4

Overlaying shows that the head-to-tail arrows really do map out the solutions

4 ODE1.nb

Page 5: Using Mathematica to solve ODEs (part 1)courses.washington.edu/ph227814/228/W14/notes/ODE1.nb.pdf · 2014. 7. 29. · 6 ODE1.nb. These are curves orthogonal to the solutions at all

In[256]:= Show@streamplot, solnplotD

Out[256]=

0 1 2 3 4 5 6 7

-1

0

1

2

3

4

Contour plotHere is another way of showing the family of solutions using a contour plotFirst rewrite the solution so that only the constant of integration (here y(0)) is on the RHS.Then put the LHS into contour plot. The labels on the lines give the values of y(0).

ODE1.nb 5

Page 6: Using Mathematica to solve ODEs (part 1)courses.washington.edu/ph227814/228/W14/notes/ODE1.nb.pdf · 2014. 7. 29. · 6 ODE1.nb. These are curves orthogonal to the solutions at all

In[246]:= curvefamily = ContourPlot@Hy - 2 Sin@xD + 2L ê Exp@-Sin@xDD - 2,8x, 0, 7<, 8y, -2, 5<, ContourLabels Ø TrueD

Out[246]=

Solutions and contours indeed match:

In[270]:= Show@curvefamily, solnplotD

Out[270]=

Orthogonal curvesThese are curves orthogonal to the solutions at all points.In some cases one can solve analytically for these (as in the examples inBoas and in one of the HW problems), but in the present case one cannot.However one can visualize these curves using StreamPlot[] without having an explicit solution.Note that (as discussed in class lecture notes) the slope of the orthogonal curve at a given point {x,y} is given by -1/(slope of solution).The directions of the arrows in the following plot should be ignored.

6 ODE1.nb

Page 7: Using Mathematica to solve ODEs (part 1)courses.washington.edu/ph227814/228/W14/notes/ODE1.nb.pdf · 2014. 7. 29. · 6 ODE1.nb. These are curves orthogonal to the solutions at all

These are curves orthogonal to the solutions at all points.In some cases one can solve analytically for these (as in the examples inBoas and in one of the HW problems), but in the present case one cannot.However one can visualize these curves using StreamPlot[] without having an explicit solution.Note that (as discussed in class lecture notes) the slope of the orthogonal curve at a given point {x,y} is given by -1/(slope of solution).The directions of the arrows in the following plot should be ignored.

In[268]:= orthogcurves = StreamPlot@81, -1 ê HSin@2 xD - y Cos@xDL<,8x, 0, 7<, 8y, -2, 5<, StreamStyle Ø 8Red, Thick<D

Out[268]=

0 1 2 3 4 5 6 7

-2

-1

0

1

2

3

4

5

Superimposing on the family of solution curves, one sees that the orthogonal trajectories can be thought of as paths of steepest descent over the “landscape” of the solutions.

ODE1.nb 7

Page 8: Using Mathematica to solve ODEs (part 1)courses.washington.edu/ph227814/228/W14/notes/ODE1.nb.pdf · 2014. 7. 29. · 6 ODE1.nb. These are curves orthogonal to the solutions at all

In[269]:= Show@curvefamily, orthogcurvesD

Out[269]=

Second-order, linear ODEs

Sec 8.5 #7

Simple second order linear with constant coeffs and homogeneous.Answer agrees with lecture notes.

In[247]:= DSolve@y''@xD - 5 y'@xD + 6 y@xD ã 0, y@xD, xD

Out[247]= 99y@xD Ø ‰2 x C@1D + ‰3 x C@2D==

Sec. 8.6 #25

Second-order linear with constant coeffs and non-zero RHS.Answer agrees with lecture notes.

In[248]:= DSolve@y''@xD - 2 y'@xD - 3 y@xD ã 16 x^2 Exp@-xD, y@xD, xD

Out[248]= ::y@xD Ø -1

24‰-x I3 + 12 x + 24 x2 + 32 x3M + ‰-x C@1D + ‰3 x C@2D>>

8 ODE1.nb