summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/mfpic4ode/demo/demo-plain.tex
blob: f8e758f3d6e491e9a293c29472c77947cb929717 (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
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
% compile with pdftex demo-plain; mpost portret; pdftex demo-plain
\input multido
\input mfpic

\usemetapost
\opengraphsfile{portret}
\tlabelsep{3pt}
\usemplabels


\def\frac#1#2{{#1 \over #2}}
\mfpicunit=1cm

\input mfpic4ode





\centerline{\bf Test file for mfpic4ode package}
\centerline{Robert Ma\v r\'\i k}
\centerline{January 3, 2008}

\bigskip

See the source file {\tt demo-plain.tex} for comments in the \TeX{} code.

\clipmfpic
\bigskip
\centerline{\bf Logistic equation}
Here we draw a simple picture which describes stability of stationary
points of teh equation and then draw phase portrait of the equation.

$$      x'=
      {{r}\cdot\left(1-{x\over K}\right)x}
$$

% We set parameters for logistic equation
\mfsrc{r:=1;K:=0.98;}

% We set parameters for drawing and for the ODE solver
\mfsrc{ODEarrowlength:=0.07; ODEstep:=0.02; ODEstepcount:=500;}

% We define the equation
\ODEdefineequation{r*y*(1-(y/K))}

Stability and sign of the right--hand side.

\mfpic[5][3]{-0.1}{1.5}{-0.1}{0.5}
  \axes
  \xmarks{K}
  \tlabel[tc](K,0){$K$}
  \tlabel[bc](0,ypos){$\smash{f(x)}$}
  \tlabel[cl](xpos,0){$x$}

  % This code draws arrows on x axis, the arrow points to the right if
  % $f(x)$ is positive and to the left if f(x) is negative. If the
  % starting point of the arrow is x_0 and the final point x_1 and if
  % the function changes sign between x_0 and x_1, the arrow from x_0
  % is not drawn
  \multido{\r=0.01+0.2}{8}{\ODEharrow{\r}}

  \pen{1pt}
  \draw[rgb(0,0.5,0)]\parafcn{0,1.5,0.1}{(t,ODErhs(1,t))}
  \draw[red]\parafcn{0,1.5,0.1}{(t,ODErhs(1,t))}
  \draw[blue]\parafcn{0,K,0.1}{(t,ODErhs(1,t))}
\endmfpic


\mfsrc{ODEarrowlength:=0.3;}


Phase portrait

\mfpic[1][4]{-0.1}{10}{-0.1}{1.5}
  \axes
  \ymarks{K}
  \tlabel[cr](0,K){$K$}
  \penwd{1pt}
  \tlabel[tc](xpos,0){$t$}
  \tlabel[bc](0,ypos){$x$}
  \pen{0.7pt}

  % This code draws arrows in 19x15 points and draws three integral
  % curves

  \multido{\r=0.0+0.1}{15}
  {\multido{\R=0.0+0.5}{19}
    {\ODEarrow{\R}{\r}}}
  \trajectories{0,0.3;0,0.01;0,1.4}

\endmfpic

\bigskip
\centerline{\bf Logistic equation with harvesting}

Similar to the previous picture, but both pictures are drawn together
to see the relations between them.

$$      x'=
      {{r}\cdot\left(1-\frac x{{K}}\right)x}-{p}
$$

% We set parameters for logistic equation with harvesting and define
% this equation.
\mfsrc{r:=1;K:=0.98;lov:=0.15;}
\ODEdefineequation{r*y*(1-(y/K))-lov}

% If the equation possesses stationary points, we store them into
% variables meza and mezb. If not, we set these variables to negative
% values (and the are not drawn in view of mfpicclip option.
\mfsrc{if (r**2*(K**2)-4*r*lov*K)<0: meza:=-1;mezb:=-1.1 
  else: meza:=(r*K-sqrt(r**2*(K**2)-4*r*lov*K))/(2*r);
  mezb:=(r*K+sqrt(r**2*(K**2)-4*r*lov*K))/(2*r) 
  fi;}


\hbox to \hsize{\hss\mfpic[1][4]{-0.1}{10}{-0.1}{1.3}
  \axes
  \ymarks{K}
  \tlabel[cr](0,K){$K$}
  \penwd{1pt}
  \tlabel[tc](xpos,0){$t$}
  \tlabel[bc](0,ypos){$x$}
  
  % here we draw lines - stationary solutions stored in meza and mezb
  % variables
  \draw[gray(0.5)]\dashed\lines{(0,meza),(xpos,meza)}
  \draw[gray(0.5)]\dashed\lines{(0,mezb),(xpos,mezb)}

  % We draw direction field using metapost cycle. Another option is to
  % use multido command as in the previous example.
  \mfsrc{for j=0 step 0.07 until 1.2:
    for i:=0 step 0.5 until 10:}
  \ODEarrow{i}{j}
  \mfsrc{endfor;endfor;}

  % We draw trajectories using black color.
  \drawcolor{black}
  \trajectories{0,0.1;0,0.2;0,0.4;0,0.6;0,0.8;0,1.1}
\endmfpic\qquad
% On the right we draw the right hand side of the equation
\mfpic[3][4]{-0.15}{0.6}{-0.1}{1.3}
  \axes
  \ymarks{K}
  \tlabel[cr](0,K){$K$}
  \tlabel[br](xpos,0){$f(x)$}
  \tlabel[bc](0,ypos){$x$}
  \pen{1pt}

  % This code draws the graph of right-hand side of logistic equation
  % without harvesting.
  \drawcolor{gray(0.7)}\draw\parafcn{0,1.2,0.1}{(ODErhs(1,t)+lov,t)}

  % This code draws the graph of right-hand side. We use the blue
  % color for positivce and red color for negative parts. We draw also
  % arrows which are determined by the sigh of the right-hand side of
  % the equation.
  \drawcolor{red}\draw\parafcn{0,1.2,0.1}{(ODErhs(1,t),t)}
  \drawcolor{blue}\parafcn{meza,mezb,0.05}{(ODErhs(1,t),t)}
  \multido{\r=0.1+0.15}{7}{\ODEvarrow{\r}}
\endmfpic\hss}

\bigskip
\centerline{\bf Three numerical methods for ODEs}

Here we draw solution of ODE using all three available numerical
methods. We use big step to see the difference between Euler,
Runge--Kutta and fourth order Runge--Kutta method.
$$    y'=x+y^3\qquad  y(0)=1
$$
  \leavevmode
  \mfpic[20][5]{0}{0.5}{0.9}{2.4}
    % We set-up parameters
    \mfsrc{ODEarrowlength:=0.5;}
    \ODEdefineequation{x+(y**3)}
    \pen{1pt}\tlabelsep{1pt}

    % We set up parameters for small step
    \nomplabels
    \drawcolor{green}
    \mfsrc{ODEstep:=0.02; ODEstepcount:=30;}
    \trajectoryRKF{0}{1}
    \tlabel[tr](0.39,2.4){\bf{Exact solution}}

    % We use bigger step to see the difference between various
    % methods.
    \mfsrc{ODEstep:=0.2; ODEstepcount:=2;}

    % We draw trajectory by Euler method
    \drawcolor{black}
    \trajectory{0}{1}
    \tlabel[bl](0.4,1.6){\bf{Euler}}
    \tlabel[tl](0,1){\bf{$k_1$}}
    \tlabel[tl](0.2,1.2){\bf{$k_1$ for second step}}
    
    % We draw trajectory by Runge-Kutta method
    \drawcolor{rgb(0.5,0.5,0.5)}
    \trajectoryRK{0}{1}
    \tlabel[cl](0.4,2.05){\bf{RK}}
    \tlabel[tl](0.1,1.1){\bf{$k_2$}}
    
    % We draw trajectory by fourth order Runge-Kutta method
    \drawcolor{rgb(1,0,0)}
    \trajectoryRKF{0}{1}
    \tlabel[tl](0.4,2.15){\bf{RK4}}
    
    \tlabelsep{3pt}

    % We draw direction field using blue arrows and metapost cycle
    \penwd{1pt}
    \drawcolor{blue}\headcolor{blue}
    \mfsrc{for j=0.9 step 0.1 until 2.3:
      for i:=0 step 0.05 until 0.5:}
    \ODEarrow{i}{j}
    \mfsrc{endfor;endfor;}
    
    
    \drawcolor{black}
    \doaxes{lbrt}
    \bmarks{0,0.2,0.4}
    \tmarks{0,0.2,0.4}
    \lmarks{1,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,2,2.2}
    \pointcolor{red}
    \pointfilltrue
    \point[4pt]{(0,1)}
    
    % We draw some other and bigger arrows in the direction field. The
    % slopes of hese arrows are important for the first step by Euler
    % and Ruge-Kutta method and the second step by Euler method.
    \pen{2pt}
    \mfsrc{ODEarrowlength:=1;}
    \colorODEarrowfalse
    \drawcolor{red}\headcolor{red}
    \ODEarrows{0,1;0.1,1.1}
    \ODEarrow{0.2}{1.2}
    
    \axislabels{b}[tc]{{$0$}0,{$0.2$}0.2,{$0.4$}0.4}
    \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}
  \endmfpic
  

\bigskip
\centerline{\bf Autonomous systems}
 
% The color arrows have no sense in the phase portrait of autonomous
% system.
\colorODEarrowfalse

We draw the phase portrait of autonomous system, nulclines, invariant
set between nulclines, trajectories. We draw arrows in regular grid
and add few more arrows on nulclines and outside the regular grid.

\mfsrc{TIMEstep:=0.05; TIMEend:=30;}

\mfpic
  [0.5]{-2}{15}{-2}{15}
  \nomplabels
  \tlabel[cc](8,15.5){\bf Competing species}
  \usemplabels

  % We set up parameters, define equations, define functions which
  % describe nulclines and store stationary points into variables z10,
  % z11, z12, z1.

  \mfsrc{a:=11;b:=1;c:=0.8;k:=10;l:=1.1;m:=1.2;}
  \mfsrc{ODEarrowlength:=0.3;}
  \ASdefineequations{x*(a-b*x-c*y)}{y*(k-l*x-m*y)}
  \fdef{xnulklina}{x}{(a-b*x)/c}
  \fdef{ynulklina}{x}{(k-l*x)/m}
  \mfsrc{z10=(0,a/c);z11=(0,k/m);z12=(a/b,0);z13=(k/l,0);}

  % Here we draw a gray polygon - invariant set fot the system.
  \pen{0.3pt}
  \gfill[gray(0.7)]\lclosed\lines{z10,z11,z13,z12}
  \axes
  \tlabel[bc](0,ypos){$y$}
  \tlabel[cl](xpos,0){$x$}
  
  \pointsize=3pt
  
  \pointfilltrue\pointcolor{red}\point{(a/b,0)}
  \draw[red]\function{0,a/b,1}{xnulklina(x)}
  \draw[red]\lines{(0,0),(0,ypos)}
  \tlabel[cr](x10,y10){$a\over c$}
  \tlabel[tc](x12,y12){$a\over b$}
    
  \pointfilltrue\pointcolor{green}\point{(0,k/m)}
  \draw[green]\function{0,k/l,1}{ynulklina(x)}
  \draw[green]\lines{(0,0),(xpos,0)}
  \tlabel[cr](x11,y11){$\alpha\over \beta$}
  \tlabel[tc](x13,y13){$\alpha\over \gamma$}
  
  
  \penwd{1.5pt}
  \drawcolor{gray(0.25)}\headcolor{gray(0.25)}
  \ASarrows{0,6;0,11;7,0;9,0;4.5,0;13,0;2,0;0,14;0,2}
  \ASarrows{3,3;8,7;13,2;3,14}
  \ASarrows{4,ynulklina(4);5,ynulklina(5);6,ynulklina(6);
    1.7,ynulklina(1.7)}
  \ASarrows{0.5,xnulklina(0.5);1.8,xnulklina(1.8);6,xnulklina(6);4,xnulklina(4);
    7.5,xnulklina(7.5)}
  
  \drawcolor{black}
  \AStrajectories{12,12;8,12;4,12;0.2,3;1,3;6,12;1,1;1,0.12;0.3,14}
  \mfsrc{TIMEstep:=-0.05; TIMEend:=5;}
  \AStrajectories{12,12;8,12;4,12;0.2,3;1,3;6,12;1,1;1,0.12;0.3,14}
  \penwd{0.5pt}
  \drawcolor{gray(0.5)}
  \headcolor{gray(0.5)}
  \multido{\r=0.5+1}{15}{\multido{\R=0.5+1}{15}{\ASarrow{\R}{\r}}}

  
\endmfpic


\break
\bigskip
\centerline{\bf Predator prey system with HollingII response function}




\mfpic[2]{-0.1}{4}{-0.1}{3}
  % we define functions and parameters, right hand sides of the system
  % and a function which defines nulcline
  \mfsrc{r:=1;K:=3;a:=1;k:=0.8;P:=1;Alfa:=0.42;}
  \fdef{funkceV}{x}{a*x/(x+P)}
  \ASdefineequations{r*x*(1-(x/K))-funkceV(x)*y}{(-Alfa+k*funkceV(x))*y}
  \fdef{xnulklina}{x}{r*(1-(x/K))*(x+P)/a}
  \mfsrc{ODEarrowlength:=0.2;}

  % Here we draw axes and nulclines
  \axes
  \tlabel[bc](0,ypos){$y$}
  \tlabel[cl](xpos,0){$x$}
  \draw[red]\function{0,K,0.1}{xnulklina(x)}
  \draw[green]\lines{(P/((k*a/Alfa)-1),0),(P/((k*a/Alfa)-1),ypos)}
  
  % Here we draw some arrows on nulclines and then arrows in the plane
  \penwd{0.5pt}
  \drawcolor{gray(0.25)}\headcolor{gray(0.25)}
  \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}
  \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)}
  \multido{\r=0.1+0.25}{20}{\multido{\R=0.1+0.25}{20}{\ASarrow{\R}{\r}}}
  
  %% We draw trajectory with IC x=2, y=2
  \drawcolor{black}
  \mfsrc{TIMEstep:=0.05; TIMEend:=10;}
  \AStrajectoryRKF{2}{2}      
  %% We continue the trajectory (spiral) from the last point
  \AStrajectoryRKF{x1}{y1}      
  \AStrajectoryRKF{x1}{y1}      
  \AStrajectoryRKF{x1}{y1}      
  \AStrajectoryRKF{x1}{y1}      
  \AStrajectoryRKF{x1}{y1}      
  \AStrajectoryRKF{x1}{y1}      
  \AStrajectoryRKF{x1}{y1}      

  %% We continue backwards
  \mfsrc{TIMEstep:=-0.05;}
  \AStrajectoryRKF{2}{2}      
  
\endmfpic


\closegraphsfile
\end