-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrad_f.mac
233 lines (137 loc) · 3.91 KB
/
grad_f.mac
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
/*
Batch file for Maxima CAS
save as a o.mac
run maxima :
maxima
and then :
batch("grad_f.mac");
*/
kill(all);
remvalue(all);
ratprint:false; /* It doesn't change the computing, just the warnings. */
display2d:false;
numer: true;
/* functions */
log2(x) := float(log(x) / log(2))$
/*
point of the unit circle D={w:abs(w)=1 } where w=l(t)
t is angle in turns
1 turn = 360 degree = 2*Pi radians
*/
give_unit_circle_point(t):= float(rectform(%e^(%i*t*2*%pi)))$
/* circle points */
give_circle_point(center, Radius, t) := float(rectform(center + Radius*give_unit_circle_point(t)))$
/*
(scalar) potential
https://en.wikibooks.org/wiki/Fractals/Iterations_in_the_complex_plane/MandelbrotSetExterior#Complex_potential
real potential
potential = log(modulus)/2^iterations
complex quadratic polynomial
https://en.wikipedia.org/wiki/Complex_quadratic_polynomial
*/
Potential(c):= block(
[i, iMax, z, ER, n,t],
z:0,
i:0,
iMax : 1000,
n:1,
ER:1000,
t: cabs(z),
while ( i< iMax and t< ER)
do
(
z : z*z+c, /* complex quadratic polynomial */
t : cabs(z),
n : n*2,
i : i+1
),
if (t>0) then t : log2(t)/n,
return (t)
)$
/*
P(x+y*%i):=Potential(x+y*%i)$
*/
/*
numerical aproximation of the potential ( scalar funtion) gradient
on the parameter plane ( c-plane)
https://en.wikipedia.org/wiki/Complex_quadratic_polynomial#Parameter_plane
https://commons.wikimedia.org/wiki/File:Gradient_of_potential.svg
input:
* n = number of points (on the circle) to check
Gradient vector can be descibed by the
* target point cMax. When the origin of the vector is known then target point describes the gradient vector
output is a complex point cMax
* on the circle with center = Center and radius = Radius
*
*/
GradientPoint(Center, Radius, n) := block(
[
pCenter,
dp , /* finite difference of potential between center and circle point */
dpMax, /* max dp */
c, /* point on the circle */
cMax, /* c : dp = dpMax */
t, /* angle in turns */
tMax,
dt /* angle step */
],
/* */
pCenter : Potential(Center),
dpMax : 0,
dt : 1/n,
t : 0, /* start with t=0 ; it can be modified to start with previous direction ??? */
while (t < 1) do ( /* compute values (of c and dp) for all points on the circle, it can be modified to search in increasing direction and stop when decreasing */
c : give_circle_point(Center, Radius,t),
dp : Potential(c) - pCenter, /* https://en.wikipedia.org/wiki/Finite_difference#Relation_with_derivatives */
if (dp > dpMax) then (
dpMax : dp,
cMax : c,
tMax : t
),
t : t + dt
),
/* knowing good direction one can check 2 more points */
c : give_circle_point(Center, Radius,tMax + dt/2),
dp : Potential(c) - pCenter, /* https://en.wikipedia.org/wiki/Finite_difference#Relation_with_derivatives */
if (dp > dpMax) then (
dpMax : dp,
cMax : c,
tMax : t
),
c : give_circle_point(Center, Radius,tMax - dt/2),
dp : Potential(c) - pCenter, /* https://en.wikipedia.org/wiki/Finite_difference#Relation_with_derivatives */
if (dp > dpMax) then (
dpMax : dp,
cMax : c,
tMax : t
),
return(cMax)
)$
/*
gives vector from c1 to c2
width = dp
using:
from draw package :
vector([x, y], [dx,dy])
http://maxima.sourceforge.net/docs/manual/maxima_52.html#Item_003a-vector
plots vector
* with width [dx, dy]
* with origin in [x, y].
*/
give_vector(c1, c2 ):=block(
[x,y,dx,dy, s, t ],
s : [],
x: realpart(c1),
y: imagpart(c1),
/*
length = cabs(c2-c1)
angle is direction from c1 to c2
*/
dx : realpart(c2) - x,
dy : imagpart(c2) - y,
s : cons(s, [vector([x, y], [dx,dy])])
)$
/* ==================================== ===================================*/
c:-2.1;
next_c : GradientPoint(c, 0.001, 101);
ploteq(Potential,[x,-5,5],[y,-5,5],[fieldlines,"blue"])$