using mathematica to solve odescourses.washington.edu/ph227814/228/nb/ode1.nb.pdf · 2015-01-05 ·...

15
Using Mathematica to solve ODEs 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. DSolve@y'@xD Sin@xD y@xD Log@y@xDD,y@xD,xD ::y@xD fiª ª C@1D TanA x 2 E >> Solution given BC that y[Pi/3]=e DSolve@8y'@xD Sin@xD y@xD Log@y@xDD,y@Pi 3D E<,y@xD,xD Solve::ifun : Inverse functions are being used by Solve, so some solutions may not be found; use Reduce for complete solution information. ::y@xD fiª 3 TanA x 2 E >> Solution diverges at x=Pi

Upload: others

Post on 31-Jan-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Using Mathematica to solve

ODEs

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.

DSolve@y'@xD Sin@xD y@xD Log@y@xDD, y@xD, xD

::y@xD ® ããC@1D

TanA x

2E>>

Solution given BC that y[Pi/3]=e

DSolve@8y'@xD Sin@xD y@xD Log@y@xDD, y@Pi 3D E<, y@xD, xDSolve::ifun : Inverse functions are being used by Solve, so

some solutions may not be found; use Reduce for complete solution information.

::y@xD ® ã3 TanA x

2E>>

Solution diverges at x=Pi

PlotBE^HSqrt@3D Tan@x 2DL, 8x, -Π, Π<, PlotRange ® Automatic,

AxesLabel ® :"x", "y@xD=ã3 TanA x

2E">, Ticks ® 8Table@n * Π 4, 8n, -4, 4<D, Automatic<F

-Π -3 Π

4-

Π

2

4

Π

4

Π

2

3 Π

x

10

20

30

40

y@xD=ã3 TanB x

2

F

This is a little easier to see with LogPlot

LogPlotBE^HSqrt@3D Tan@x 2DL, 8x, -Π, Π<, PlotRange ® Automatic,

AxesLabel ® :"x", "y@xD=ã3 TanA x

2E">, Ticks ® 8Table@n * Π 4, 8n, -4, 4<D, Automatic<F

-3 Π

4-

Π

2

40

Π

4

Π

2

3 Π

x

10-5

0.001

0.100

10

1000

105

y@xD=ã3 TanB x

2

F

Sec. 8.3 #11 (another example discussed in class)

General linear first-order but non-separable

DSolve@y'@xD + y@xD Cos@xD Sin@2 xD, y@xD, xD

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”.

Useful plots---methods are needed for HW1

Will use example of Sec. 8.3 #11

2 RevisedODE1.nb

Useful plots---methods are needed for HW1

Will use example of Sec. 8.3 #11

Family of solution curves

Here 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].

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

solnplot = Plot@8yy@x, -1D, yy@x, 0D, yy@x, 1D<, 8x, 0, 7<,PlotStyle ® 88Thick, Red<, 8Thick, Purple<, 8Thick, Green<<,PlotLegends ® 8"y0=-1", "y0=0", "y0=1"<, AxesLabel ® 8"x", "y"<,Ticks ® 8Table@n * Π 2, 8n, -4, 4<D, Automatic<D

Π

3 Π

22 Π

x

-1

1

2

3

4

y

y0=-1

y0=0

y0=1

Slope plot

Here 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].

RevisedODE1.nb 3

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

0 2 4 6

-2

-1

0

1

2

3

4

5

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.):

Show@slopeplot, solnplotD

0 1 2 3 4 5 6 7

-1

0

1

2

3

4

4 RevisedODE1.nb

Stream Plot

Here’s an alternative, using “StreamPlot[]” in which Mathematica does the

work 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

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

0 2 4 6

-1

0

1

2

3

4

5

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

RevisedODE1.nb 5

Show@streamplot, solnplotD

0 2 4 6

-1

0

1

2

3

4

5

Contour plot

Here is another way of showing the family of solutions using a contour plot

First 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).

6 RevisedODE1.nb

curvefamily = ContourPlot@Hy - 2 Sin@xD + 2L Exp@-Sin@xDD - 2,

8x, 0, 7<, 8y, -2, 5<, ContourLabels ® TrueD

-6

-4

-4

-2

-2

022

4 4

6

6

8

8

10

0 1 2 3 4 5 6 7

-2

-1

0

1

2

3

4

5

Solutions and contours indeed match:

Show@curvefamily, solnplotD

-6

-4

-4

-2

-2

022

4 4

6

6

8

8

10

0 1 2 3 4 5 6 7

-2

-1

0

1

2

3

4

5

The number of countours can be increased to make this more obvious

RevisedODE1.nb 7

Show@ContourPlot@Hy - 2 Sin@xD + 2L Exp@-Sin@xDD - 2,

8x, 0, 7<, 8y, -2, 5<, ContourLabels ® True, Contours ® 25D, solnplotD

-6.57

-5.84

-5.11

-4.38

-4.38

-3.65

-3.65

-2.92 -2.92

-2.19

-2.19

-1.46-1.46

-0.73

0

0.73

1.46

1.46

2.19

2.19

2.92

2.92

3.653.65

4.38

4.38

5.115.115.84

5.84

6.57

6.57

7.3

7.3 8.038.03

8.76

8.76

9.49

10.22

10.95

0 1 2 3 4 5 6 7

-2

-1

0

1

2

3

4

5

y0=-1

y0=0

y0=1

Orthogonal curves

These are curves orthogonal to the solutions at all points.

In some cases one can solve analytically for these (as in the examples in

Boas 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.

8 RevisedODE1.nb

orthogcurves = StreamPlot@81, -1 HSin@2 xD - y Cos@xDL<,8x, 0, 7<, 8y, -2, 5<, StreamStyle ® 8Red, Thick<D

0 2 4 6

-2

0

2

4

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.

Show@curvefamily, orthogcurvesD

-6

-4

-4

-2

-2

022

4 4

6

6

8

8

10

0 1 2 3 4 5 6 7

-2

-1

0

1

2

3

4

5

Exact first order equation

RevisedODE1.nb 9

Exact first order equation

Recall this is an ODE which can be written dF(x,y)=P(x,y)dx+Q(x,y)dy=0

Sec. 8.4 #5

First simply solve and look at solutions:

DSolve@y'@xD Hx - y@xDL + y@xD + x + 1 0, y@xD, xD FullSimplify

::y@xD ® x - ä -2 x H1 + xL - C@1D >, :y@xD ® x + ä -2 x H1 + xL - C@1D >>

Rewrite solutions without pesky i’s and minus signs inside square-roots.

Integration constants are chosen so that yplus[0]=yplus0 and yminus[0]=-yminus0

yplus@x_, yplus0_D := x + Sqrt@2 Hx^2 - xL + yplus0^2D;yminus@x_, yminus0_D := x - Sqrt@2 Hx^2 - xL + yminus0^2D

Plot@8yplus@x, 10D, yminus@x, -10D<, 8x, 0, 10<,AxesLabel ® 8"x", "y"<, PlotLegends ® 8"y0=10", "y0=-10"<D

2 4 6 8 10

x

-10

10

20

y

y0=10

y0=-10

Now make use of fact that ODE is exact, so the solution can be written as F(x,y)=constant, where F is

given by:

f845@x_, y_D := x y - y^2 2 + x^2 2 + x

10 RevisedODE1.nb

ContourPlot@f845@x, yD, 8x, 0, 10<, 8y, -10, 20<,ContourLabels ® True, FrameLabel ® 8"x", "y"<, PlotLabel ® "contours of F"D

-150

-100

-50

-50

0

50

100

0 2 4 6 8 10

-10

-5

0

5

10

15

20

x

y

contours of F

To be clear: contour lines are solutions to our ODE, and you can see that two of the contours match the

plots above.

Second-order, linear ODEs

Not a whole lot to say about this: Mathematica just gives the solution!

Sec 8.5 #7

Simple second order linear with constant coeffs and homogeneous.

Answer agrees with lecture notes.

DSolve@y''@xD - 5 y'@xD + 6 y@xD 0, y@xD, xD99y@xD ® ã

2 xC@1D + ã

3 xC@2D==

Sec. 8.6 #25

Second-order linear with constant coeffs and non-zero RHS.

Answer agrees with lecture notes.

DSolve@y''@xD - 2 y'@xD - 3 y@xD 16 x^2 Exp@-xD, y@xD, xD

::y@xD ® -1

24

ã-x I3 + 12 x + 24 x

2+ 32 x

3M + ã-xC@1D + ã

3 xC@2D>>

RevisedODE1.nb 11

Solving an ODE using Fourier transforms (following lecture notes)

Here’s the step function:

ff@x_D = Piecewise@881, -1 £ x £ 1<<D1 -1 £ x £ 1

0 True

Plot@ff@xD, 8x, -2, 2<, PlotStyle ® 8Thick, Red<, Exclusions ® NoneD

-2 -1 1 2

0.2

0.4

0.6

0.8

1.0

Note: The “Exclusions ® None” option tells Mathematica to always connect the line, even at the disconti-

nuities at x=+/-1

Here’s the Fourier transform

The “FourierParameters” command sets parameters to match our conventions

ftff@alp_D = FourierTransform@ff@xD, x, alp, FourierParameters ® 8-1, -1<DSin@alpDalp Π

The format for FourierTransform[ ] is FourierTransform[function you want to transform, initial variable

(eg. time), transformed variable (eg. frequency)]. The “FourierParameters®a,b” fit into the transforma-

tion as:

b¤H2 ΠL1-a

à-¥

¥

fHtL ei b Ω t

dt.

12 RevisedODE1.nb

Plot@ftff@alpD, 8alp, -3.5 Pi, 3.5 Pi<, PlotStyle ® 8Thick<D

-10 -5 5 10

0.1

0.2

0.3

Mod squaring this gives the intensity,

PlotAAbs@ftff@alpDD2, 8alp, -3.5 Pi, 3.5 Pi<, PlotStyle ® 8Thick<, PlotRange ® AllE

-10 -5 5 10

0.02

0.04

0.06

0.08

0.10

This may (or may not) look familiar as the intensity pattern resulting from a single slit with incident plane

waves. This should make sense as a slit’s can be discribed as a step funciton and Fraunhofer diffraction

tells us the amplitude of a diffracted wave is the Forier transform of the aperture. You will learn more

about this is E&M.

Solving the ODE with Λ+= -2 and Λ- = -1 with initial condition

y[-1]=y’[-1]=0

ODE is (D^2+3D+2) y(x) = ff(x)

This is an overdamped oscillator at rest in equilibrium position which is pushed with constant accelera-

tion for 2 time units (thinking of x as time).

Here is the complementary solution, which we know well to be exponential fall offs.

DSolve@y''@xD + 3 y'@xD + 2 y@xD 0, y@xD, xD99y@xD ® ã

-2 xC@1D + ã

-xC@2D==

Solve directly:

RevisedODE1.nb 13

soln = DSolve@8y''@xD + 3 y'@xD + 2 y@xD ff@xD, y@-1D 0, y'@-1D 0<, y@xD, xD

::y@xD ®

0 x £ -1

1

2ã-2-2 x H-1 + ã1+xL2

-1 < x £ 1

1

2ã-2-2 x H-1 + ã2L H-1 - ã2 + 2 ã1+xL True

>>

Oscillator is pushed to one side, and continues moving after the push stops (at x=1) but then turns

around and damps.

plotsoln = Plot@y@xD . soln, 8x, -2, 6<, PlotStyle ® ThickD

-2 2 4 6

0.1

0.2

0.3

0.4

Solve using inverse Fourier transform

Here’s how we obtain the Fourier transform of ff[x] using Mathematica.

(This is worked out explicitly in the lecture notes.)

FullSimplify@FourierTransform@ff@xD, x, alp, FourierParameters ® 8-1, -1<DDSin@alpDalp Π

This is the function whose inverse Fourier transform gives the solution to the differential equation (as

discussed in lecture notes):

yft@alp_D = HSin@alpD HPi alpLL 1 HHI alp + 2L HI alp + 1LLSin@alpD

H1 + ä alpL H2 + ä alpL alp Π

Inverse Fourier Transform to obtain the solution;

solnft@x_D = InverseFourierTransform@yft@alpD, alp, x, FourierParameters ® 8-1, -1<D1

4

HCosh@2 + 2 xD - Sinh@2 + 2 xDL

H2 HeavisideTheta@1 + xD - 4 Cosh@1 + xD HeavisideTheta@1 + xD + 2 HeavisideTheta@-1 + xDHCosh@3D + Sinh@3DL H-ã + 2 Cosh@xD + 2 Sinh@xDL - 4 HeavisideTheta@1 + xD Sinh@1 + xD +

HSign@1 - xD + Sign@1 + xDL HCosh@2 + 2 xD + Sinh@2 + 2 xDLL

Note: InverseFourierTranform[ ] is exactly the same as FourierTransform[ ] but with

“FourierParameters®-a,-b”

14 RevisedODE1.nb

Note: InverseFourierTranform[ ] is exactly the same as FourierTransform[ ] but with

“FourierParameters®-a,-b”

FourierTransform@yft@alpD, alp, x, FourierParameters ® 8-1, -1<D ==

InverseFourierTransform@yft@alpD, alp, x, FourierParameters ® 81, 1<DTrue

plotsolnft = Plot@solnft@xD, 8x, -2, 6<, PlotStyle ® 8Thick, Red<D

-2 2 4 6

0.1

0.2

0.3

0.4

The solution agrees with that found directly by Mathematica.

Show@plotsoln, plotsolnftD

-2 2 4 6

0.1

0.2

0.3

0.4

RevisedODE1.nb 15