1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ -->
<head>
<title>Maxima Manual: Functions for numerical solution of differential equations</title>
<meta name="description" content="Maxima Manual: Functions for numerical solution of differential equations">
<meta name="keywords" content="Maxima Manual: Functions for numerical solution of differential equations">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="maxima_toc.html#Top" rel="start" title="Top">
<link href="maxima_264.html#g_t_0423_043a_0430_0437_0430_0442_0435_043b_044c-_0444_0443_043d_043a_0446_0438_0439-_0438-_043f_0435_0440_0435_043c_0435_043d_043d_044b_0445" rel="index" title="Указатель функций и переменных">
<link href="maxima_toc.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="maxima_83.html#Numerical" rel="up" title="Numerical">
<link href="maxima_90.html#g_t_041c_0430_0441_0441_0438_0432_044b" rel="next" title="Массивы">
<link href="maxima_88.html#Introduction-to-numerical-solution-of-differential-equations" rel="previous" title="Introduction to numerical solution of differential equations">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
div.smalllisp {margin-left: 3.2em}
kbd {font-style:oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
body {color: black; background: white; margin-left: 8%; margin-right: 13%;
font-family: "FreeSans", sans-serif}
h1 {font-size: 150%; font-family: "FreeSans", sans-serif}
h2 {font-size: 125%; font-family: "FreeSans", sans-serif}
h3 {font-size: 100%; font-family: "FreeSans", sans-serif}
a[href] {color: rgb(0,0,255); text-decoration: none;}
a[href]:hover {background: rgb(220,220,220);}
div.textbox {border: solid; border-width: thin; padding-top: 1em;
padding-bottom: 1em; padding-left: 2em; padding-right: 2em}
div.titlebox {border: none; padding-top: 1em; padding-bottom: 1em;
padding-left: 2em; padding-right: 2em; background: rgb(200,255,255);
font-family: sans-serif}
div.synopsisbox {
border: none; padding-top: 1em; padding-bottom: 1em; padding-left: 2em;
padding-right: 2em; background: rgb(255,220,255);}
pre.example {border: 1px solid rgb(180,180,180); padding-top: 1em;
padding-bottom: 1em; padding-left: 1em; padding-right: 1em;
background-color: rgb(238,238,255)}
div.spacerbox {border: none; padding-top: 2em; padding-bottom: 2em}
div.image {margin: 0; padding: 1em; text-align: center}
div.categorybox {border: 1px solid gray; padding-top: 1em; padding-bottom: 1em;
padding-left: 1em; padding-right: 1em; background: rgb(247,242,220)}
img {max-width:80%; max-height: 80%; display: block; margin-left: auto; margin-right: auto}
-->
</style>
<link rel="icon" href="figures/favicon.ico">
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6>"></script>
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
</head>
<body lang="ru" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Functions-for-numerical-solution-of-differential-equations"></a>
<div class="header">
<p>
Previous: <a href="maxima_88.html#Introduction-to-numerical-solution-of-differential-equations" accesskey="p" rel="previous">Introduction to numerical solution of differential equations</a>, Up: <a href="maxima_83.html#Numerical" accesskey="u" rel="up">Numerical</a> [<a href="maxima_toc.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="maxima_264.html#g_t_0423_043a_0430_0437_0430_0442_0435_043b_044c-_0444_0443_043d_043a_0446_0438_0439-_0438-_043f_0435_0440_0435_043c_0435_043d_043d_044b_0445" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Functions-for-numerical-solution-of-differential-equations-1"></a>
<h3 class="section">21.6 Functions for numerical solution of differential equations</h3>
<a name="plotdf"></a><a name="Item_003a-Numerical_002fdeffn_002fplotdf"></a><dl>
<dt><a name="index-plotdf"></a>Function: <strong>plotdf</strong> <em><br> <tt>plotdf</tt> (<var>dydx</var>, options…) <br> <tt>plotdf</tt> (<var>dvdu</var>, [<var>u</var>,<var>v</var>], options…) <br> <tt>plotdf</tt> ([<var>dxdt</var>,c<var>dydt</var>], options…) <br> <tt>plotdf</tt> ([<var>dudt</var>,c<var>dvdt</var>], [<var>u</var>,c<var>v</var>], options…)</em></dt>
<dd>
<p>The function <code>plotdf</code> creates a two-dimensional plot of the direction
field (also called slope field) for a first-order Ordinary Differential
Equation (ODE) or a system of two autonomous first-order ODE’s.
</p>
<p>Plotdf requires Xmaxima, even if its run from a Maxima session in
a console, since the plot will be created by the Tk scripts in Xmaxima.
If Xmaxima is not installed plotdf will not work.
</p>
<p><var>dydx</var>, <var>dxdt</var> and <var>dydt</var> are expressions that depend on
<var>x</var> and <var>y</var>. <var>dvdu</var>, <var>dudt</var> and <var>dvdt</var> are
expressions that depend on <var>u</var> and <var>v</var>. In addition to those two
variables, the expressions can also depend on a set of parameters, with
numerical values given with the <code>parameters</code> option (the option
syntax is given below), or with a range of allowed values specified by a
<var>sliders</var> option.
</p>
<p>Several other options can be given within the command, or selected in
the menu. Integral curves can be obtained by clicking on the plot, or
with the option <code>trajectory_at</code>. The direction of the integration
can be controlled with the <code>direction</code> option, which can have
values of <em>forward</em>, <em>backward</em> or <em>both</em>. The number of
integration steps is given by <code>nsteps</code>; at each integration
step the time increment will be adjusted automatically to produce
displacements much smaller than the size of the plot window. The
numerical method used is 4th order Runge-Kutta with variable time steps.
</p>
<p><b>Plot window menu:</b>
</p>
<p>The menu bar of the plot window has the following seven icons:
</p>
<p>An X. Can be used to close the plot window.
</p>
<p>A wrench and a screwdriver. Opens the configuration menu with several
fields that show the ODE(s) in use and various other settings. If a pair
of coordinates are entered in the field <em>Trajectory at</em> and the
<tt class="key">enter</tt> key is pressed, a new integral curve will be shown, in
addition to the ones already shown.
</p>
<p>Two arrows following a circle. Replots the direction field with the
new settings defined in the configuration menu and replots only the last
integral curve that was previously plotted.
</p>
<p>Hard disk drive with an arrow. Used to save a copy of the
plot, in Postscript format, in the file specified in a field of the
box that appears when that icon is clicked.
</p>
<p>Magnifying glass with a plus sign. Zooms in the plot.
</p>
<p>Magnifying glass with a minus sign. Zooms out the plot. The plot can be
displaced by holding down the right mouse button while the mouse is
moved.
</p>
<p>Icon of a plot. Opens another window with a plot of the two variables
in terms of time, for the last integral curve that was plotted.
</p>
<p><b>Plot options:</b>
</p>
<p>Options can also be given within the <code>plotdf</code> itself, each one being
a list of two or more elements. The first element in each option is the name
of the option, and the remainder is the value or values assigned to the
option.
</p>
<p>The options which are recognized by <code>plotdf</code> are the following:
</p>
<ul>
<li> <em>nsteps</em> defines the number of steps that will be used for the
independent variable, to compute an integral curve. The default value is
100.
</li><li> <em>direction</em> defines the direction of the independent
variable that will be followed to compute an integral curve. Possible
values are <code>forward</code>, to make the independent variable increase
<code>nsteps</code> times, with increments <code>tstep</code>, <code>backward</code>, to
make the independent variable decrease, or <code>both</code> that will lead to
an integral curve that extends <code>nsteps</code> forward, and <code>nsteps</code>
backward. The keywords <code>right</code> and <code>left</code> can be used as
synonyms for <code>forward</code> and <code>backward</code>.
The default value is <code>both</code>.
</li><li> <em>tinitial</em> defines the initial value of variable <var>t</var> used
to compute integral curves. Since the differential equations are
autonomous, that setting will only appear in the plot of the curves as
functions of <var>t</var>.
The default value is 0.
</li><li> <em>versus_t</em> is used to create a second plot window, with a
plot of an integral curve, as two functions <var>x</var>, <var>y</var>, of the
independent variable <var>t</var>. If <code>versus_t</code> is given any value
different from 0, the second plot window will be displayed. The second
plot window includes another menu, similar to the menu of the main plot
window.
The default value is 0.
</li><li> <em>trajectory_at</em> defines the coordinates <var>xinitial</var> and
<var>yinitial</var> for the starting point of an integral curve.
The option is empty by default.
</li><li> <em>parameters</em> defines a list of parameters, and their
numerical values, used in the definition of the differential
equations. The name and values of the parameters must be given in a
string with a comma-separated sequence of pairs <code>name=value</code>.
</li><li> <em>sliders</em> defines a list of parameters that will be changed
interactively using slider buttons, and the range of variation of those
parameters. The names and ranges of the parameters must be given in a
string with a comma-separated sequence of elements <code>name=min:max</code>
</li><li> <em>xfun</em> defines a string with semi-colon-separated sequence
of functions of <var>x</var> to be displayed, on top of the direction field.
Those functions will be parsed by Tcl and not by Maxima.
</li><li> <em>x</em> should be followed by two numbers, which will set up the minimum
and maximum values shown on the horizontal axis. If the variable on the
horizontal axis is not <var>x</var>, then this option should have the name of
the variable on the horizontal axis.
The default horizontal range is from -10 to 10.
</li><li> <em>y</em> should be followed by two numbers, which will set up the minimum
and maximum values shown on the vertical axis. If the variable on the
vertical axis is not <var>y</var>, then this option should have the name of
the variable on the vertical axis.
The default vertical range is from -10 to 10.
</li><li> <em>xaxislabel</em> will be used to identify the horizontal axis. Its default value is the name of the first state variable.
</li><li> <em>yaxislabel</em> will be used to identify the vertical axis. Its default value is the name of the second state variable.
</li><li> <em>number_of_arrows</em> should be set to a square number and defines the approximate
density of the arrows being drawn. The default value is 225.
</li></ul>
<p><strong>Examples:</strong>
</p>
<ul>
<li> To show the direction field of the differential equation <em>y' = exp(-x) + y</em> and the solution that goes through <em>(2, -0.1)</em>:
<div class="example">
<pre class="example">(%i1) plotdf(exp(-x)+y,[trajectory_at,2,-0.1])$
</pre></div>
<img src="figures/plotdf1.png" alt="figures/plotdf1">
</li><li> To obtain the direction field for the equation <em>diff(y,x) = x - y^2</em> and the solution with initial condition <em>y(-1) = 3</em>, we can use the command:
<div class="example">
<pre class="example">(%i1) plotdf(x-y^2,[xfun,"sqrt(x);-sqrt(x)"],
[trajectory_at,-1,3], [direction,forward],
[y,-5,5], [x,-4,16])$
</pre></div>
<p>The graph also shows the function <em>y = sqrt(x)</em>.
</p>
<img src="figures/plotdf2.png" alt="figures/plotdf2">
</li><li> The following example shows the direction field of a harmonic oscillator,
defined by the two equations <em>dz/dt = v</em> and <em>dv/dt = -k*z/m</em>,
and the integral curve through <em>(z,v) = (6,0)</em>, with a slider that
will allow you to change the value of <em>m</em> interactively (<em>k</em> is
fixed at 2):
<div class="example">
<pre class="example">(%i1) plotdf([v,-k*z/m], [z,v], [parameters,"m=2,k=2"],
[sliders,"m=1:5"], [trajectory_at,6,0])$
</pre></div>
<img src="figures/plotdf3.png" alt="figures/plotdf3">
</li><li> To plot the direction field of the Duffing equation, <em>m*x''+c*x'+k*x+b*x^3 = 0</em>, we introduce the variable <em>y=x'</em> and use:
<div class="example">
<pre class="example">(%i1) plotdf([y,-(k*x + c*y + b*x^3)/m],
[parameters,"k=-1,m=1.0,c=0,b=1"],
[sliders,"k=-2:2,m=-1:1"],[tstep,0.1])$
</pre></div>
<img src="figures/plotdf4.png" alt="figures/plotdf4">
</li><li> The direction field for a damped pendulum, including the
solution for the given initial conditions, with a slider that
can be used to change the value of the mass <em>m</em>, and with a plot of
the two state variables as a function of time:
<div class="example">
<pre class="example">(%i1) plotdf([w,-g*sin(a)/l - b*w/m/l], [a,w],
[parameters,"g=9.8,l=0.5,m=0.3,b=0.05"],
[trajectory_at,1.05,-9],[tstep,0.01],
[a,-10,2], [w,-14,14], [direction,forward],
[nsteps,300], [sliders,"m=0.1:1"], [versus_t,1])$
</pre></div>
<img src="figures/plotdf5.png" alt="figures/plotdf5"><img src="figures/plotdf6.png" alt="figures/plotdf6">
</li></ul>
</dd></dl>
<a name="Item_003a-Numerical_002fdeffn_002fploteq"></a><dl>
<dt><a name="index-ploteq"></a>Function: <strong>ploteq</strong> <em>(<var>exp</var>, ...options...)</em></dt>
<dd>
<p>Plots equipotential curves for <var>exp</var>, which should be an expression
depending on two variables. The curves are obtained by integrating the
differential equation that define the orthogonal trajectories to the solutions
of the autonomous system obtained from the gradient of the expression given.
The plot can also show the integral curves for that gradient system (option
fieldlines).
</p>
<p>This program also requires Xmaxima, even if its run from a Maxima session in
a console, since the plot will be created by the Tk scripts in Xmaxima.
By default, the plot region will be empty until the user clicks in a point
(or gives its coordinate with in the set-up menu or via the trajectory_at option).
</p>
<p>Most options accepted by plotdf can also be used for ploteq and the plot
interface is the same that was described in plotdf.
</p>
<p>Example:
</p>
<div class="example">
<pre class="example">(%i1) V: 900/((x+1)^2+y^2)^(1/2)-900/((x-1)^2+y^2)^(1/2)$
(%i2) ploteq(V,[x,-2,2],[y,-2,2],[fieldlines,"blue"])$
</pre></div>
<p>Clicking on a point will plot the equipotential curve that passes by that point
(in red) and the orthogonal trajectory (in blue).
</p>
</dd></dl>
<a name="rk"></a><a name="Item_003a-Numerical_002fdeffn_002frk"></a><dl>
<dt><a name="index-rk"></a>Function: <strong>rk</strong> <em><br> <tt>rk</tt> (<var>ODE</var>, <var>var</var>, <var>initial</var>, <var>domain</var>) <br> <tt>rk</tt> ([<var>ODE1</var>, …, <var>ODEm</var>], [<var>v1</var>, …, <var>vm</var>], [<var>init1</var>, …, <var>initm</var>], <var>domain</var>)</em></dt>
<dd>
<p>The first form solves numerically one first-order ordinary differential
equation, and the second form solves a system of m of those equations,
using the 4th order Runge-Kutta method. <var>var</var> represents the dependent
variable. <var>ODE</var> must be an expression that depends only on the independent
and dependent variables and defines the derivative of the dependent
variable with respect to the independent variable.
</p>
<p>The independent variable is specified with <code>domain</code>, which must be a
list of four elements as, for instance:
</p><div class="example">
<pre class="example">[t, 0, 10, 0.1]
</pre></div>
<p>the first element of the list identifies the independent variable, the
second and third elements are the initial and final values for that
variable, and the last element sets the increments that should be used
within that interval.
</p>
<p>If <var>m</var> equations are going to be solved, there should be <var>m</var>
dependent variables <var>v1</var>, <var>v2</var>, ..., <var>vm</var>. The initial values
for those variables will be <var>init1</var>, <var>init2</var>, ..., <var>initm</var>.
There will still be just one independent variable defined by <code>domain</code>,
as in the previous case. <var>ODE1</var>, ..., <var>ODEm</var> are the expressions
that define the derivatives of each dependent variable in
terms of the independent variable. The only variables that may appear in
those expressions are the independent variable and any of the dependent
variables. It is important to give the derivatives <var>ODE1</var>, ...,
<var>ODEm</var> in the list in exactly the same order used for the dependent
variables; for instance, the third element in the list will be interpreted
as the derivative of the third dependent variable.
</p>
<p>The program will try to integrate the equations from the initial value
of the independent variable until its last value, using constant
increments. If at some step one of the dependent variables takes an
absolute value too large, the integration will be interrupted at that
point. The result will be a list with as many elements as the number of
iterations made. Each element in the results list is itself another list
with <var>m</var>+1 elements: the value of the independent variable, followed
by the values of the dependent variables corresponding to that point.
</p>
<p>See also <code>`drawdf'</code>, <code><a href="maxima_82.html#desolve">desolve</a></code> and <code><a href="maxima_82.html#ode2">ode2</a></code>.
</p>
<p>Examples:
</p>
<p>To solve numerically the differential equation
</p>
<div class="example">
<pre class="example"> dx/dt = t - x^2
</pre></div>
<p>With initial value x(t=0) = 1, in the interval of t from 0 to 8 and with
increments of 0.1 for t, use:
</p>
<div class="example">
<pre class="example">(%i1) results: rk(t-x^2,x,1,[t,0,8,0.1])$
(%i2) plot2d ([discrete, results])$
</pre></div>
<p>the results will be saved in the list <code>results</code> and the plot will show the solution obtained, with <var>t</var> on the horizontal axis and <var>x</var> on the vertical axis.
</p>
<p>To solve numerically the system:
</p>
<div class="example">
<pre class="example"> dx/dt = 4-x^2-4*y^2 dy/dt = y^2-x^2+1
</pre></div>
<p>for t between 0 and 4, and with values of -1.25 and 0.75 for x and y at t=0:
</p>
<div class="example">
<pre class="example">(%i1) sol: rk([4-x^2-4*y^2, y^2-x^2+1], [x, y], [-1.25, 0.75],
[t, 0, 4, 0.02])$
(%i2) plot2d([discrete, makelist([p[1], p[3]], p, sol)], [xlabel, "t"],
[ylabel, "y"])$
</pre></div>
<p>The plot will show the solution for variable <var>y</var> as a function of <var>t</var>.
</p>
</dd></dl>
<hr>
<div class="header">
<p>
Previous: <a href="maxima_88.html#Introduction-to-numerical-solution-of-differential-equations" accesskey="p" rel="previous">Introduction to numerical solution of differential equations</a>, Up: <a href="maxima_83.html#Numerical" accesskey="u" rel="up">Numerical</a> [<a href="maxima_toc.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="maxima_264.html#g_t_0423_043a_0430_0437_0430_0442_0435_043b_044c-_0444_0443_043d_043a_0446_0438_0439-_0438-_043f_0435_0440_0435_043c_0435_043d_043d_044b_0445" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|