The Computable Differential Equation Lecture ... - Bruce E. Shapiro

The Computable Differential Equation Lecture ... - Bruce E. Shapiro The Computable Differential Equation Lecture ... - Bruce E. Shapiro

bruce.shapiro.com
from bruce.shapiro.com More from this publisher
21.04.2015 Views

22 CHAPTER 1. CLASSIFYING THE PROBLEM Math 582B, Spring 2007 California State University Northridge c○2007, B.E.Shapiro Last revised: May 23, 2007

CHAPTER 1. CLASSIFYING THE PROBLEM 23 Here is a more LISP-like implementation that returns the same, identical results: EulersMethod2[f , t0 , y0 , tmax , h ] := Module[{Euler}, Euler[{x , u }] := {x + h, u + h * f[x, u]}; ⇐= Return[NestList[Euler, {t0, y0}, Round[tmax/h]]] ] ⇐= This makes use of the function NestList . NestList implements fixed point iteration in Mathematica. For example, NestList[f, a, 3] returns three fixed point iterations of the function f starting at the point a: {a, f[a], f[f[a]], f[f[f[a]]]} In EulersMethod2 the fixed point iteration is being performed on the locally defined function Euler. Eulertakes a pair of numbers {t, y} and returns a new pair of numbers {t + h, y + hf(t, y)} These are same two numbers that are calculated at each step during the previous Euler’s method implementation. They are then used recursively by NestList as the new values of the parameters x and u. The number of iterations of NestList is determined by rounding the ratio t max /h, which might not otherwise be an integer. c○2007, B.E.Shapiro Last revised: May 23, 2007 Math 582B, Spring 2007 California State University Northridge

CHAPTER 1. CLASSIFYING THE PROBLEM 23<br />

Here is a more LISP-like implementation that returns the same, identical results:<br />

EulersMethod2[f , t0 , y0 , tmax , h ] :=<br />

Module[{Euler},<br />

Euler[{x , u }] := {x + h, u + h * f[x, u]}; ⇐=<br />

Return[NestList[Euler, {t0, y0}, Round[tmax/h]]]<br />

]<br />

⇐=<br />

This makes use of the function NestList . NestList implements fixed point<br />

iteration in Mathematica. For example,<br />

NestList[f, a, 3]<br />

returns three fixed point iterations of the function f starting at the point a:<br />

{a, f[a], f[f[a]], f[f[f[a]]]}<br />

In EulersMethod2 the fixed point iteration is being performed on the locally defined<br />

function Euler. Eulertakes a pair of numbers {t, y} and returns a new pair of<br />

numbers<br />

{t + h, y + hf(t, y)}<br />

<strong>The</strong>se are same two numbers that are calculated at each step during the previous<br />

Euler’s method implementation. <strong>The</strong>y are then used recursively by NestList<br />

as the new values of the parameters x and u. <strong>The</strong> number of iterations of<br />

NestList is determined by rounding the ratio t max /h, which might not otherwise<br />

be an integer.<br />

c○2007, B.E.<strong>Shapiro</strong><br />

Last revised: May 23, 2007<br />

Math 582B, Spring 2007<br />

California State University Northridge

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!