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

20 CHAPTER 1. CLASSIFYING THE PROBLEM This sets the local variable solution is going to be a list containing a single point (which itself is described by a list) {t0, y0} . The local variables y and t are then set equal to to the parameter values y0 and t0 respectively. Next we follow with a While loop. The format of a While loop is the following: While[expression, statement; statement; ] . statement; When Mathematica gets to this statement, it looks at the value of expression. If expression is True then it executes each statement in succession. If expression is False the rest of the While loop is ignored. After executing the full sequence of statements, Mathematica returns to the top of the loop and evaluates expression again. The process is repeated until expression has a value of False. In our case we have the following loop: While[t < tmax, y = y + h*f[t, y]; t = t + h; solution = Append[solution, {t, y}]; ]; Here we repeat the loop as long as the expression t < t max is true. Inside the loop the numbers y and t are updated according to specific formulas, and then the pair of numbers {t, y} is added to the end of solution. Since the value of t is updated each iteration, so long as the initial value of t (which is given by t0) is smaller than tmax , the loop will eventually terminate. If t0>tmax, then the program will have an infinite loop. A more robust program would test for this possibility and print an error message before actually attempting to execute the infinite loop. Finally, when the loop completes we have one final statement, Return[solution]; which tells us to return the list of values as the “value” of the function. This will be a list of numbers such as {{t 0 , y 0 }, {t 1 , y 1 }, . . . , {t n , y n }} =⇒ for some integer n = t max /h. To invoke the function one would first define the function f. For example, to solve the initial value problem y ′ = cos t − 2t sin t y(0) = 1 on the interval [0, 4π] with a step size of 0.5, one would type Math 582B, Spring 2007 California State University Northridge c○2007, B.E.Shapiro Last revised: May 23, 2007

CHAPTER 1. CLASSIFYING THE PROBLEM 21 f[t , y ]:= Cos[t] - 2t Sin[t]; solution=EulersMethod[f, 0, 1, 4Pi, .5] When we run the program it will return a list of values: {{0,1},{0.5,1.5},{1.,1.69908},{1.5, 1.12776},{2.,-0.333115}, {2.5,-2.35978},{3.,-4.25654},{3.5,-5.17489},{4.,-4.41538}, {4.5,-1.71499},{5.,2.5785},{5.5,7.51495},{6.,11.7498},{6.5, 13.9063}, {7.,12.9963},{7.5,8.77439},{8., 1.91271},{8.5,-6.07491},{9.,-13.1631}, {9.5,-17.3277},{10.,-17.1123},{10.5, -12.0917},{11.,-3.09262}, {11.5,7.90948},{12.,18.2188},{12.5,25.0796}} Since we set the value of the variable solution equal to the return value of EulersMethod, Mathematica will remember the values in this list and we can use them again later. For example, suppose we want to make a plot of the solution. We can do this with the function ListPlot: ListPlot[solution] This plots the list of points using dots to represent each point: To plot the figure with the dots connected, ListPlot[solution, PlotJoined->True] To get a plot with a blue line and with small red dots, plot1 = ListPlot[solution, PlotStyle -> Red, PointSize[.02]]; plot2 = ListPlot[solution, PlotJoined -> True, PlotStyle -> Blue]; Show[plot1, plot2]; c○2007, B.E.Shapiro Last revised: May 23, 2007 Math 582B, Spring 2007 California State University Northridge

20 CHAPTER 1. CLASSIFYING THE PROBLEM<br />

This sets the local variable solution is going to be a list containing a single point<br />

(which itself is described by a list) {t0, y0} . <strong>The</strong> local variables y and t are then<br />

set equal to to the parameter values y0 and t0 respectively. Next we follow with a<br />

While loop. <strong>The</strong> format of a While loop is the following:<br />

While[expression,<br />

statement;<br />

statement;<br />

]<br />

.<br />

statement;<br />

When Mathematica gets to this statement, it looks at the value of expression. If<br />

expression is True then it executes each statement in succession. If expression is<br />

False the rest of the While loop is ignored. After executing the full sequence of<br />

statements, Mathematica returns to the top of the loop and evaluates expression<br />

again. <strong>The</strong> process is repeated until expression has a value of False. In our case<br />

we have the following loop:<br />

While[t < tmax,<br />

y = y + h*f[t, y];<br />

t = t + h;<br />

solution = Append[solution, {t, y}];<br />

];<br />

Here we repeat the loop as long as the expression t < t max is true. Inside the loop<br />

the numbers y and t are updated according to specific formulas, and then the pair<br />

of numbers {t, y} is added to the end of solution. Since the value of t is updated<br />

each iteration, so long as the initial value of t (which is given by t0) is smaller than<br />

tmax , the loop will eventually terminate. If t0>tmax, then the program will have<br />

an infinite loop. A more robust program would test for this possibility and print an<br />

error message before actually attempting to execute the infinite loop. Finally, when<br />

the loop completes we have one final statement,<br />

Return[solution];<br />

which tells us to return the list of values as the “value” of the function. This will<br />

be a list of numbers such as<br />

{{t 0 , y 0 }, {t 1 , y 1 }, . . . , {t n , y n }}<br />

=⇒<br />

for some integer n = t max /h.<br />

To invoke the function one would first define the function f. For example, to<br />

solve the initial value problem<br />

y ′ = cos t − 2t sin t<br />

y(0) = 1<br />

on the interval [0, 4π] with a step size of 0.5, one would type<br />

Math 582B, Spring 2007<br />

California State University Northridge<br />

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

Last revised: May 23, 2007

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

Saved successfully!

Ooh no, something went wrong!