The Computable Differential Equation Lecture ... - Bruce E. Shapiro
The Computable Differential Equation Lecture ... - Bruce E. Shapiro The Computable Differential Equation Lecture ... - Bruce E. Shapiro
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
- Page 1 and 2: The Computable Differential Equatio
- Page 3 and 4: Contents 1 Classifying The Problem
- Page 5 and 6: CONTENTS v Timeline on Computable D
- Page 7 and 8: Chapter 1 Classifying The Problem 1
- Page 9 and 10: CHAPTER 1. CLASSIFYING THE PROBLEM
- Page 11 and 12: CHAPTER 1. CLASSIFYING THE PROBLEM
- Page 13 and 14: CHAPTER 1. CLASSIFYING THE PROBLEM
- Page 15 and 16: CHAPTER 1. CLASSIFYING THE PROBLEM
- Page 17 and 18: CHAPTER 1. CLASSIFYING THE PROBLEM
- Page 19 and 20: CHAPTER 1. CLASSIFYING THE PROBLEM
- Page 21 and 22: CHAPTER 1. CLASSIFYING THE PROBLEM
- Page 23 and 24: CHAPTER 1. CLASSIFYING THE PROBLEM
- Page 25: CHAPTER 1. CLASSIFYING THE PROBLEM
- Page 29 and 30: CHAPTER 1. CLASSIFYING THE PROBLEM
- Page 31 and 32: Chapter 2 Successive Approximations
- Page 33 and 34: CHAPTER 2. SUCCESSIVE APPROXIMATION
- Page 35 and 36: CHAPTER 2. SUCCESSIVE APPROXIMATION
- Page 37 and 38: CHAPTER 2. SUCCESSIVE APPROXIMATION
- Page 39 and 40: CHAPTER 2. SUCCESSIVE APPROXIMATION
- Page 41 and 42: CHAPTER 2. SUCCESSIVE APPROXIMATION
- Page 43 and 44: CHAPTER 2. SUCCESSIVE APPROXIMATION
- Page 45 and 46: CHAPTER 2. SUCCESSIVE APPROXIMATION
- Page 47 and 48: CHAPTER 2. SUCCESSIVE APPROXIMATION
- Page 49 and 50: Chapter 3 Approximate Solutions 3.1
- Page 51 and 52: CHAPTER 3. APPROXIMATE SOLUTIONS 45
- Page 53 and 54: CHAPTER 3. APPROXIMATE SOLUTIONS 47
- Page 55 and 56: CHAPTER 3. APPROXIMATE SOLUTIONS 49
- Page 57 and 58: CHAPTER 3. APPROXIMATE SOLUTIONS 51
- Page 59 and 60: CHAPTER 3. APPROXIMATE SOLUTIONS 53
- Page 61 and 62: CHAPTER 3. APPROXIMATE SOLUTIONS 55
- Page 63 and 64: CHAPTER 3. APPROXIMATE SOLUTIONS 57
- Page 65 and 66: CHAPTER 3. APPROXIMATE SOLUTIONS 59
- Page 67 and 68: CHAPTER 3. APPROXIMATE SOLUTIONS 61
- Page 69 and 70: CHAPTER 3. APPROXIMATE SOLUTIONS 63
- Page 71 and 72: CHAPTER 3. APPROXIMATE SOLUTIONS 65
- Page 73 and 74: CHAPTER 3. APPROXIMATE SOLUTIONS 67
- Page 75 and 76: Chapter 4 Improving on Euler’s Me
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