Fast function plot program

Contributions to this software library are always welcome. Please ensure that you post program listings rather than .raw files. They give a reasonable idea of what your program does without having to load them into a DM42 and you can also include comments in your code. Check out the following link for a decoder/encoder: https://technical.swissmicros.com/decoders/dm42/

You can then copy/paste the listing and post it in "code" tags.
Post Reply
User avatar
deetee
Posts: 54
Joined: Mon Jul 22, 2019 8:01 am
Location: Vienna

Fast function plot program

Post by deetee »

Hi all!

This plot program is my first attempt in programming the DM42. So there is room for improvement and all hints are highly welcome.

"Fastplot" plots a function f(x) very fast but rough (50 4x4 dots in 200x120 GrMod 2). It scales automatically and provides zooming and (horizontally) shifting.

plot.bmp
plot.bmp (12.31 KiB) Viewed 4403 times

Call pPLOT with the x-axis-range (Y:from X:to), shift left/right with keys 1/3 or zoom out/in with keys 2/5. After plotting you can see the recent x-range stored in the variables a and b:

Code: Select all

00 { 124-Byte Prgm }
01▸LBL "pPLOT" @ Plot f(x) in Y,X - KEYS: 5/2 - zoom in/out
02 STO "b" @ to                           1/3 - shift left/right
03 R↓
04 STO "a" @ from

05 2 @ set resolution of 400x120
06 STO "GrMod"

07▸LBL 14 @ plot f(x)
08 CLLCD
09 RCL "a"
10 RCL "b"
11 XEQ "pF(X)"

12▸LBL 13 @ calculate scale for shift/zoom
13 RCL "b"
14 RCL "a"
15 -
16 4 @ scale of 25%
17 ÷
18 STO 06 @ 06:scale

19 GETKEY @ Menu
20 29 @ key1:left
21 X=Y?
22 GTO 01
23 R↓
24 31 @ key3:right
25 X=Y?
26 GTO 02
27 R↓
28 30 @ key2:out
29 X=Y?
30 GTO 03
31 R↓
32 25 @ key5:in
33 X=Y?
34 GTO 04
35 GTO 13

36▸LBL 01 @ shift left
37 RCL 06
38 STO- "a"
39 STO- "b"
40 GTO 14

41▸LBL 02 @ shift right
42 RCL 06
43 STO+ "b"
44 STO+ "a"
45 GTO 14

46▸LBL 03 @ zoom out
47 RCL 06
48 STO- "a"
49 STO+ "b"
50 GTO 14

51▸LBL 04 @ zoom in
52 RCL 06
53 STO+ "a"
54 STO- "b"
55 GTO 14
56 END
pPLOT calls pF(x) which plots f(x) between Y and X:

Code: Select all

00 { 127-Byte Prgm }
01▸LBL "pF(X)" @ Plot f(x) between Y and X
02 X<>Y
03 STO 00 @ 00:Fa
04 -
05 50
06 ÷
07 STO 01 @ 01:dx

08 RCL 00 @ initial Fmin and Fmax
09 XEQ "f(x)"
10 STO 02 @ 02:Fmin
11 STO 03 @ 03:Fmax

12 1.049 @ calculate Fmin and Fmax
13 STO 04 @ 04:index
14▸LBL 14
15 RCL 04
16 RCL 01
17 ×
18 RCL 00
19 +
20 XEQ "f(x)"
21 STO 05 @ 05:y
22 RCL 03
23 X<>Y
24 X>Y?
25 STO 03
26 RCL 02
27 RCL 05
28 X<Y?
29 STO 02
30 ISG 04
31 GTO 14

32 0.049 @ Plot
33 STO 04
34▸LBL 13
35 RCL 04
36 RCL 01
37 ×
38 RCL 00
39 +
40 XEQ "f(x)"
41 XEQ 12
42 "→→→→"
43 AGRAPH @ print dot (4x4)
44 0
45 XEQ 12
46 "√√√√"
47 AGRAPH @ print x-axis
48 ISG 04
49 GTO 13
50 RTN

51▸LBL 12 @ scale y
52 RCL 02
53 -
54 RCL 03
55 RCL 02
56 -
57 ÷
58 120
59 ×
60 +/-
61 119
62 +
63 RCL 04 @ scale x
64 4
65 ×
66 RTN
67 END
And finally f(x) calculates X=f(X) ... for example this polynomial function of third degree:

Code: Select all

00 { 23-Byte Prgm }
01▸LBL "f(x)" @ Calculate X=f(X)
02 STO 20
03 3
04 Y↑X
05 RCL 20
06 X↑2
07 5
08 ×
09 +
10 8
11 -
12 END
And all in one:
fastplot.raw
(283 Bytes) Downloaded 244 times

Regards
deetee
User avatar
deetee
Posts: 54
Joined: Mon Jul 22, 2019 8:01 am
Location: Vienna

Re: Fast function plot program

Post by deetee »

Finally I came up with an improved version:
* Draws y-axis too
* Draws "round" dots - looks better
* All in one file
* "Named" variables - for easier code reading (even if the code is slightly bigger)
* Exit with key 0 (clears all variables)

PLOT.bmp
PLOT.bmp (12.31 KiB) Viewed 4253 times

Call PLOT with the x-axis-range (Y:from X:to), shift left/right with keys 1/3, zoom out/in with keys 2/5 and exit with key 0.

Code: Select all

00 { 393-Byte Prgm }
01▸LBL "PLOT" @ Plot f(x) in Y,X - KEYS: 5/2 - zoom in/out
02 STO "b" @ to                          1/3 - shift left/right
03 R↓ @                                  0   - exit (CLV)
04 STO "a" @ from

05 2 @ set resolution of 200x120
06 STO "GrMod"
07 CLA @ Set dot (4x4)
08 6
09 XTOA
10 ├"→→"
11 6
12 XTOA

13▸LBL 11 @ Plot f(x)
14 CLLCD
15 RCL "b"
16 RCL "a"
17 XEQ 99

18▸LBL 10 @ Calculate scale for shift/zoom
19 RCL "b"
20 RCL "a"
21 -
22 4 @ scale of 25%
23 ÷
24 STO "s" @ Scale

25 GETKEY @ Menu
26 29 @ key1:Left
27 X=Y?
28 GTO 01
29 R↓
30 31 @ key3:Right
31 X=Y?
32 GTO 02
33 R↓
34 30 @ key2:Out
35 X=Y?
36 GTO 03
37 R↓
38 25 @ key5:In
39 X=Y?
40 GTO 04
41 R↓
42 34 @ key0:Exit
43 X=Y?
44 GTO 05
45 GTO 10

46▸LBL 01 @ Shift left
47 RCL "s"
48 STO- "a"
49 STO- "b"
50 GTO 11

51▸LBL 02 @ Shift right
52 RCL "s"
53 STO+ "b"
54 STO+ "a"
55 GTO 11

56▸LBL 03 @ Zoom out
57 RCL "s"
58 STO- "a"
59 STO+ "b"
60 GTO 11

61▸LBL 04 @ Zoom in
62 RCL "s"
63 STO+ "a"
64 STO- "b"
65 GTO 11

66▸LBL 05 @ Exit
67 AVIEW
68 CLLCD
69 CLV "i"
70 CLV "min"
71 CLV "max"
72 CLV "dx"
73 CLV "a"
74 CLV "b"
75 CLV "fx"
76 CLV "ay"
77 CLV "s"
78 STOP



79▸LBL 99 @ Plot f(x) between Y and X
80 - @ Calculate dx
81 50
82 ÷
83 STO "dx"
84 RCL "a" @ Initial Fmin and Fmax
85 XEQ "f(x)"
86 STO "min"
87 STO "max"

88 1.049 @ Calculate Fmin and Fmax
89 STO "i"
90▸LBL 14
91 RCL "i"
92 RCL "dx"
93 ×
94 RCL "a"
95 +
96 XEQ "f(x)"
97 STO "fx"
98 RCL "max"
99 X<>Y
100 X>Y?
101 STO "max"
102 RCL "min"
103 RCL "fx"
104 X<Y?
105 STO "min"
106 ISG "i"
107 GTO 14

108 RCL "a" @ Draw y-axis
109 +/-
110 RCL "b"
111 RCL "a"
112 -
113 ÷
114 200
115 ×
116 +/-
117 STO "ay"
118 0 @ Draw x-axis
119 XEQ 12
120 X<>Y
121 +/-
122 RCL "ay"
123 PIXEL

124 0.049 @ Plot
125 STO "i"
126▸LBL 13
127 RCL "i"
128 RCL "dx"
129 ×
130 RCL "a"
131 +
132 XEQ "f(x)"
133 XEQ 12
134 AGRAPH @ Print dot (4x4)
135 ISG "i"
136 GTO 13
137 RTN

138▸LBL 12 @ Scale y
139 RCL "min"
140 -
141 RCL "max"
142 RCL "min"
143 -
144 ÷
145 120
146 ×
147 +/-
148 119
149 +
150 RCL "i" @ Scale x
151 4
152 ×
153 RTN


154 END
Note that the function should be in a program labeled "f(x)" like:

Code: Select all

00 { 23-Byte Prgm }
01▸LBL "f(x)" @ Calculate X=f(X)
02 STO 20
03 3
04 Y↑X
05 RCL 20
06 X↑2
07 5
08 ×
09 +
10 8
11 -
12 END
Raw file:
PLOT.raw
(396 Bytes) Downloaded 259 times

Regards deetee
Post Reply