Debian Codegraph @

texmf-dist/source/latex/mfpic4ode/mfpic4ode.dtx

raw
1 % \iffalse meta-comment
2 %
3 % Copyright (C) 2006-2008 by Robert Marik <[email protected]>
4 % ----------------------------------------------------------
5 %
6 % This file may be distributed and/or modified under the
7 % conditions of the LaTeX Project Public License, either version 1.2
8 % of this license or (at your option) any later version.
9 % The latest version of this license is in:
10 %
11 % http://www.latex-project.org/lppl.txt
12 %
13 % and version 1.2 or later is part of all distributions of LaTeX
14 % version 1999/12/01 or later.
15 %
16 % \fi
17 %
18 % \iffalse
19 %<*driver>
20 \ProvidesFile{mfpic4ode.dtx}
21 %</driver>
22 %<sty>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
23 %<sty>\ProvidesPackage{mfpic4ode}
24 %<*sty>
25 [2009/04/15 v0.3 mfpic4ode.dtx file]
26 %</sty>
27 %
28 %<*driver>
29 \documentclass{ltxdoc}
30 \EnableCrossrefs
31 \CodelineIndex
32 \RecordChanges
33 \begin{document}
34 \DocInput{mfpic4ode.dtx}
35 \PrintChanges
36 \PrintIndex
37 \end{document}
38 %</driver>
39 % \fi
40 %
41 % \CheckSum{185}
42 %
43 % \CharacterTable
44 % {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
45 % Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
46 % Digits \0\1\2\3\4\5\6\7\8\9
47 % Exclamation \! Double quote \" Hash (number) \#
48 % Dollar \$ Percent \% Ampersand \&
49 % Acute accent \' Left paren \( Right paren \)
50 % Asterisk \* Plus \+ Comma \,
51 % Minus \- Point \. Solidus \/
52 % Colon \: Semicolon \; Less than \<
53 % Equals \= Greater than \> Question mark \?
54 % Commercial at \@ Left bracket \[ Backslash \\
55 % Right bracket \] Circumflex \^ Underscore \_
56 % Grave accent \` Left brace \{ Vertical bar \|
57 % Right brace \} Tilde \~}
58 %
59 %
60 % \changes{v0.2}{2008/02/03}{First public version}
61 % \changes{v0.3}{2009/04/15}{Added connect environment to paths}
62 % \changes{v0.4}{2010/04/07}{Updated documentations}
63 %
64 % \GetFileInfo{mfpic4ode.dtx}
65 %
66 % \DoNotIndex{\newcommand,\newenvironment}
67 %
68 % \def\fileversion{0.4}
69 % \def\filedate{2010/04/07}
70 %
71 % \title{The \textsf{mfpic4ode} package\thanks{This document
72 % corresponds to \textsf{mfpic4ode}~\fileversion, dated
73 % \filedate.}} \author{Robert Marik \\ \texttt{[email protected]}
74 % \thanks{Supported by the grant FRV\v{S} 99/2008 (Fund for
75 % Developement of Czech Universities).}}
76 % \maketitle
77 %
78 % \section{Introduction}
79 %
80 % The package |mfpic4ode| is a set of macros for drawing phase
81 % portraits and integral curves of differential equations and
82 % autonomous systems using |mfpic| macros. These macros have been used
83 % by the author to prepare some pictures for classrooms and the
84 % results seem to be acceptable for this purpose, but always remember
85 % that due to the fixed points arithmetics in Metapost, the error in
86 % computations could be significant. Another excellent tool which can
87 % be used to draw trajectories is Sage\TeX{} which gives you full
88 % power of computer algebra system Sage in \LaTeX.
89 %
90 % \section{Usage}
91 % You can load the package in \LaTeX{} using standard
92 % |\usepackage{mfpic4ode}| command, or you can use the macros in
93 % plain\TeX{} and load by |\input mfpic4ode.tex| command.
94 %
95 % \subsection{First order differential equation}
96 %
97 % To draw phase portrait of first order ordinary differential equation
98 % $$
99 % y'=f(x,y)
100 % $$
101 % we define commands |\ODEarrow| for drawing element of direction
102 % field and |\trajectory|, |\trajectoryRK| and |\trajectoryRKF| for
103 % drawing integral curves using Euler, second order Runge-Kutta and
104 % fourth order Runge-Kutta methods, respectively. Some important
105 % parameters, such as the number of steps, the length of step or the
106 % function from the right-hand side of the equations are stored in
107 % MetaPost variables and to keep the package simple and short, these
108 % variables are accessible using |\mfsrc| command.
109 %
110 % \DescribeMacro{\ifcolorODEarrow} \DescribeMacro{\colorODEarrowtrue}
111 % \DescribeMacro{\colorODEarrowfalse} If the \TeX{} boolean variable
112 % |\ifcolorODEarrow| is true, then the arrows from direction field are
113 % blue if the solution is increasing and red if decreasing. If
114 % |\ifcolorODEarrow| is false, the mfpic color from |\drawcolor| and
115 % |\headcolor| macros is used. More precisely,
116 % \begin{itemize}
117 % \item if |\ifcolorODEarrow| is true and $f(x_0,y_0)>0$, then the
118 % arrow at the point $(x_0,y_0)$ is blue
119 % \item if |\ifcolorODEarrow| is true and $f(x_0,y_0)\leq 0$, then the
120 % arrow at the point $(x_0,y_0)$ is red
121 % \item if |\ifcolorODEarrow| is false, then color from |\drawcolor|
122 % is used to draw the body of an arrow and color |\headcolor| is
123 % used to draw the head.
124 % \end{itemize}
125 % Arrows are drawn using mfpic |\draw\arrow\lines{...}| command and
126 % hence the parameters for customizing shape and size of the head from
127 % mfpic are also available. The MetaPost variable |ODEarrowlength| is
128 % used to customize the length of each arrow. If the arrow is
129 % horizontal, then the length of the arrow in mfpic coordinates is
130 % equal to |ODEarrowlength/xscale|. (This fixes the case when
131 % different |xscale| and |yscale| are used. All arrows have the same
132 % length.) You can set this variable using |\mfsrc| command, you can
133 % write e.g.
134 % \begin{verbatim}
135 % \mfsrc{ODEarrowlength:=0.07;}
136 % \end{verbatim}
137 % in your document.
138 %
139 % To draw arrows in regular rectangular grid you should use the
140 % |\ODEarrow| macro in a double cycle such as
141 % \begin{verbatim}
142 % \mfsrc{for j=0 step 0.07 until 1.2: for i:=0 step 0.5 until 10:}
143 % \ODEarrow{i}{j}
144 % \mfsrc{endfor;endfor;}
145 % \end{verbatim}
146 % or using the |multido| package
147 % \begin{verbatim}
148 % \multido{\r=0.0+0.1}{15}{\multido{\R=0.0+0.5}{19}{\ODEarrow{\R}{\r}}}
149 % \end{verbatim}
150 %
151 % \DescribeMacro{\ODEdefineequation{f(x,y)}} The macro |\ODEdefineequation| is
152 % used to save the right hand side of the ODE, i. e. the function
153 % $f(x,y)$. You should write the expression in the MetaPost format,
154 % the independent variable is supposed to be $x$, the dependent
155 % variable is $y$.
156 %
157 % \DescribeMacro{\trajectory\{x0\}\{y0\}}
158 % \DescribeMacro{\trajectoryRK\{x0\}\{y0\}}
159 % \DescribeMacro{\trajectoryRKF\{x0\}\{y0\}} The macros |\trajectory|,
160 % |\trajectoryRK| and |\trajectoryRKF| are used for drawing integral
161 % curves with initial condition $y(x_0)=y_0$ using Euler, second order
162 % Runge-Kutta and fourth order Runge-Kutta methods, respectively.
163 % The length of each step is stored in MetaPost variable
164 % |ODEstepcount|, the length of each step is in the MetaPost variable
165 % |ODEstep|. You can set these variables using |\mfsrc| macro as
166 % follows
167 % \begin{verbatim}
168 % \mfsrc{ODEstep:=0.02; ODEstepcount:=500;}
169 % \end{verbatim}
170 % The integral curve is drawn from short linear parts using |\ODEline|
171 % command which expands to |\lines| command from |mfpic| package by
172 % default. These linear parts are connected in connect environment and
173 % this allows to use prefixes like |\dotted| or |\dashed| to the
174 % trajectories. A simple test is used to keep the arithmetics in
175 % reasonable bounds: if after the step the curve leaves the horizontal
176 % strip between |yneg| and |ypos| variables, then the evaluation is
177 % stopped (in fact, in this case we do not change the independent
178 % variable and we do the remaining steps with the same last point).
179 % Recall that |yneg| and |ypos| variables are set when you call
180 % |mfpicture| environment. If you call the environment as follows
181 % \begin{verbatim}
182 % \begin{mfpic}[5][3]{-0.1}{1.5}{-0.1}{0.5}
183 % ...........
184 % \end{mfpic}
185 % \end{verbatim}
186 % then no more than one short linear part of the integral curve is
187 % outside the horizontal strip between $y=-0.1$ and $y=0.5$.
188 %
189 % \DescribeMacro{\trajectories}\DescribeMacro{\ODEarrows} To draw more
190 % trajectories you can use |\trajectories| command. The command
191 % |\trajectories{x1,y1;x2,y2;x3,y3;....;xn,yn}| expands to $n$
192 % |\trajectoryRKF| commands with initial conditions $y(x_i)=y_i$ for
193 % $i=1..n$. In a similar way,
194 % |\ODEarrows{x1,y1;x2,y2;x3,y3;....;xn,yn}| expands into $n$
195 % |\ODEarrow| commands.
196 %
197 % \subsection{Two-dimensional autonomous systems}
198 % Trajectories for two-dimensional autonomous system
199 % \begin{eqnarray*}
200 % x'&=&f(x,y)\\y'&=&g(x,y)
201 % \end{eqnarray*}
202 % are drawn using a very simple method based on the direction field.
203 % This could be improved in the next release of the package, but till
204 % now the results obtained in this way are qualitatively correct and
205 % sufficiently accurate (remember that you cannot expect accurate
206 % approximation due to the limitation of arithmetics in MetaPost).
207 % Anyway, some users may prefer the fourth order Runge--Kutta method.
208 %
209 % The macros |\ASdefineequation| |\ASarrow|, |\AStrajectory|,
210 % |\AStrajectoryRKF|, |\ASarrows| and |\AStrajectories| are
211 % counterparts to their |\ODE....| versions. The last point of each
212 % trajectory is stored in the |x1| and |x2| MetaPost variables. Hence,
213 % you can say |\AStrajectory{2}{2}| to draw trajectory with initial
214 % conditions $x(0)=2$, $y(0)=2$ and then you can continue this
215 % trajectory using |\AStrajectory{x1}{y1}| command. The macro
216 % |\AStrajectory| uses |ODEstep| and |ODEstepcount| variables, the
217 % macro |\AStrajectoryRKF| uses |TIMEstep| and |TIMEend| variables do
218 % perform the steps in the numerical solution. The number of steps is
219 % in the latter case evaluated as absolute value of the quotient
220 % |TIMEend/TIMEstep|. You can use negative value for |TIMEstep| to
221 % continue the trajectory backwards.
222 %
223 % \section{Troubleshooting}
224 % \subsection{The catcode of @ is messed} We set the category of @ to
225 % 11 (letter) when we load the package and at the end of definitions
226 % for mfpic4ode we set the category to 12. This could be a source of
227 % rare problems, if you use different value in your document.
228 %
229 % \subsection{Metapost: Not implemented: (unknown numeric) \dots }
230 % You have to set |ODEstep|, |ODEstepcount|, |TIMEstep| and |TIMEend|
231 % other variables using |\mfsrc| command (depending on the type of the
232 % problem).
233 %
234 % \StopEventually{}
235 %
236 % \section{Implementation}
237 % \begin{macrocode}
238 %<*tex>
239 \catcode`\@=11
240
241 \newif\ifcolorODEarrow
242 %%%\colorODEarrowfalse
243 \colorODEarrowtrue
244
245 %%% The line from one point to another
246 \def\ODEline#1#2{\lines{#1,#2}}
247
248 %%% The variable ODErhs is used to store the function from the right
249 %%% hand side of ODE in the form y'=f(x,y). We use command
250 %%% ODEdefineequation to set up this variable.
251 \def\ODEdefineequation#1{\fdef{ODErhs}{x,y}{#1}}
252
253 %%% Integral curve using Euler method. The step of this method is
254 %%% ODEstep and the number of steps is ODEstepcount. The points are
255 %%% stored in metapost variables x1,y1.
256 \def\trajectory#1#2{
257 \begin{connect}
258 \mfsrc{x1:=#1;y1:=#2;
259 for i=1 upto ODEstepcount:
260 x2:=x1+ODEstep;
261 y2:=y1+ODEstep*ODErhs(x1,y1);}
262 \ODEline{z1}{z2}
263 \mfsrc{
264 if ((y2>yneg) and (y2<ypos)): x1:=x2; y1:=y2 fi;
265 endfor
266 }
267 \end{connect}
268 }
269
270 %%% Integral curve using Runge--Kutta method.
271 \def\trajectoryRK#1#2{
272 \begin{connect}
273 \mfsrc{x1:=#1;y1:=#2;
274 for i=1 upto ODEstepcount:
275 k1:=ODErhs(x1,y1);
276 x3:=x1+(ODEstep/2);
277 y3:=y1+k1*(ODEstep/2);
278 k2:=ODErhs(x3,y3);
279 x2:=x1+ODEstep;
280 y2:=y1+ODEstep*k2;}
281 \ODEline{z1}{z2}
282 \mfsrc{
283 if ((y2>yneg) and (y2<ypos)): x1:=x2; y1:=y2 fi;
284 endfor
285 }
286 \end{connect}
287 }
288 %%% Integral curve using fourth order Runge--Kutta method.
289 \def\trajectoryRKF#1#2{
290 \begin{connect}
291 \mfsrc{x1:=#1;y1:=#2;
292 for i=1 upto ODEstepcount:
293 k1:=ODErhs(x1,y1);
294 x3:=x1+(ODEstep/2);
295 y3:=y1+k1*(ODEstep/2);
296 k2:=ODErhs(x3,y3);
297 y4:=y1+k2*(ODEstep/2);
298 k3:=ODErhs(x3,y4);
299 y5:=y1+k3*(ODEstep/2);
300 k4:=ODErhs(x3,y5);
301 kk:=(k1+2*k2+2*k3+k4)/6;
302 x2:=x1+ODEstep;
303 y2:=y1+ODEstep*kk;}
304 \ODEline{z1}{z2}
305 \mfsrc{
306 if ((y2>yneg) and (y2<ypos)): x1:=x2; y1:=y2 fi;
307 endfor
308 }
309 \end{connect}
310 }
311 \def\ODEarrow#1#2{
312 \mfsrc{x1:=#1; y1:=#2;
313 x3:=x1+(ODEarrowlength)/((xscale)++(ODErhs(#1,#2)*yscale));
314 y3:=y1+(ODEarrowlength*ODErhs(#1,#2))/((xscale)++(ODErhs(#1,#2)*yscale));
315 if y3>y1:ODEcolorarrow:=blue else: ODEcolorarrow:=red fi;
316 }
317 \ifcolorODEarrow
318 \drawcolor{ODEcolorarrow} \headcolor{ODEcolorarrow}
319 \fi
320 \draw\arrow\lines{z1,z3}
321 }
322
323 \def\ODEarrows#1{\ODE@cycle@points#1;,;}
324 \def\trajectories#1{\ODE@cycle@IC#1;,;}
325 \def\ODE@last@point{}
326 \def\ODE@cycle@points#1,#2;{\def\temp{#1}\ifx\temp\ODE@last@point\let\next\relax
327 \else\ODEarrow{#1}{#2}\relax\let\next\ODE@cycle@points\fi\next}
328 \def\ODE@cycle@IC#1,#2;{\def\temp{#1}\ifx\temp\ODE@last@point\let\next\relax
329 \else
330 \trajectoryRKF{#1}{#2}\relax\let\next\ODE@cycle@IC\fi\next}
331 \mfsrc{path p,q;color ODEcolorarrow;}
332
333 %%% One-dimensional autonomous systems y'=f(y) where '=d/dx
334 \def\ODEharrow#1{
335 \mfsrc{x1:=#1;
336 if ODErhs(0,x1)>0: x3:=x1+ODEarrowlength else: x3:=x1-ODEarrowlength fi;
337 if ODErhs(0,x1)*ODErhs(0,x3)<0: x1:=-100;x3:=-100 fi;
338 if x3>x1:ODEcolorarrow:=blue else: ODEcolorarrow:=red fi;
339 }
340 \ifcolorODEarrow \drawcolor{ODEcolorarrow}
341 \headcolor{ODEcolorarrow} \fi
342 \pen{1.5pt}
343 \draw\arrow\lines{(x1,0),(x3,0)}
344 }
345
346 \def\ODEvarrow#1{
347 \mfsrc{x1:=#1;
348 if ODErhs(0,#1)>0:
349 x3:=x1+(ODEarrowlength/yscale) else: x3:=x1-(ODEarrowlength/yscale) fi;
350 if ODErhs(0,x1)*ODErhs(0,x3)<0: x1:=-100;x3:=-100 fi;
351 if x3>x1:ODEcolorarrow:=blue else: ODEcolorarrow:=red fi;
352 }
353 \ifcolorODEarrow \drawcolor{ODEcolorarrow}
354 \headcolor{ODEcolorarrow} \fi
355 \pen{1.5pt}
356 \draw\arrow\lines{(0,x1),(0,x3)}
357 }
358
359 %%% Two-dimensional autonomous systems x'=f(x,y), y'=g(x,y) where '=d/dt
360 \def\ASdefineequations#1#2{\fdef{ASf}{x,y}{#1}\fdef{ASg}{x,y}{#2}}
361
362 \def\AStrajectory#1#2{
363 \begin{connect}
364 \mfsrc{x1:=#1;y1:=#2;
365 for i=1 upto ODEstepcount:
366 x2:=x1+ODEstep*ASf(x1,y1);
367 y2:=y1+ODEstep*ASg(x1,y1);}
368 \ODEline{z1}{z2}
369 \mfsrc{
370 if ((y2>yneg) and (y2<ypos)): x1:=x2; y1:=y2 fi;
371 endfor
372 }
373 \end{connect}
374 }
375 \def\ASarrow#1#2{
376 \mfsrc{x1:=#1; y1:=#2;
377 x3:=x1+(ODEarrowlength*ASf(#1,#2))/((ASf(#1,#2)*xscale)++(ASg(#1,#2)*yscale ));
378 y3:=y1+(ODEarrowlength*ASg(#1,#2))/((ASf(#1,#2)*xscale)++(ASg(#1,#2)*yscale ));
379 if y3>y1:ODEcolorarrow:=blue else: ODEcolorarrow:=red fi;
380 }
381 \ifcolorODEarrow
382 \drawcolor{ODEcolorarrow} \headcolor{ODEcolorarrow}
383 \fi
384 \draw\arrow\lines{z1,z3}
385 }
386
387 \def\ASarrows#1{\AS@cycle@points#1;,;}
388 \def\AS@cycle@points#1,#2;{\def\temp{#1}\ifx\temp\ODE@last@point\let\next\relax
389 \else\ASarrow{#1}{#2}\relax\let\next\AS@cycle@points\fi\next}
390 \def\AStrajectories#1{\AS@cycle@IC#1;,;}
391 \def\AS@cycle@IC#1,#2;{\def\temp{#1}\ifx\temp\ODE@last@point\let\next\relax
392 \else
393 \AStrajectoryRKF{#1}{#2}\relax\let\next\AS@cycle@IC\fi\next}
394 \def\AStrajectoryRKF#1#2{
395 \begin{connect}
396 \mfsrc{x1:=#1;y1:=#2;
397 TIMEsteps:=abs(TIMEend/TIMEstep);
398 TIME:=0;
399 for i=1 upto TIMEsteps:
400 k1:=ASf(x1,y1);
401 l1:=ASg(x1,y1);
402 k2:=ASf(x1+(TIMEstep*k1/2),y1+(TIMEstep*l1/2));
403 l2:=ASg(x1+(TIMEstep*k1/2),y1+(TIMEstep*l1/2));
404 k3:=ASf(x1+(TIMEstep*k2/2),y1+(TIMEstep*l2/2));
405 l3:=ASg(x1+(TIMEstep*k2/2),y1+(TIMEstep*l2/2));
406 k4:=ASf(x1+(TIMEstep*k3),y1+(TIMEstep*l3));
407 l4:=ASg(x1+(TIMEstep*k3),y1+(TIMEstep*l3));
408 k5:=((k1)/6)+((k2)/3)+((k3)/3)+((k4)/6);
409 l5:=(l1/6)+(l2/3)+(l3/3)+(l4/6);
410 x2:=x1+(TIMEstep*k5);
411 y2:=y1+(TIMEstep*l5);}
412 \ODEline{z1}{z2}
413 \mfsrc{
414 if ((y2>yneg) and (y2<ypos) and (x2<xpos) and (x2>xneg)): x1:=x2; y1:=y2 fi;
415 endfor
416 }
417 \end{connect}
418 }
419
420 \catcode`\@12\relax
421 %</tex>
422 %<sty>\input mfpic4ode.tex\relax
423 % \end{macrocode}
424 %
425 % \Finale
426 \endinput
427
428
429