| 1 |
%% |
| 2 |
%% This is file `mfpic4ode.tex', |
| 3 |
%% generated with the docstrip utility. |
| 4 |
%% |
| 5 |
%% The original source files were: |
| 6 |
%% |
| 7 |
%% mfpic4ode.dtx (with options: `tex') |
| 8 |
%% |
| 9 |
%% This is a generated file. |
| 10 |
%% |
| 11 |
%% Copyright (C) 2007 by Robert Marik <[email protected]> |
| 12 |
%% |
| 13 |
%% This file may be distributed and/or modified under the conditions of |
| 14 |
%% the LaTeX Project Public License, either version 1.2 of this license |
| 15 |
%% or (at your option) any later version. The latest version of this |
| 16 |
%% license is in: |
| 17 |
%% |
| 18 |
%% http://www.latex-project.org/lppl.txt |
| 19 |
%% |
| 20 |
%% and version 1.2 or later is part of all distributions of LaTeX version |
| 21 |
%% 1999/12/01 or later. |
| 22 |
%% |
| 23 |
\catcode`\@=11 |
| 24 |
|
| 25 |
\newif\ifcolorODEarrow |
| 26 |
%%%\colorODEarrowfalse |
| 27 |
\colorODEarrowtrue |
| 28 |
|
| 29 |
%%% The line from one point to another |
| 30 |
\def\ODEline#1#2{\lines{#1,#2}} |
| 31 |
|
| 32 |
%%% The variable ODErhs is used to store the function from the right |
| 33 |
%%% hand side of ODE in the form y'=f(x,y). We use command |
| 34 |
%%% ODEdefineequation to set up this variable. |
| 35 |
\def\ODEdefineequation#1{\fdef{ODErhs}{x,y}{#1}} |
| 36 |
|
| 37 |
%%% Integral curve using Euler method. The step of this method is |
| 38 |
%%% ODEstep and the number of steps is ODEstepcount. The points are |
| 39 |
%%% stored in metapost variables x1,y1. |
| 40 |
\def\trajectory#1#2{ |
| 41 |
\begin{connect} |
| 42 |
\mfsrc{x1:=#1;y1:=#2; |
| 43 |
for i=1 upto ODEstepcount: |
| 44 |
x2:=x1+ODEstep; |
| 45 |
y2:=y1+ODEstep*ODErhs(x1,y1);} |
| 46 |
\ODEline{z1}{z2} |
| 47 |
\mfsrc{ |
| 48 |
if ((y2>yneg) and (y2<ypos)): x1:=x2; y1:=y2 fi; |
| 49 |
endfor |
| 50 |
} |
| 51 |
\end{connect} |
| 52 |
} |
| 53 |
|
| 54 |
%%% Integral curve using Runge--Kutta method. |
| 55 |
\def\trajectoryRK#1#2{ |
| 56 |
\begin{connect} |
| 57 |
\mfsrc{x1:=#1;y1:=#2; |
| 58 |
for i=1 upto ODEstepcount: |
| 59 |
k1:=ODErhs(x1,y1); |
| 60 |
x3:=x1+(ODEstep/2); |
| 61 |
y3:=y1+k1*(ODEstep/2); |
| 62 |
k2:=ODErhs(x3,y3); |
| 63 |
x2:=x1+ODEstep; |
| 64 |
y2:=y1+ODEstep*k2;} |
| 65 |
\ODEline{z1}{z2} |
| 66 |
\mfsrc{ |
| 67 |
if ((y2>yneg) and (y2<ypos)): x1:=x2; y1:=y2 fi; |
| 68 |
endfor |
| 69 |
} |
| 70 |
\end{connect} |
| 71 |
} |
| 72 |
%%% Integral curve using fourth order Runge--Kutta method. |
| 73 |
\def\trajectoryRKF#1#2{ |
| 74 |
\begin{connect} |
| 75 |
\mfsrc{x1:=#1;y1:=#2; |
| 76 |
for i=1 upto ODEstepcount: |
| 77 |
k1:=ODErhs(x1,y1); |
| 78 |
x3:=x1+(ODEstep/2); |
| 79 |
y3:=y1+k1*(ODEstep/2); |
| 80 |
k2:=ODErhs(x3,y3); |
| 81 |
y4:=y1+k2*(ODEstep/2); |
| 82 |
k3:=ODErhs(x3,y4); |
| 83 |
y5:=y1+k3*(ODEstep/2); |
| 84 |
k4:=ODErhs(x3,y5); |
| 85 |
kk:=(k1+2*k2+2*k3+k4)/6; |
| 86 |
x2:=x1+ODEstep; |
| 87 |
y2:=y1+ODEstep*kk;} |
| 88 |
\ODEline{z1}{z2} |
| 89 |
\mfsrc{ |
| 90 |
if ((y2>yneg) and (y2<ypos)): x1:=x2; y1:=y2 fi; |
| 91 |
endfor |
| 92 |
} |
| 93 |
\end{connect} |
| 94 |
} |
| 95 |
\def\ODEarrow#1#2{ |
| 96 |
\mfsrc{x1:=#1; y1:=#2; |
| 97 |
x3:=x1+(ODEarrowlength)/((xscale)++(ODErhs(#1,#2)*yscale)); |
| 98 |
y3:=y1+(ODEarrowlength*ODErhs(#1,#2))/((xscale)++(ODErhs(#1,#2)*yscale)); |
| 99 |
if y3>y1:ODEcolorarrow:=blue else: ODEcolorarrow:=red fi; |
| 100 |
} |
| 101 |
\ifcolorODEarrow |
| 102 |
\drawcolor{ODEcolorarrow} \headcolor{ODEcolorarrow} |
| 103 |
\fi |
| 104 |
\draw\arrow\lines{z1,z3} |
| 105 |
} |
| 106 |
|
| 107 |
\def\ODEarrows#1{\ODE@cycle@points#1;,;} |
| 108 |
\def\trajectories#1{\ODE@cycle@IC#1;,;} |
| 109 |
\def\ODE@last@point{} |
| 110 |
\def\ODE@cycle@points#1,#2;{\def\temp{#1}\ifx\temp\ODE@last@point\let\next\relax |
| 111 |
\else\ODEarrow{#1}{#2}\relax\let\next\ODE@cycle@points\fi\next} |
| 112 |
\def\ODE@cycle@IC#1,#2;{\def\temp{#1}\ifx\temp\ODE@last@point\let\next\relax |
| 113 |
\else |
| 114 |
\trajectoryRKF{#1}{#2}\relax\let\next\ODE@cycle@IC\fi\next} |
| 115 |
\mfsrc{path p,q;color ODEcolorarrow;} |
| 116 |
|
| 117 |
%%% One-dimensional autonomous systems y'=f(y) where '=d/dx |
| 118 |
\def\ODEharrow#1{ |
| 119 |
\mfsrc{x1:=#1; |
| 120 |
if ODErhs(0,x1)>0: x3:=x1+ODEarrowlength else: x3:=x1-ODEarrowlength fi; |
| 121 |
if ODErhs(0,x1)*ODErhs(0,x3)<0: x1:=-100;x3:=-100 fi; |
| 122 |
if x3>x1:ODEcolorarrow:=blue else: ODEcolorarrow:=red fi; |
| 123 |
} |
| 124 |
\ifcolorODEarrow \drawcolor{ODEcolorarrow} |
| 125 |
\headcolor{ODEcolorarrow} \fi |
| 126 |
\pen{1.5pt} |
| 127 |
\draw\arrow\lines{(x1,0),(x3,0)} |
| 128 |
} |
| 129 |
|
| 130 |
\def\ODEvarrow#1{ |
| 131 |
\mfsrc{x1:=#1; |
| 132 |
if ODErhs(0,#1)>0: |
| 133 |
x3:=x1+(ODEarrowlength/yscale) else: x3:=x1-(ODEarrowlength/yscale) fi; |
| 134 |
if ODErhs(0,x1)*ODErhs(0,x3)<0: x1:=-100;x3:=-100 fi; |
| 135 |
if x3>x1:ODEcolorarrow:=blue else: ODEcolorarrow:=red fi; |
| 136 |
} |
| 137 |
\ifcolorODEarrow \drawcolor{ODEcolorarrow} |
| 138 |
\headcolor{ODEcolorarrow} \fi |
| 139 |
\pen{1.5pt} |
| 140 |
\draw\arrow\lines{(0,x1),(0,x3)} |
| 141 |
} |
| 142 |
|
| 143 |
%%% Two-dimensional autonomous systems x'=f(x,y), y'=g(x,y) where '=d/dt |
| 144 |
\def\ASdefineequations#1#2{\fdef{ASf}{x,y}{#1}\fdef{ASg}{x,y}{#2}} |
| 145 |
|
| 146 |
\def\AStrajectory#1#2{ |
| 147 |
\begin{connect} |
| 148 |
\mfsrc{x1:=#1;y1:=#2; |
| 149 |
for i=1 upto ODEstepcount: |
| 150 |
x2:=x1+ODEstep*ASf(x1,y1); |
| 151 |
y2:=y1+ODEstep*ASg(x1,y1);} |
| 152 |
\ODEline{z1}{z2} |
| 153 |
\mfsrc{ |
| 154 |
if ((y2>yneg) and (y2<ypos)): x1:=x2; y1:=y2 fi; |
| 155 |
endfor |
| 156 |
} |
| 157 |
\end{connect} |
| 158 |
} |
| 159 |
\def\ASarrow#1#2{ |
| 160 |
\mfsrc{x1:=#1; y1:=#2; |
| 161 |
x3:=x1+(ODEarrowlength*ASf(#1,#2))/((ASf(#1,#2)*xscale)++(ASg(#1,#2)*yscale )); |
| 162 |
y3:=y1+(ODEarrowlength*ASg(#1,#2))/((ASf(#1,#2)*xscale)++(ASg(#1,#2)*yscale )); |
| 163 |
if y3>y1:ODEcolorarrow:=blue else: ODEcolorarrow:=red fi; |
| 164 |
} |
| 165 |
\ifcolorODEarrow |
| 166 |
\drawcolor{ODEcolorarrow} \headcolor{ODEcolorarrow} |
| 167 |
\fi |
| 168 |
\draw\arrow\lines{z1,z3} |
| 169 |
} |
| 170 |
|
| 171 |
\def\ASarrows#1{\AS@cycle@points#1;,;} |
| 172 |
\def\AS@cycle@points#1,#2;{\def\temp{#1}\ifx\temp\ODE@last@point\let\next\relax |
| 173 |
\else\ASarrow{#1}{#2}\relax\let\next\AS@cycle@points\fi\next} |
| 174 |
\def\AStrajectories#1{\AS@cycle@IC#1;,;} |
| 175 |
\def\AS@cycle@IC#1,#2;{\def\temp{#1}\ifx\temp\ODE@last@point\let\next\relax |
| 176 |
\else |
| 177 |
\AStrajectoryRKF{#1}{#2}\relax\let\next\AS@cycle@IC\fi\next} |
| 178 |
\def\AStrajectoryRKF#1#2{ |
| 179 |
\begin{connect} |
| 180 |
\mfsrc{x1:=#1;y1:=#2; |
| 181 |
TIMEsteps:=abs(TIMEend/TIMEstep); |
| 182 |
TIME:=0; |
| 183 |
for i=1 upto TIMEsteps: |
| 184 |
k1:=ASf(x1,y1); |
| 185 |
l1:=ASg(x1,y1); |
| 186 |
k2:=ASf(x1+(TIMEstep*k1/2),y1+(TIMEstep*l1/2)); |
| 187 |
l2:=ASg(x1+(TIMEstep*k1/2),y1+(TIMEstep*l1/2)); |
| 188 |
k3:=ASf(x1+(TIMEstep*k2/2),y1+(TIMEstep*l2/2)); |
| 189 |
l3:=ASg(x1+(TIMEstep*k2/2),y1+(TIMEstep*l2/2)); |
| 190 |
k4:=ASf(x1+(TIMEstep*k3),y1+(TIMEstep*l3)); |
| 191 |
l4:=ASg(x1+(TIMEstep*k3),y1+(TIMEstep*l3)); |
| 192 |
k5:=((k1)/6)+((k2)/3)+((k3)/3)+((k4)/6); |
| 193 |
l5:=(l1/6)+(l2/3)+(l3/3)+(l4/6); |
| 194 |
x2:=x1+(TIMEstep*k5); |
| 195 |
y2:=y1+(TIMEstep*l5);} |
| 196 |
\ODEline{z1}{z2} |
| 197 |
\mfsrc{ |
| 198 |
if ((y2>yneg) and (y2<ypos) and (x2<xpos) and (x2>xneg)): x1:=x2; y1:=y2 fi; |
| 199 |
endfor |
| 200 |
} |
| 201 |
\end{connect} |
| 202 |
} |
| 203 |
|
| 204 |
\catcode`\@12\relax |
| 205 |
\endinput |
| 206 |
%% |
| 207 |
%% End of file `mfpic4ode.tex'. |