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