Math 230, Spring 1999
Mathematica assignment 3
(due Monday, 3/1/99)

The following problems are taken from pages 101 to 107 of the Differential Equations with Mathematica book.  In problems requiring the use of Euler's method, feel free to use my
version of the EulerMethod command rather than the book's version.   The main difference is that my method returns a function whereas the book's returns a table of values.  You should also compare results obtained with Euler's method with results obtained using the Runge-Kutta method.

Problem 1.

First we find explicit solutions to y' = (y^2)and y' = 1+ (y^2)with y(0) = 1.

sol1  =  
      y[x] /. First[
          DSolve[{y'[x]  ==  y[x]^2,  y[0]  ==  1},  y[x],  x]]

sol2  =  
      y[x] /. First[
          DSolve[{y'[x]  ==  y[x]^2  + 1,  y[0]  ==  1},  y[x],  
            x]]

\(plot1\  = \ Plot[sol1, {x, 0, 1}]; \ 

    plot2\  = \ Plot[sol2, {x, 0, Pi/4}]; 
Show[\ plot1, \ plot2]; \)

\(TextForm
    \`Now\ if\ I\ call\ the\ functions\ I\ just\ graphed\ \(y\_\(1\ \)\) and\ 
      y\_2\ and\ I\ let\ y\_3\)be the solution to y'  =  (x^2)  +  (y^s),  
    y (0)  =  1,  then I expect\(TextForm
    \`that\ \(y\_3\) will\ lie\ between\ \(y\_\(1\ \)\) and\ 
      y\_2 . \ \ \ This\ is\ because, \ first\ of\ all, \ all\ \)three solutions agree at x = 0.     Secondly,  
    if  0  ≤  x  ≤ 1,  and (Subscript[y, 1 ]) ≤  (Subscript[y, 3]) ≤ (Subscript[y,  2]),  
    then it  follows that  the right sides of the\(TextForm\`corresponding\ differential\ equations\ satisfy
\)\(TextForm\`\ \ \ \ \ \ \ \ 
    \((\ y\_\(1\ \ \))\)\^2 ≤ \ \ \((y\_3)\)\^2 + \ x\^2 
      ≤ \ \((y\_\(\ 2\))\)\^2\  + \ 1. 
\)\(TextForm\`Hence\ the\ slopes\ of\ the\ three\ functions\ will\ satisfy

    \)\(TextForm\`\ \ \ \ \ \ \ \ \ 
    \((y\_\(1\ \)\ )\)' ≤ \ \((y\_3)\)' ≤ 
      \((y\_\(\ 2\))\)' . 

So\ as\ x\ increases\ we\ will\ continue\ to\ 
        have\ \ \ \(y\_\(1\ \)\) \((x)\)\  ≤ \ \(y\_3\) \((x)\) 
      ≤ \(y\_\(\ 2\)\) \((x)\)\ \)for all x  ≤  1.

From these observations and the formulae for   (Subscript[y, 1 ]) and (Subscript[y, 2]), we see
that  (Subscript[y, 3]) will necessarily tend to infinity as x goes to
some point x=a between π/4 and 1.  To confirm this, we solve
numerically:

sol3  =  
      y[x] /. First[
          NDSolve[{y'[x]  ==  y[x]^2  + x^2,  y[0]  ==  1},  y[x], 
             {x, 0, 1}]]

We then plot and compare with the previous two graphs.  We'll have
to play with the range a lot in order to find the location of the 
vertical asymptote of the new function.

\(plot3\  = \ Plot[sol3, {x, 0,  .967}]; 
Show[plot1, plot2, plot3]; 
    \)

So the last solution really does lie between the other two 
solutions, and it has a vertical asymptote at about x = .967.

\(\(








\)\)

Problem 4.

a.

\(sol[x_, b_]\  := \ 
      y[x] /. First[
          NDSolve[

            				\ {
              \(y'\)[x]\  == \ \((y[x]\  - \ x^2)\) \((1 - y[x]^2)\), \ 
              y[0]\  == \ b}, y[x], \ {x, 0, 5}]]\)

Plot[Evaluate[Table[sol[x, b], {b, -2, 4,  .25}]], {x, 0, 4}, 
      PlotRange -> {-2, 4}];

b.

Looking at the ODE, it's clear that y = -1 and y=1 are constant
solutions.  It looks like any solution that starts off with y(0)<1
will tend eventually to y= -1 as x goes to infinity.  It's worth
pointing out however that solutions with 0<y(0) < 1 are actually
increasing for small values of x before they turn around and 
decrease to y=-1.  Solutions that begin with y(0) > 1 seem to
decrease initially and then turn around and head for infinity
as x increases.  Just to make sure about this last point, it's worth
plotting solutions with y(0) > 1 on a larger scale:

plot1  =  
      Plot[Evaluate[Table[sol[x, b], {b, 1, 4,  .25}]], {x, 0, 5}, 
        PlotRange -> { .5, 25}];

This is truly peculiar behavior .   All these solutions develop
        a `kink' around x  =  2.25 .   Nevertheless,  
          they do all seem to \(go\ to\ infinity\ as\ x\ increases . \ \ Since\ y'\  ≤ 
        0\ when\ y ≥ x\^2\ and\ y ≥ 1, \ 

      it' s\ apparent\ that\ no\ solution\ will\ remain\ above\ y = 
        x\^2\ for\)long .   However,  
      it does look like solutions might be asymptoticto y = 
        (x^2) .   We can tell best if we plot them along side the 
          parabola :

c.

Show[plot1,  Plot[x^2, {x, 0, 5}],  PlotRange -> {0, 25}];

...an asymptote if ever there was one!

Needs["Graphics`PlotField`"]

\(\(PlotVectorField[{1, \((y - x^2)\) \((1 - y^2)\)}, 
      \ {x, 0, 5}, {y, 0, 25}, \ Axes -> \ True, 
	
      ScaleFunction -> \((1&)\), Ticks -> None, Frame -> True, 
      AspectRatio -> 1, \ PlotPoints\  -> \ 30]; \)\)

The arrows in the direction field seem to be nearly vertical
except on the parabola.  The arrows above the parabola point
down and those below point up.  Hence any solution is going to be
driven to converge to the parabola as x increases.

\(\(








\)\)

Problem 12.

a.

I copy in the EulerMethod routine from one of the class demos:

\(\(EulerMethod[\ f_, {x0_, \ y0_}, \ h_, \ n_\ ]\  := \ 

		
      Module[
		\ \ \ {EMStep}, 
		\ \ \ 
        Interpolation[
\ \ \ 	\ \ \ \ \ \ \ \ \ 
          EMStep[{x_, y_}]\  := \ N[{x + h, y + h*f[x, y]}]; 

          		\ \ \ \ \ \ \ \ \ \ NestList[EMStep, {x0, y0}, n], 

          			\ \ \ \ \ \ \ InterpolationOrder\  -> \ 1
		\ \ \ ]

        	\ \ ]\ \ \ \ \ \ \ \ \ \ \ \ \ \)\)

Now I apply to y' = f(x,y) where

f[x_, y_]  =  4  x - 2  y/x

(Notice the problem at x=0!)

\(sol1\  = \ EulerMethod[f, {\(- .5\), 4.35},  .2, 10]; 

    Plot[sol1[x], {x, \(- .5\), 1.5}, \ PlotRange -> \ {\(-260\), 100}]; \)

What a mess!

b.

\(sol2\  = \ EulerMethod[f, {\(- .5\), 4.35},  .1, 20]; 

    Plot[sol2[x], {x, \(- .5\), 1.5}, \ PlotRange -> \ {\(-260\), 100}]; \)

This one is even worse.  We might've expected that things would
go wrong around x=0.  It's a little surprising that the Euler
Method command didn't balk with a step size of .1, since at the
fifth step x lands right on 0.

c.

\(sol3\  = \ 
      y[x] /. First[
          NDSolve[{\(y'\)[x] == f[x, y[x]], \ y[\(- .5\)]\  == \ 4.35}, y[x], 
            
		\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ {x, \(- .5\), 1.5}]]\)

It's choking around x=0 again.  We'll just plot what comes before
that:

Plot[sol3, {x, - .5, 0}];

Sure enough, there's a vertical asymptote at x=0.  There's
no reasonable way to continue the solution (numerically or
otherwise) past this point.

d.

Mathematica won't do the first one on its own.  We'll settle
for the general solution to the ODE and then solve the initial
value problem ourselves:

\(\(			\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 
    DSolve[{\(y'\)[x]\  == \ f[x, y[x]]}, y[x], x]\)\)

To get y (0)  =  0,  
    we need to take C[1]  =  0.   That is,  
    y (x)  =  (x^2) .

sol1  =  x^2

The other initial value problems are easier.

\(sol2 = \ 
      y[x] /. First[
			\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 
          DSolve[{\(y'\)[x]\  == \ f[x, y[x]], y[\(- .5\)] == 4.25}, y[x], x]
            
		\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ]\)\(sol3 = \ 
      y[x] /. First[
			\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 
          DSolve[{\(y'\)[x]\  == \ f[x, y[x]], y[ .5] == 4.25}, y[x], x]

          		\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ]\)\(	sol4 = \ 
      y[x] /. First[
			\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 
          DSolve[{\(y'\)[x]\  == \ f[x, y[x]], y[\(- .5\)] == 3.75}, y[x], x]
            
		\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ]\)\(	sol5 = \ 
      y[x] /. First[
			\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 
          DSolve[{\(y'\)[x]\  == \ f[x, y[x]], y[ .5] == 3.75}, y[x], x]

          		\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ]\)

The first two and last two of these are the same.  Hence we need only
plot three of the five solutions.

Plot[{sol1,  sol2,  sol4}, {x, -3, 3},  
      PlotRange -> {-1, 20}];

The solution y =  
        (x^2) is a little surprising but the asymptotesin the other solutions match up very well with the x in
             the denominator of the original equation .

\(\(







\)\)

Problem 15.

a.

f[x_, y_]  :=  2  y - 2 + 3  E^(-x)

sol1  =  EulerMethod[f, {0, 0},  .2, 10]

plot1  =  Plot[sol1[x], {x, 0, 2}];

Based on this graph, it looks like y(x) is going to infinity as
x increases.

b.

sol2  =  EulerMethod[f, {0, 0},  .1, 20]plot2  =  Plot[sol2[x], {x, 0, 2}];

sol3  =  EulerMethod[f, {0, 0},  .05, 40]plot3  =  Plot[sol3[x], {x, 0, 2}];

Looks like there's a problem here.  The graphs appear to be identical
but their ranges are different.  If we put them all together, we see:

Show[plot1, plot2, plot3];

Long term predictions based on these approximate solutions
are going to be unreliable.

c.

\(sol4\  = \ 
      y[x] /. First[
          NDSolve[

            				\ \ \ \ \ \ \ {
              \(y'\)[x]\  == \ 2  y[x] - 2 + 3  E^\((\(-x\))\), \ y[0] == 0}, 
            \ y[x], \ {x, 0, 2}]]\)\(	\(Plot[sol4, {x, 0, 2}]; \)\)

Now it looks like y might be headed for a finite limit as x increases.
Can't tell though without checking some larger values of x.

\(sol5\  = \ 
      y[x] /. First[
          NDSolve[

            				\ \ \ \ \ \ \ {
              \(y'\)[x]\  == \ 2  y[x] - 2 + 3  E^\((\(-x\))\), \ y[0] == 0}, 
            \ y[x], \ {x, 0, 10}]]\)\(	\(Plot[sol5, {x, 0, 10}]; \)\)

Now it looks like y is headed to negative infinity!

d.

\(sol6\  = \ 
      Expand[y[x] /. 
          First[DSolve[

              			\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ {
                \(y'\)[x]\  == \ 2  y[x] - 2 + 3  E^\((\(-x\))\), \ 
                y[0] == 0}, \ \ \ \ \ 

              			\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ y[x], x]]]
        \)Plot[sol6, {x, 0, 10}];

\(TextForm
    \`So\ the\ truth\ is\ that\ y\ is\ tending\ to\ 1\ as\ x\ 
      increases . \ \ My\ guess\ is\ that\)\(TextForm
    \`the\ solution\ y \((x)\)\  = 
      1 - E\^\(-x\)\ is\ very\ unstable\ in\ the\ sense\ that\ any\)\(TextForm
    \`solution\ that\ starts\ near\ this\ one\ diverges\ rapidly\ as\ x\ 
      increases . 
This\ means\ that\ when\ I\ seek\ a\ numerical\ 
      approximation, \ and\ small\)\(TextForm
    \`error\ in\ the\ approximation\ is\ quickly\ magnified\ for\ larger\ 
      values\)of x .   To check my theory,  
    I' ll solve the ODE for several initial\(TextForm
    \`values\ close\ to\ zero\ and\ plot\ all\ the\ solutions\ 
      \(together . \)\)

\(sol[x_, b_]\  = \ 
      y[x] /. First[
          DSolve[\ 

            				\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ {
              \(y'\)[x]\  == \ 2  y[x] - 2 + 3  E^\((\(-x\))\), \ y[0] == b}, 
            \ \ \ \ \ 
			\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 
            y[x], x]]\)

\(\(Plot[
      Evaluate[Table[sol[x, b], {b, \(- .001\),  .001,  .0002}]], {x, 0, 10}, 
      
PlotRange -> {\(-5\), 5}]; \)\)

That's pretty unstable looking!

\(TextForm\`\(









\)\)

Quadratic Version of Euler's Method

Euler' s method for approximating solutions of                     y' (x)  =  f (x, y), 
             y ((Subscript[x, 0]))  =  (Subscript[y, 0])works by assuming that y is approximately linear on small 
          intervals . Describe a variation on Euler's method that works by assuming that y is quadratic on small intervals .  Implement this new method as a Mathematica command called QuadMethod.  Use my EulerMethodand (especially) RKMethod commands as models for syntax, etc .   When you get your QuadMethod command working, try to answer the following questions by applying the
             \(command\ in\ specific\ examples 
        \((e . g . \ the\ y'\  = \ y, \ y \((0)\)\  = \ 1\ example; \ \ 
          also\ try\ y'\  = \ 3  y/x, 

          y \((1)\)\  = \ 
            1\ to\ check\ that\ your\ method\ works\ for\ non - 
              autonomous\ ODE' s . 
	
\ \ \ \ \ \ \((a)\)\ How\ do\ local\ 
                and\ global\ truncation\ errors\ depend\ on\ h?\ \ That\ is, 
          \ are\ they\ about\ 
	\ \ \ \ \ \ \ \ equal\ to\ Ch?\ to\ 
              \(Ch\^2\)?\ \ etc . 
		
\ \ \ \ \ \ \((b)\)\ Does\ the\ new
              \ method\ work\ better\ or\ worse\ than\ Euler' s\ \ \ 
              method?\ \ than\ the\ Runge - 
	\ \ \ \ \ \ \ \ \ 
            Kutta\ \(method?\)\ \)\)

Solution

\(RKMethod[f_, {x0_, y0_}, h_, n_]\  := 
	
\ \ \ \ \ 
      Module[
		\ \ \ \ \ \ \ {RKStep}, 
		
		\ \ \ \ \ \ \ \ 
        RKStep[{x_, y_}]\  := \ 
			\ \ \ \ \ \ 
          Module[{k1, k2, k3, k4}, 

            		\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ k1\  = \ f[x, y]; 
            
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 
            k2\  = \ f[x + h/2, \ y + \ k1*h/2]; 

            		\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 
            k3\  = \ f[x + h/2, y + \ k2*h/2]; 
										\ \ 
            k4\ \  = \ f[x + h, y + k3*h]; 

            		\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 
            N[{x + h, y + h*\((k1\  + \ 2*k2\  + \ 2*k3\  + \ k4)\)/6}, 12]

            			\ \ \ \ \ ]; 
		
		\ \ \ \ \ \ \ \ 
        Interpolation[
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 
          NestList[RKStep, {x0, y0}, n], 
			\ \ \ \ \ \ \ \ \ \ \ \ 
          InterpolationOrder\  -> \ 1
			\ \ \ \ \ ]
	\ \ \ \ \ ]\)

\(TextForm
    \`My\ QuadMethod\ command\ will\ be\ similar\ to\ Euler' s\ method, \ but
    \)instead of using linear approximations to estimate y, 
     I will\(TextForm
    \`use\ quadratic\ approximation\ 
        \(\((i . e . \ the\ first\ three\ terms\ in
the\ Taylor\ series)\) : 
          
				
		y \((x)\)\)\  = \ 
      y \((x\_0)\)\  + \ y' \((x\_0)\) \((x - x\_0)\)\  + 
        \(\(\(y'\)' \((x\_0)\)\)\/2\) \((x - x\_0)\)\^2
As\ with\ Euler' s\ 
          method, \ 
    I\ get\ y' \((x\_0)\)\ from\ the\ differential
equation
		\ \ \ \ \ 
        y' = \ f \((x, y)\) . 
To\ get\ \(y'\)' \((x\_0)\), \ 
    I\ differentiate\ this\ equation\ with\ respect\ to\ 

        \(x : 
		\ \ \ \ \ \(y'\)'\) = \ 
      \(∂f\/∂x\ dx\/dx\  + \ \ 
          ∂f\/∂y\ dy\/dx\  = \ \ 
        ∂f\/∂x\ \  + \ \ 
          ∂f\/∂y\ \(f . \)\)

		\)

\(QuadMethod[f_, {x0_, y0_}, h_, n_]\  := 

		
      Module[
		\ \ \ \ \ \ \ \ {QMStep, \ deriv2}, 		

        		\ \ \ \ \ \ \ \ 
		\ \ \ \ \ \ \ \ 
        deriv2[x_, y_]\  = \ 
          ∂\_x f[x, y]\  + \ f[x, y]*∂\_y f[x, y]; 

        \ \ \ \ \ \ \ \ \ \ \ \ \ 
        QMStep[{x_, y_}]\  := \ 
          N[{x + h, 
              y + h*f[x, y]

                								\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \  + \((h^2)\)*deriv2[x, y]/2. }, 
            20]; 
					
        Interpolation[
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 
          NestList[QMStep, {x0, y0}, n], 
			\ \ \ \ \ \ \ \ \ \ \ \ 
          InterpolationOrder\  -> \ 2
			\ \ \ \ \ ]	
	\ \ ]\)

Note that I'm setting the InterpolationOrder option to 2 this time.
		
For starters, we'll try this method out on the initial value problem
    y'= y,   y(0) = 1,
comparing our results for a given step size with those from Euler's
method and the Runge-Kutta method.

f[x_, y_]  :=  y;

\(sol1\  = \ QuadMethod[f, {0, 1},  .25, 4]; 

    sol2\  = \ EulerMethod[f, {0, 1},  .25, 4]; 

    sol3\  = \ RKMethod[f, {0, 1},  .25, 4]; \)

Plot[{sol1[x], sol2[x], sol3[x], E^x}, {x, 0, 1}];

Only one of these four graphs is much different from the others.
I'll bet it's the EulerMethod graph.  Let's just look at values
at x=1 instead:

sol1[1]sol2[1]sol3[1]N[E^1, 6]

So the Runge-Kutta approximation looks best by about three digits!
At least our new method beats the Euler approximation.   Now we
consider the issue of global approximation error.   I define a 
function that takes a given step size h and gives me the difference
between my approximate solution (based on the new method with
step size h) at x=1and the correct value of e .

error[h_]  :=  
      (QuadMethod[f, {0, 1}, h, IntegerPart[2. /h]])[1]  -  E

error[ .1]

error[ .01]

error[ .001]

error[ .0001]

Every time I decrease h by a factor of ten,  
    I gain about a factor of\(TextForm
    \`one\ hundred\ in\ accuracy . \ \ It\ looks\ like\ I\ have\ a\ global\ 
      approximation\)error proportional to (h^2) .   Again,  
    this is worse than Runge - Kuttabut better than Euler' s Method .

Finally I consider local approximation error

local[h_]  :=  (QuadMethod[f, {0, 1}, h, 2])[h]  -  E^h

local[ .1]

local[ .01]

local[ .001]

local[ .0001]

\(TextForm
    \`So\ with\ every\ factor\ of\ 10\ decrease\ in\ step\ size\ I\ seem\ to\ 
      be\ getting\)\(TextForm
    \`a\ factor\ of\ 1000\ increase\ in\ accuracy . \ \ It\ looks\ like\ local
      \ \)truncation error is proportional to (h^3) .