Debian Codegraph @

texmf-dist/doc/latex/mfpic4ode/demo/demo-plain.tex

raw
1 % compile with pdftex demo-plain; mpost portret; pdftex demo-plain
2 \input multido
3 \input mfpic
4
5 \usemetapost
6 \opengraphsfile{portret}
7 \tlabelsep{3pt}
8 \usemplabels
9
10
11 \def\frac#1#2{{#1 \over #2}}
12 \mfpicunit=1cm
13
14 \input mfpic4ode
15
16
17
18
19
20 \centerline{\bf Test file for mfpic4ode package}
21 \centerline{Robert Ma\v r\'\i k}
22 \centerline{January 3, 2008}
23
24 \bigskip
25
26 See the source file {\tt demo-plain.tex} for comments in the \TeX{} code.
27
28 \clipmfpic
29 \bigskip
30 \centerline{\bf Logistic equation}
31 Here we draw a simple picture which describes stability of stationary
32 points of teh equation and then draw phase portrait of the equation.
33
34 $$ x'=
35 {{r}\cdot\left(1-{x\over K}\right)x}
36 $$
37
38 % We set parameters for logistic equation
39 \mfsrc{r:=1;K:=0.98;}
40
41 % We set parameters for drawing and for the ODE solver
42 \mfsrc{ODEarrowlength:=0.07; ODEstep:=0.02; ODEstepcount:=500;}
43
44 % We define the equation
45 \ODEdefineequation{r*y*(1-(y/K))}
46
47 Stability and sign of the right--hand side.
48
49 \mfpic[5][3]{-0.1}{1.5}{-0.1}{0.5}
50 \axes
51 \xmarks{K}
52 \tlabel[tc](K,0){$K$}
53 \tlabel[bc](0,ypos){$\smash{f(x)}$}
54 \tlabel[cl](xpos,0){$x$}
55
56 % This code draws arrows on x axis, the arrow points to the right if
57 % $f(x)$ is positive and to the left if f(x) is negative. If the
58 % starting point of the arrow is x_0 and the final point x_1 and if
59 % the function changes sign between x_0 and x_1, the arrow from x_0
60 % is not drawn
61 \multido{\r=0.01+0.2}{8}{\ODEharrow{\r}}
62
63 \pen{1pt}
64 \draw[rgb(0,0.5,0)]\parafcn{0,1.5,0.1}{(t,ODErhs(1,t))}
65 \draw[red]\parafcn{0,1.5,0.1}{(t,ODErhs(1,t))}
66 \draw[blue]\parafcn{0,K,0.1}{(t,ODErhs(1,t))}
67 \endmfpic
68
69
70 \mfsrc{ODEarrowlength:=0.3;}
71
72
73 Phase portrait
74
75 \mfpic[1][4]{-0.1}{10}{-0.1}{1.5}
76 \axes
77 \ymarks{K}
78 \tlabel[cr](0,K){$K$}
79 \penwd{1pt}
80 \tlabel[tc](xpos,0){$t$}
81 \tlabel[bc](0,ypos){$x$}
82 \pen{0.7pt}
83
84 % This code draws arrows in 19x15 points and draws three integral
85 % curves
86
87 \multido{\r=0.0+0.1}{15}
88 {\multido{\R=0.0+0.5}{19}
89 {\ODEarrow{\R}{\r}}}
90 \trajectories{0,0.3;0,0.01;0,1.4}
91
92 \endmfpic
93
94 \bigskip
95 \centerline{\bf Logistic equation with harvesting}
96
97 Similar to the previous picture, but both pictures are drawn together
98 to see the relations between them.
99
100 $$ x'=
101 {{r}\cdot\left(1-\frac x{{K}}\right)x}-{p}
102 $$
103
104 % We set parameters for logistic equation with harvesting and define
105 % this equation.
106 \mfsrc{r:=1;K:=0.98;lov:=0.15;}
107 \ODEdefineequation{r*y*(1-(y/K))-lov}
108
109 % If the equation possesses stationary points, we store them into
110 % variables meza and mezb. If not, we set these variables to negative
111 % values (and the are not drawn in view of mfpicclip option.
112 \mfsrc{if (r**2*(K**2)-4*r*lov*K)<0: meza:=-1;mezb:=-1.1
113 else: meza:=(r*K-sqrt(r**2*(K**2)-4*r*lov*K))/(2*r);
114 mezb:=(r*K+sqrt(r**2*(K**2)-4*r*lov*K))/(2*r)
115 fi;}
116
117
118 \hbox to \hsize{\hss\mfpic[1][4]{-0.1}{10}{-0.1}{1.3}
119 \axes
120 \ymarks{K}
121 \tlabel[cr](0,K){$K$}
122 \penwd{1pt}
123 \tlabel[tc](xpos,0){$t$}
124 \tlabel[bc](0,ypos){$x$}
125
126 % here we draw lines - stationary solutions stored in meza and mezb
127 % variables
128 \draw[gray(0.5)]\dashed\lines{(0,meza),(xpos,meza)}
129 \draw[gray(0.5)]\dashed\lines{(0,mezb),(xpos,mezb)}
130
131 % We draw direction field using metapost cycle. Another option is to
132 % use multido command as in the previous example.
133 \mfsrc{for j=0 step 0.07 until 1.2:
134 for i:=0 step 0.5 until 10:}
135 \ODEarrow{i}{j}
136 \mfsrc{endfor;endfor;}
137
138 % We draw trajectories using black color.
139 \drawcolor{black}
140 \trajectories{0,0.1;0,0.2;0,0.4;0,0.6;0,0.8;0,1.1}
141 \endmfpic\qquad
142 % On the right we draw the right hand side of the equation
143 \mfpic[3][4]{-0.15}{0.6}{-0.1}{1.3}
144 \axes
145 \ymarks{K}
146 \tlabel[cr](0,K){$K$}
147 \tlabel[br](xpos,0){$f(x)$}
148 \tlabel[bc](0,ypos){$x$}
149 \pen{1pt}
150
151 % This code draws the graph of right-hand side of logistic equation
152 % without harvesting.
153 \drawcolor{gray(0.7)}\draw\parafcn{0,1.2,0.1}{(ODErhs(1,t)+lov,t)}
154
155 % This code draws the graph of right-hand side. We use the blue
156 % color for positivce and red color for negative parts. We draw also
157 % arrows which are determined by the sigh of the right-hand side of
158 % the equation.
159 \drawcolor{red}\draw\parafcn{0,1.2,0.1}{(ODErhs(1,t),t)}
160 \drawcolor{blue}\parafcn{meza,mezb,0.05}{(ODErhs(1,t),t)}
161 \multido{\r=0.1+0.15}{7}{\ODEvarrow{\r}}
162 \endmfpic\hss}
163
164 \bigskip
165 \centerline{\bf Three numerical methods for ODEs}
166
167 Here we draw solution of ODE using all three available numerical
168 methods. We use big step to see the difference between Euler,
169 Runge--Kutta and fourth order Runge--Kutta method.
170 $$ y'=x+y^3\qquad y(0)=1
171 $$
172 \leavevmode
173 \mfpic[20][5]{0}{0.5}{0.9}{2.4}
174 % We set-up parameters
175 \mfsrc{ODEarrowlength:=0.5;}
176 \ODEdefineequation{x+(y**3)}
177 \pen{1pt}\tlabelsep{1pt}
178
179 % We set up parameters for small step
180 \nomplabels
181 \drawcolor{green}
182 \mfsrc{ODEstep:=0.02; ODEstepcount:=30;}
183 \trajectoryRKF{0}{1}
184 \tlabel[tr](0.39,2.4){\bf{Exact solution}}
185
186 % We use bigger step to see the difference between various
187 % methods.
188 \mfsrc{ODEstep:=0.2; ODEstepcount:=2;}
189
190 % We draw trajectory by Euler method
191 \drawcolor{black}
192 \trajectory{0}{1}
193 \tlabel[bl](0.4,1.6){\bf{Euler}}
194 \tlabel[tl](0,1){\bf{$k_1$}}
195 \tlabel[tl](0.2,1.2){\bf{$k_1$ for second step}}
196
197 % We draw trajectory by Runge-Kutta method
198 \drawcolor{rgb(0.5,0.5,0.5)}
199 \trajectoryRK{0}{1}
200 \tlabel[cl](0.4,2.05){\bf{RK}}
201 \tlabel[tl](0.1,1.1){\bf{$k_2$}}
202
203 % We draw trajectory by fourth order Runge-Kutta method
204 \drawcolor{rgb(1,0,0)}
205 \trajectoryRKF{0}{1}
206 \tlabel[tl](0.4,2.15){\bf{RK4}}
207
208 \tlabelsep{3pt}
209
210 % We draw direction field using blue arrows and metapost cycle
211 \penwd{1pt}
212 \drawcolor{blue}\headcolor{blue}
213 \mfsrc{for j=0.9 step 0.1 until 2.3:
214 for i:=0 step 0.05 until 0.5:}
215 \ODEarrow{i}{j}
216 \mfsrc{endfor;endfor;}
217
218
219 \drawcolor{black}
220 \doaxes{lbrt}
221 \bmarks{0,0.2,0.4}
222 \tmarks{0,0.2,0.4}
223 \lmarks{1,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,2,2.2}
224 \pointcolor{red}
225 \pointfilltrue
226 \point[4pt]{(0,1)}
227
228 % We draw some other and bigger arrows in the direction field. The
229 % slopes of hese arrows are important for the first step by Euler
230 % and Ruge-Kutta method and the second step by Euler method.
231 \pen{2pt}
232 \mfsrc{ODEarrowlength:=1;}
233 \colorODEarrowfalse
234 \drawcolor{red}\headcolor{red}
235 \ODEarrows{0,1;0.1,1.1}
236 \ODEarrow{0.2}{1.2}
237
238 \axislabels{b}[tc]{{$0$}0,{$0.2$}0.2,{$0.4$}0.4}
239 \axislabels{l}[cr]{{$0.8$}0.8,{$1$}1,{$1.2$}1.2,{$1.4$}1.4,{$1.6$}1.6,{$1.8$}1.8,{$2$}2,{$2.2$}2.2,{$2.4$}2.4}
240 \endmfpic
241
242
243 \bigskip
244 \centerline{\bf Autonomous systems}
245
246 % The color arrows have no sense in the phase portrait of autonomous
247 % system.
248 \colorODEarrowfalse
249
250 We draw the phase portrait of autonomous system, nulclines, invariant
251 set between nulclines, trajectories. We draw arrows in regular grid
252 and add few more arrows on nulclines and outside the regular grid.
253
254 \mfsrc{TIMEstep:=0.05; TIMEend:=30;}
255
256 \mfpic
257 [0.5]{-2}{15}{-2}{15}
258 \nomplabels
259 \tlabel[cc](8,15.5){\bf Competing species}
260 \usemplabels
261
262 % We set up parameters, define equations, define functions which
263 % describe nulclines and store stationary points into variables z10,
264 % z11, z12, z1.
265
266 \mfsrc{a:=11;b:=1;c:=0.8;k:=10;l:=1.1;m:=1.2;}
267 \mfsrc{ODEarrowlength:=0.3;}
268 \ASdefineequations{x*(a-b*x-c*y)}{y*(k-l*x-m*y)}
269 \fdef{xnulklina}{x}{(a-b*x)/c}
270 \fdef{ynulklina}{x}{(k-l*x)/m}
271 \mfsrc{z10=(0,a/c);z11=(0,k/m);z12=(a/b,0);z13=(k/l,0);}
272
273 % Here we draw a gray polygon - invariant set fot the system.
274 \pen{0.3pt}
275 \gfill[gray(0.7)]\lclosed\lines{z10,z11,z13,z12}
276 \axes
277 \tlabel[bc](0,ypos){$y$}
278 \tlabel[cl](xpos,0){$x$}
279
280 \pointsize=3pt
281
282 \pointfilltrue\pointcolor{red}\point{(a/b,0)}
283 \draw[red]\function{0,a/b,1}{xnulklina(x)}
284 \draw[red]\lines{(0,0),(0,ypos)}
285 \tlabel[cr](x10,y10){$a\over c$}
286 \tlabel[tc](x12,y12){$a\over b$}
287
288 \pointfilltrue\pointcolor{green}\point{(0,k/m)}
289 \draw[green]\function{0,k/l,1}{ynulklina(x)}
290 \draw[green]\lines{(0,0),(xpos,0)}
291 \tlabel[cr](x11,y11){$\alpha\over \beta$}
292 \tlabel[tc](x13,y13){$\alpha\over \gamma$}
293
294
295 \penwd{1.5pt}
296 \drawcolor{gray(0.25)}\headcolor{gray(0.25)}
297 \ASarrows{0,6;0,11;7,0;9,0;4.5,0;13,0;2,0;0,14;0,2}
298 \ASarrows{3,3;8,7;13,2;3,14}
299 \ASarrows{4,ynulklina(4);5,ynulklina(5);6,ynulklina(6);
300 1.7,ynulklina(1.7)}
301 \ASarrows{0.5,xnulklina(0.5);1.8,xnulklina(1.8);6,xnulklina(6);4,xnulklina(4);
302 7.5,xnulklina(7.5)}
303
304 \drawcolor{black}
305 \AStrajectories{12,12;8,12;4,12;0.2,3;1,3;6,12;1,1;1,0.12;0.3,14}
306 \mfsrc{TIMEstep:=-0.05; TIMEend:=5;}
307 \AStrajectories{12,12;8,12;4,12;0.2,3;1,3;6,12;1,1;1,0.12;0.3,14}
308 \penwd{0.5pt}
309 \drawcolor{gray(0.5)}
310 \headcolor{gray(0.5)}
311 \multido{\r=0.5+1}{15}{\multido{\R=0.5+1}{15}{\ASarrow{\R}{\r}}}
312
313
314 \endmfpic
315
316
317 \break
318 \bigskip
319 \centerline{\bf Predator prey system with HollingII response function}
320
321
322
323
324 \mfpic[2]{-0.1}{4}{-0.1}{3}
325 % we define functions and parameters, right hand sides of the system
326 % and a function which defines nulcline
327 \mfsrc{r:=1;K:=3;a:=1;k:=0.8;P:=1;Alfa:=0.42;}
328 \fdef{funkceV}{x}{a*x/(x+P)}
329 \ASdefineequations{r*x*(1-(x/K))-funkceV(x)*y}{(-Alfa+k*funkceV(x))*y}
330 \fdef{xnulklina}{x}{r*(1-(x/K))*(x+P)/a}
331 \mfsrc{ODEarrowlength:=0.2;}
332
333 % Here we draw axes and nulclines
334 \axes
335 \tlabel[bc](0,ypos){$y$}
336 \tlabel[cl](xpos,0){$x$}
337 \draw[red]\function{0,K,0.1}{xnulklina(x)}
338 \draw[green]\lines{(P/((k*a/Alfa)-1),0),(P/((k*a/Alfa)-1),ypos)}
339
340 % Here we draw some arrows on nulclines and then arrows in the plane
341 \penwd{0.5pt}
342 \drawcolor{gray(0.25)}\headcolor{gray(0.25)}
343 \ASarrows{P/((k*a/Alfa)-1),1;P/((k*a/Alfa)-1),2;P/((k*a/Alfa)-1),0.5;P/((k*a/Alfa)-1),1.5}
344 \ASarrows{0,xnulklina(0);0.5,xnulklina(0.5);1,xnulklina(1);1.5,xnulklina(1.5);2,xnulklina(2);2.25,xnulklina(2.25)}
345 \multido{\r=0.1+0.25}{20}{\multido{\R=0.1+0.25}{20}{\ASarrow{\R}{\r}}}
346
347 %% We draw trajectory with IC x=2, y=2
348 \drawcolor{black}
349 \mfsrc{TIMEstep:=0.05; TIMEend:=10;}
350 \AStrajectoryRKF{2}{2}
351 %% We continue the trajectory (spiral) from the last point
352 \AStrajectoryRKF{x1}{y1}
353 \AStrajectoryRKF{x1}{y1}
354 \AStrajectoryRKF{x1}{y1}
355 \AStrajectoryRKF{x1}{y1}
356 \AStrajectoryRKF{x1}{y1}
357 \AStrajectoryRKF{x1}{y1}
358 \AStrajectoryRKF{x1}{y1}
359
360 %% We continue backwards
361 \mfsrc{TIMEstep:=-0.05;}
362 \AStrajectoryRKF{2}{2}
363
364 \endmfpic
365
366
367 \closegraphsfile
368 \end