summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/mfpic4ode/mfpic4ode.tex
blob: 6fe2928ca67d504b99cf626b52f4c971696bade3 (plain)
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
%%
%% This is file `mfpic4ode.tex',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% mfpic4ode.dtx  (with options: `tex')
%% 
%% This is a generated file.
%% 
%% Copyright (C) 2007 by Robert Marik <marik@mendelu.cz>
%% 
%% This file may be distributed and/or modified under the conditions of
%% the LaTeX Project Public License, either version 1.2 of this license
%% or (at your option) any later version.  The latest version of this
%% license is in:
%% 
%%    http://www.latex-project.org/lppl.txt
%% 
%% and version 1.2 or later is part of all distributions of LaTeX version
%% 1999/12/01 or later.
%% 
\catcode`\@=11

\newif\ifcolorODEarrow
%%%\colorODEarrowfalse
\colorODEarrowtrue

%%% The line from one point to another
\def\ODEline#1#2{\lines{#1,#2}}

%%% The variable ODErhs is used to store the function from the right
%%% hand side of ODE in the form y'=f(x,y). We use command
%%% ODEdefineequation to set up this variable.
\def\ODEdefineequation#1{\fdef{ODErhs}{x,y}{#1}}

%%% Integral curve using Euler method. The step of this method is
%%% ODEstep and the number of steps is ODEstepcount. The points are
%%% stored in metapost variables x1,y1.
\def\trajectory#1#2{
  \begin{connect}
    \mfsrc{x1:=#1;y1:=#2;
      for i=1 upto ODEstepcount:
      x2:=x1+ODEstep;
      y2:=y1+ODEstep*ODErhs(x1,y1);}
    \ODEline{z1}{z2}
    \mfsrc{
      if ((y2>yneg) and (y2<ypos)): x1:=x2; y1:=y2 fi;
      endfor
    }
  \end{connect}
}

%%% Integral curve using Runge--Kutta method.
\def\trajectoryRK#1#2{
  \begin{connect}
    \mfsrc{x1:=#1;y1:=#2;
      for i=1 upto ODEstepcount:
      k1:=ODErhs(x1,y1);
      x3:=x1+(ODEstep/2);
      y3:=y1+k1*(ODEstep/2);
      k2:=ODErhs(x3,y3);
      x2:=x1+ODEstep;
      y2:=y1+ODEstep*k2;}
    \ODEline{z1}{z2}
    \mfsrc{
      if ((y2>yneg) and (y2<ypos)): x1:=x2; y1:=y2 fi;
      endfor
    }
  \end{connect}
}
%%% Integral curve using fourth order Runge--Kutta method.
\def\trajectoryRKF#1#2{
  \begin{connect}
    \mfsrc{x1:=#1;y1:=#2;
      for i=1 upto ODEstepcount:
      k1:=ODErhs(x1,y1);
      x3:=x1+(ODEstep/2);
      y3:=y1+k1*(ODEstep/2);
      k2:=ODErhs(x3,y3);
      y4:=y1+k2*(ODEstep/2);
      k3:=ODErhs(x3,y4);
      y5:=y1+k3*(ODEstep/2);
      k4:=ODErhs(x3,y5);
      kk:=(k1+2*k2+2*k3+k4)/6;
      x2:=x1+ODEstep;
      y2:=y1+ODEstep*kk;}
    \ODEline{z1}{z2}
    \mfsrc{
      if ((y2>yneg) and (y2<ypos)): x1:=x2; y1:=y2 fi;
      endfor
    }
  \end{connect}
}
\def\ODEarrow#1#2{
  \mfsrc{x1:=#1; y1:=#2;
    x3:=x1+(ODEarrowlength)/((xscale)++(ODErhs(#1,#2)*yscale));
    y3:=y1+(ODEarrowlength*ODErhs(#1,#2))/((xscale)++(ODErhs(#1,#2)*yscale));
    if y3>y1:ODEcolorarrow:=blue else: ODEcolorarrow:=red fi;
  }
  \ifcolorODEarrow
    \drawcolor{ODEcolorarrow} \headcolor{ODEcolorarrow}
  \fi
  \draw\arrow\lines{z1,z3}
}

\def\ODEarrows#1{\ODE@cycle@points#1;,;}
\def\trajectories#1{\ODE@cycle@IC#1;,;}
\def\ODE@last@point{}
\def\ODE@cycle@points#1,#2;{\def\temp{#1}\ifx\temp\ODE@last@point\let\next\relax
  \else\ODEarrow{#1}{#2}\relax\let\next\ODE@cycle@points\fi\next}
\def\ODE@cycle@IC#1,#2;{\def\temp{#1}\ifx\temp\ODE@last@point\let\next\relax
  \else
  \trajectoryRKF{#1}{#2}\relax\let\next\ODE@cycle@IC\fi\next}
\mfsrc{path p,q;color ODEcolorarrow;}

%%% One-dimensional autonomous systems y'=f(y) where  '=d/dx
\def\ODEharrow#1{
  \mfsrc{x1:=#1;
    if ODErhs(0,x1)>0: x3:=x1+ODEarrowlength else: x3:=x1-ODEarrowlength fi;
    if ODErhs(0,x1)*ODErhs(0,x3)<0: x1:=-100;x3:=-100 fi;
    if x3>x1:ODEcolorarrow:=blue else: ODEcolorarrow:=red fi;
  }
  \ifcolorODEarrow \drawcolor{ODEcolorarrow}
  \headcolor{ODEcolorarrow} \fi
  \pen{1.5pt}
  \draw\arrow\lines{(x1,0),(x3,0)}
}

\def\ODEvarrow#1{
  \mfsrc{x1:=#1;
    if ODErhs(0,#1)>0:
    x3:=x1+(ODEarrowlength/yscale) else: x3:=x1-(ODEarrowlength/yscale) fi;
    if ODErhs(0,x1)*ODErhs(0,x3)<0: x1:=-100;x3:=-100 fi;
    if x3>x1:ODEcolorarrow:=blue else: ODEcolorarrow:=red fi;
  }
  \ifcolorODEarrow \drawcolor{ODEcolorarrow}
  \headcolor{ODEcolorarrow} \fi
  \pen{1.5pt}
  \draw\arrow\lines{(0,x1),(0,x3)}
}

%%% Two-dimensional autonomous systems  x'=f(x,y), y'=g(x,y) where '=d/dt
\def\ASdefineequations#1#2{\fdef{ASf}{x,y}{#1}\fdef{ASg}{x,y}{#2}}

\def\AStrajectory#1#2{
  \begin{connect}
    \mfsrc{x1:=#1;y1:=#2;
      for i=1 upto ODEstepcount:
      x2:=x1+ODEstep*ASf(x1,y1);
      y2:=y1+ODEstep*ASg(x1,y1);}
    \ODEline{z1}{z2}
    \mfsrc{
      if ((y2>yneg) and (y2<ypos)): x1:=x2; y1:=y2 fi;
      endfor
    }
  \end{connect}
}
\def\ASarrow#1#2{
  \mfsrc{x1:=#1; y1:=#2;
    x3:=x1+(ODEarrowlength*ASf(#1,#2))/((ASf(#1,#2)*xscale)++(ASg(#1,#2)*yscale    ));
    y3:=y1+(ODEarrowlength*ASg(#1,#2))/((ASf(#1,#2)*xscale)++(ASg(#1,#2)*yscale    ));
    if y3>y1:ODEcolorarrow:=blue else: ODEcolorarrow:=red fi;
  }
  \ifcolorODEarrow
  \drawcolor{ODEcolorarrow} \headcolor{ODEcolorarrow}
  \fi
  \draw\arrow\lines{z1,z3}
}

\def\ASarrows#1{\AS@cycle@points#1;,;}
\def\AS@cycle@points#1,#2;{\def\temp{#1}\ifx\temp\ODE@last@point\let\next\relax
  \else\ASarrow{#1}{#2}\relax\let\next\AS@cycle@points\fi\next}
\def\AStrajectories#1{\AS@cycle@IC#1;,;}
\def\AS@cycle@IC#1,#2;{\def\temp{#1}\ifx\temp\ODE@last@point\let\next\relax
  \else
  \AStrajectoryRKF{#1}{#2}\relax\let\next\AS@cycle@IC\fi\next}
\def\AStrajectoryRKF#1#2{
  \begin{connect}
    \mfsrc{x1:=#1;y1:=#2;
      TIMEsteps:=abs(TIMEend/TIMEstep);
      TIME:=0;
      for i=1 upto TIMEsteps:
      k1:=ASf(x1,y1);
      l1:=ASg(x1,y1);
      k2:=ASf(x1+(TIMEstep*k1/2),y1+(TIMEstep*l1/2));
      l2:=ASg(x1+(TIMEstep*k1/2),y1+(TIMEstep*l1/2));
      k3:=ASf(x1+(TIMEstep*k2/2),y1+(TIMEstep*l2/2));
      l3:=ASg(x1+(TIMEstep*k2/2),y1+(TIMEstep*l2/2));
      k4:=ASf(x1+(TIMEstep*k3),y1+(TIMEstep*l3));
      l4:=ASg(x1+(TIMEstep*k3),y1+(TIMEstep*l3));
      k5:=((k1)/6)+((k2)/3)+((k3)/3)+((k4)/6);
      l5:=(l1/6)+(l2/3)+(l3/3)+(l4/6);
      x2:=x1+(TIMEstep*k5);
      y2:=y1+(TIMEstep*l5);}
    \ODEline{z1}{z2}
    \mfsrc{
      if ((y2>yneg) and (y2<ypos) and (x2<xpos) and (x2>xneg)): x1:=x2; y1:=y2 fi;
      endfor
    }
  \end{connect}
}

\catcode`\@12\relax
\endinput
%%
%% End of file `mfpic4ode.tex'.