meta-imp-functions.mkxl /size: 8472 b    last modification: 2024-01-16 10:22
1%D \module
2%D   [       file=meta-imp-functions,
3%D        version=2022.05.17,
4%D          title=\METAPOST\ Graphics,
5%D       subtitle=All kind of functions,
6%D         author=Mikael Sundqvist & Hans Hagen,
7%D           date=\currentdate,
8%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9%C
10%C This module is part of the \CONTEXT\ macro||package and is
11%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12%C details.
13
14% \useMPlibrary[functions]
15
16% todo: as with contours, cache
17
18\registerctxluafile{meta-imp-functions}{autosuffix}
19
20\startMPextensions
21    newscriptindex mfid_processfunction ; mfid_processfunction := scriptindex("processfunction") ;
22
23    presetparameters "function" [ name = "unknown", smoothen = false, maxpath = 65536 ] ;
24
25    def lmt_processfunction = applyparameters "function" "lmt_do_processfunction" enddef ;
26
27    vardef lmt_do_processfunction =
28        if getparameter "function" "smoothen" :
29            for k within (runscript mfid_processfunction) : pathpoint .. endfor nocycle
30        else :
31            (runscript mfid_processfunction)
32        fi
33    enddef ;
34\stopMPextensions
35
36\continueifinputfile{meta-imp-functions.mkxl}
37
38\starttext
39
40\startluacode
41    mp.registeraction {
42        name   = "ms2",
43        action = function(t, x, y)
44            return -y, x
45        end
46    }
47\stopluacode
48
49\startMPpage
50    path p ; p := lmt_processfunction [
51        name = "rungekutta"
52        lua  = "ms2",
53        x    = 1,
54        y    = 0,
55        tmax = 6pi,
56        dt   = 0.001,
57        eps  = 0.00001,
58    ] xysized (5cm,5cm) ;
59    draw       p withcolor red withpen pencircle scaled 1 ;
60    path q ; q := for i within p : pathpoint .. endfor nocycle ;
61    draw       q withcolor green withpen pencircle scaled 1/2 ;
62\stopMPpage
63
64%
65
66\startluacode
67    mp.registeraction {
68        name   = "ms3",
69        action = function(t, x, y)
70            return 1, -t*y*y
71        end
72    }
73\stopluacode
74
75\startMPpage
76    path p ; p := lmt_processfunction [
77        name = "rungekutta"
78        lua  = "ms3",
79        x    = 0,
80        y    = 2,
81        tmax = 10,
82        dt   = 0.001,
83        eps  = 0.00001,
84    ] xysized (5cm,5cm) ;
85    draw       p withcolor red withpen pencircle scaled  6 ;
86\stopMPpage
87
88%
89
90\startluacode
91    mp.registeraction {
92        name   = "ms4",
93        action = function(t, x, y)
94            return 1, -y
95        end
96    }
97\stopluacode
98
99\startMPpage
100    path p ; p := lmt_processfunction [
101        name = "rungekutta"
102        lua  = "ms4",
103        x    = 0,
104        y    = 1,
105        tmax = 10,
106        dt   = 0.001,
107        eps  = 0.00001,
108    ] scaled 5cm ;
109    draw       p withcolor red withpen pencircle scaled  6 ;
110\stopMPpage
111
112%
113
114\startluacode
115    mp.registeraction {
116        name   = "ms5",
117        action = function(t, x, y)
118            return x + y + x*x + y*y, x - y - x*x + y*y
119        end
120    }
121\stopluacode
122
123\startMPpage
124    path b ; numeric s;
125
126    s := 1cm ;
127    b := ((-2.5,-2.5) -- (1.5,-2.5) -- (1.5,1.5) -- (-2.5,1.5) -- cycle) scaled s ;
128
129    for i = -3.05 step 0.1 until 2.5 :
130        for j = -3.05 step 0.1 until 2.5 :
131            draw lmt_processfunction [
132                name = "rungekutta",
133                lua  = "ms5",
134                x    = i,
135                y    = j,
136                tmax = 10,
137                dt   = 0.1,
138                eps  = 0.001,
139                smoothen = true,
140                maxpath  = 500, % crazy large paths .. so kind of bad spec here
141            ]
142                scaled s
143                withpen pencircle scaled  0.1
144                withcolor (uniformdeviate(1),uniformdeviate(1),uniformdeviate(1))
145            ;
146        endfor ;
147    endfor ;
148
149    % so how much slack ?
150
151    draw b withpen pencircle scaled 2 ;
152    clip currentpicture to b ;
153
154\stopMPpage
155
156% https://wolfram.com/xid/0dekz2-wla
157
158\startluacode
159    mp.registeraction {
160        name   = "mma1",
161        action = function(t, x, y)
162            return 1, y*math.cos(x+y)
163        end
164    }
165\stopluacode
166
167\startMPpage
168    path p ; p := lmt_processfunction [
169        name = "rungekutta"
170        lua  = "mma1",
171        x    = 0,
172        y    = 1,
173        tmax = 30,
174        dt   = 0.001,
175        eps  = 0.00001,
176    ] xysized(10cm,5cm) ;
177    draw p withcolor red withpen pencircle scaled  2 ;
178    drawpoints p withcolor blue withpen pencircle scaled 2 ;
179\stopMPpage
180
181% https://wolfram.com/xid/0dekz2-gpx
182
183\startluacode
184    mp.registeraction {
185        name   = "mma2",
186        action = function(t, x, y)
187            return -y-x*x, 2*x-y*y*y
188        end
189    }
190\stopluacode
191
192\startMPpage
193    path p ; p := lmt_processfunction [
194        name = "rungekutta"
195        lua  = "mma2",
196        x    = 1,
197        y    = 1,
198        tmax = 20,
199        dt   = 0.001,
200        eps  = 0.00001,
201    ] xysized(10cm,10cm) ;
202    draw p withcolor red withpen pencircle scaled  2 ;
203    drawpoints p withcolor blue withpen pencircle scaled 2 ;
204\stopMPpage
205
206%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
207% https://wolfram.com/xid/0dekz2-xixft
208
209\startluacode
210    mp.registeraction {
211        name   = "mma3",
212        action = function(t, x, y)
213            return y, -0.1 * y - math.sin(x) + 0.5 * math.cos(t)
214        end
215    }
216\stopluacode
217
218\startMPpage
219    path p ; p := lmt_processfunction [
220        name    = "rungekutta"
221        lua     = "mma3",
222        x       = 0,
223        y       = 0,
224        tmax    = 100,
225        dt      = 0.001,
226        eps     = 0.00001,
227    ] xysized(10cm,10cm) ;
228    draw p withcolor red withpen pencircle scaled  2 ;
229    drawpoints p withcolor blue withpen pencircle scaled 2 ;
230\stopMPpage
231
232% https://wolfram.com/xid/0dekz2-dmilmp
233% Stiff! Not working!
234
235% \startluacode
236%     mp.registeraction {
237%         name   = "mma4",
238%         action = function(t, x, y)
239%             return y, - x + 1000 * (1 - x * x) * y
240%         end
241%     }
242% \stopluacode
243
244% \startMPpage % [instance=decimalfun]
245%     path p ; p := lmt_processfunction [
246%         name = "rungekutta"
247%         lua  = "mma4",
248%         x    = 2,
249%         y    = 0,
250%         tmax = 2000,
251%         dt   = 0.01,
252%         eps  = 0.0001
253%     ] xynormalized(10cm,10cm) ;
254%     draw p
255%         withpen pencircle scaled 2
256%         withcolor red
257%     ;
258%     %     drawpoints p withcolor blue withpen pencircle scaled 2 ;
259% \stopMPpage
260
261% https://wolfram.com/xid/0dekz2-d1orb
262% Discontinuous right-hand side
263
264\startluacode
265    mp.registeraction {
266        name   = "mma5",
267        action = function(t, x, y)
268            return 1, - math.cos(y) + math.floor(t)
269        end
270    }
271\stopluacode
272
273\startMPpage
274    save n ; n := 0 ;
275    for i = "xy", "tx", "ty" :
276        path p ; p := lmt_processfunction [
277            name    = "rungekutta",
278            type    = i,
279            lua     = "mma5",
280            x       = 0,
281            y       = 1,
282            tmax    = 3,
283            dt      = 0.001,
284            eps     = 0.0001,
285        ] xysized(10cm,10cm) shifted (n*11cm,0) ;
286        draw p withcolor red withpen pencircle scaled  2 ;
287        drawpoints p withcolor blue withpen pencircle scaled 2 ;
288        n := n + 1 ;
289    endfor ;
290\stopMPpage
291
292\startluacode
293    mp.registeraction {
294        name   = "mma6",
295        action = function(t, x, y)
296            return y, -x
297        end
298    }
299\stopluacode
300
301\startMPpage
302    save n ; n := 0 ;
303    for i = "xy", "tx", "ty" :
304        path p ; p := lmt_processfunction [
305            name    = "rungekutta",
306            kind    = i,
307            lua     = "mma6",
308            x       = 1,
309            y       = 0,
310            tmin    = 0,
311            tmax    = 6.28,
312            dt      = 0.001,
313            eps     = 0.0000001,
314        ] xysized(10cm,10cm) shifted (n*11cm,0) ;
315        draw p withcolor red withpen pencircle scaled  2 ;
316        drawpoints p withcolor blue withpen pencircle scaled 2 ;
317        n := n + 1 ;
318    endfor ;
319\stopMPpage
320
321\startMPpage
322    save n ; n := 0 ;
323    for i = "xy", "tx", "ty" :
324        path p ; p  := lmt_processfunction [
325            name     = "rungekutta",
326            kind     = i,
327            lua      = "mma6",
328            x        = 1,
329            y        = 0,
330            tmin     = 0,
331            tmax     = 6,
332            stepsize = "fixed",
333            dt       = 0.5,
334            maxpath  = 3,
335        ] xysized(10cm,10cm) shifted (n*11cm,0) ;
336        draw p withcolor red withpen pencircle scaled  2 ;
337        drawpoints p withcolor blue withpen pencircle scaled 2 ;
338        n := n + 1 ;
339    endfor ;
340\stopMPpage
341
342\stoptext
343