Você está na página 1de 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 ® ãã TanA E
>>
C@1D x
2

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 ® ã 3 TanA E
>>
x
2

Solution diverges at x=Pi


2 RevisedODE1.nb

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


3 TanA E
AxesLabel ® :"x", "y@xD=ã ">, Ticks ® 8Table@n * Π  4, 8n, - 4, 4<D, Automatic<F
x
2

3 TanB F
x

y@xD=ã 2

40

30

20

10

x
3Π Π Π Π Π 3Π
-Π - - - Π
4 2 4 4 2 4

This is a little easier to see with LogPlot

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


3 TanA E
AxesLabel ® :"x", "y@xD=ã ">, Ticks ® 8Table@n * Π  4, 8n, - 4, 4<D, Automatic<F
x
2

3 TanB F
x

y@xD=ã 2

105

1000

10

0.100

0.001

10-5
x
3Π Π Π Π Π 3Π
- - - 0 Π
4 2 4 4 2 4

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
RevisedODE1.nb 3

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
y

y0 =-1
2
y0 =0
1 y0 =1

x
Π 3Π
Π 2

2

-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].
4 RevisedODE1.nb

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
5

-1

-2
0 2 4 6

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

-1

0 1 2 3 4 5 6 7
RevisedODE1.nb 5

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
5

-1

0 2 4 6

Overlaying shows that the head-to-tail arrows really do map out the solutions
6 RevisedODE1.nb

Show@streamplot, solnplotD
5

-1

0 2 4 6

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).
RevisedODE1.nb 7

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


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

10 8

4 4 4
8
6

3 6

2 0 2

0 -2

-4 -2
-1

-6

-2 -4
0 1 2 3 4 5 6 7

Solutions and contours indeed match:


Show@curvefamily, solnplotD
5

10 8

4 4 4
8
6

3 6

2 0 2

0 -2

-4 -2
-1

-6

-2 -4
0 1 2 3 4 5 6 7

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


8 RevisedODE1.nb

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


8x, 0, 7<, 8y, - 2, 5<, ContourLabels ® True, Contours ® 25D, solnplotD
5 5.84 7.3 5.11 2.19 5.11 8.038.76
8.03 10.95 6.57
10.22
9.49 7.3
4 8.76 2.92
3.65
3.65
0.73 5.84
6.57
3
4.38
4.38

2
2.19
2.92 y0 =-1
0 y0 =0
1.46
1.46
1
y0 =1

0 -2.19

-0.73
-4.38 -2.19
-1
-5.11
-5.84
-3.65
-6.57
-2 -2.92 -3.65 -1.46 -1.46 -2.92-4.38
0 1 2 3 4 5 6 7

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.
RevisedODE1.nb 9

orthogcurves = StreamPlot@81, - 1  HSin@2 xD - y Cos@xDL<,


8x, 0, 7<, 8y, - 2, 5<, StreamStyle ® 8Red, Thick<D

-2

0 2 4 6

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
5

10 8

4 4 4
8
6

3 6

2 0 2

0 -2

-4 -2
-1

-6

-2 -4
0 1 2 3 4 5 6 7

Exact first order equation


10 RevisedODE1.nb

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
y

20

y0 =10
10
y0 =-10

x
2 4 6 8 10

-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
RevisedODE1.nb 11

ContourPlot@f845@x, yD, 8x, 0, 10<, 8y, - 10, 20<,


ContourLabels ® True, FrameLabel ® 8"x", "y"<, PlotLabel ® "contours of F"D
contours of F
20 -50

-150

15
-100

10

100
5
y

50
0 0

-5

-50

-10
0 2 4 6 8 10
x

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, xD
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.
DSolve@y ''@xD - 2 y '@xD - 3 y@xD Š 16 x ^ 2 Exp@- xD, y@xD, xD

::y@xD ® - ã-x I3 + 12 x + 24 x2 + 32 x3 M + ã-x C@1D + ã3 x C@2D>>


1
24
12 RevisedODE1.nb

Solving an ODE using Fourier transforms (following lecture notes)


Here’s the step function:
ff@x_D = Piecewise@881, - 1 £ x £ 1<<D
1 -1 £ x £ 1
0 True

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

1.0

0.8

0.6

0.4

0.2

-2 -1 1 2

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<D
Sin@alpD
alp Π

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-

 b¤
à
¥
fHtL ei b Ω t dt.
H2 ΠL
tion as:
1-a -¥
RevisedODE1.nb 13

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

0.3

0.2

0.1

-10 -5 5 10

Mod squaring this gives the intensity,

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

0.10

0.08

0.06

0.04

0.02

-10 -5 5 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, xD
99y@xD ® ã-2 x C@1D + ã-x C@2D==

Solve directly:
14 RevisedODE1.nb

soln = DSolve@8y ''@xD + 3 y '@xD + 2 y@xD Š ff@xD, y@- 1D Š 0, y '@- 1D Š 0<, y@xD, xD
0 x £ -1
::y@xD ® H- 1 + ã1+x L2 - 1 < x £ 1 >>
1
2
ã-2-2 x
H- 1 + ã2 L H- 1 - ã1+x L
1
2
ã-2-2 x ã2 +2 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
0.4

0.3

0.2

0.1

-2 2 4 6

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<DD
Sin@alpD
alp Π

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 + 1LL
Sin@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<D

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


1
4
H2 HeavisideTheta@1 + xD - 4 Cosh@1 + xD HeavisideTheta@1 + xD + 2 HeavisideTheta@- 1 + xD
HCosh@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}”
RevisedODE1.nb 15

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<D
True

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


0.4

0.3

0.2

0.1

-2 2 4 6

The solution agrees with that found directly by Mathematica.


Show@plotsoln, plotsolnftD
0.4

0.3

0.2

0.1

-2 2 4 6

Você também pode gostar