The Computable Differential Equation Lecture ... - Bruce E. Shapiro
The Computable Differential Equation Lecture ... - Bruce E. Shapiro
The Computable Differential Equation Lecture ... - Bruce E. Shapiro
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
CHAPTER 3. APPROXIMATE SOLUTIONS 59<br />
using ForwardEuler on [0, 2π] using h = 0.1π, and plot the results.<br />
Solution. First we define the function and the initial conditions, then we solve the<br />
system.<br />
y0={1,0};<br />
f[t , y ]:= {y[[2]], -2y[[1]]};<br />
r=ForwardEuler[f,{0, y0}, 0.1π, 2π];<br />
<strong>The</strong> function ListPlot can be used to plot either points or a joined line, but not<br />
both on the same function call. To avoid having to repeatedly call ListPlot , we<br />
define the following function JoinedListPlotwhich will plot a set of points as a a<br />
sequence of connected dots.<br />
JoinedListPlot[data , color , opt ?OptionQ] := Show[<br />
ListPlot[data, PlotJoined -> True, PlotStyle -> color,<br />
DisplayFunction -> Identity],<br />
ListPlot[data, PlotStyle -> color, PointSize[0.02],<br />
DisplayFunction -> Identity],<br />
opt, DisplayFunction -> $DisplayFunction]<br />
<strong>The</strong> two calls to ListPlot generate the plot with the connected lines (the first<br />
call, with PlotJoined->True); and then, the plot with the dots (the second call).<br />
<strong>The</strong> options DisplayFunction->Identity tell Mathematica to actually suppress<br />
actually showing the individual plots, and to just calculate the graphical elements<br />
that make up the plots. <strong>The</strong> call to Show then renders the two plots generated by<br />
ListPlot by using the option DisplayFunction->$DisplayFunction. <strong>The</strong> argument<br />
opt is used to pass any desired options to the Showfunction.<br />
We actually want to generate two plots for our simulation: one for y 1 , and<br />
the other for y 2 , but we want them plotted on a single graph. Our new function<br />
JoinedListPlot will plot a single data set, but not two data sets. But we<br />
can repeat the same trick we used with the definition of JoinedListPlot: set<br />
DisplayFunction->Identityand then call the function twice. In outline form,<br />
p1 = JoinedListPlot[FirstDataSet, Red, DisplayFunction->Identity];<br />
p2 = JoinedListPlot[SecondDataSet, Blue, DisplayFunction->Identity];<br />
Show[p1, p2, DisplayFunction -> $DisplayFunction]<br />
<strong>The</strong> main problem now is to extract the data sets. <strong>The</strong> data is in the form<br />
{ {{t 0 , {y 0,1 , y 0,2 , . . . }}, {{t 1 , {y 1,1 , y 1,2 , . . . }}, . . . }<br />
and what we need for each plot is<br />
{ {t 0 , y 0,1 }, {t 1 , y 1,1 }, {t 2 , y 2,1 }, . . . }<br />
c○2007, B.E.<strong>Shapiro</strong><br />
Last revised: May 23, 2007<br />
Math 582B, Spring 2007<br />
California State University Northridge