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
|
/************************************************************************
Copyright (C) 2003, 2004, 2006, 2007 Jaime E. Villate <villate@fe.up.pt>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
$Id: dynamics.mac,v 1.6 2010-04-20 12:55:46 villate Exp $
***********************************************************************/
load("complex_dynamics.lisp")$
load("plotdf.lisp")$
/************************************************************************
Function evolution
Description: Maxima program to draw orbits of one-dimensional mappings
Usage:
evolution(fun, initial, n, ..., [options], ...);
"fun" must be an expression which depends only on one variable.
"initial" is the initial value for that variable, and n is the number
of steps. [options] can be any options accepted by plot2d.
*/
evolution(fun, initial, n, [options]) :=
block
([z:initial, data: [[0, initial]], kwds: [], plot: numer: true, float: true],
if length(listofvars(fun)) # 1 then
error("fun should depend on one variable"),
for i thru n do
(z: ev(fun, listofvars(fun)[1]=z),
if not numberp(z) then
error("The function gave a non-numerical value:", z),
data: cons([i, z], data)),
for i thru length(options) do
(if not listp(options[i]) then error("Wrong option", options[i]),
kwds: cons(options[i][1], kwds)),
if not member('x,kwds) then options: cons(['x, -0.5, n+0.5],options),
if not member('xlabel,kwds) then options: endcons(['xlabel, "n"],options),
if not member('ylabel,kwds) then
options: endcons(['ylabel,sconcat(listofvars(fun)[1],"(n)")],options),
if not member('style,kwds) then options: endcons(['style,'points],options),
options: cons(['discrete, data], options),
apply(plot2d, options))$
/************************************************************************
Function staircase
Description: Maxima program to draw staircase diagrams of
one-dimensional mappings
Usage:
staircase(fun, initial, n, [options]);
"fun" must be an expression which depends only on one variable.
"initial" is the initial value for that variable, and n is the number
of steps. [options] can be any options accepted by plot2d.
*/
staircase(fun, initial, n, [options]) :=
block
([zf, z:initial, z1:initial, z2:initial, stair:[[initial, initial]],
kwds: [], numer: true, float: true],
if length(listofvars(fun)) # 1 then
error("fun should depend on one variable"),
for i thru n do
(zf: ev(fun, listofvars(fun)[1]=z),
if not numberp(zf) then
error("The function gave a non-numerical value:", zf),
stair: append(stair, [[z, zf], [zf, zf]]),
z: zf,
if z < z1 then z1: z,
if z > z2 then z2: z),
for i thru length(options) do
(if not listp(options[i]) then error("Wrong option", options[i]),
kwds: cons(options[i][1], kwds)),
if not member(listofvars(fun)[1],kwds) then
options: cons([listofvars(fun)[1],
4*(1.1*z1-.1*z2)/3,4*(1.1*z2-.1*z1)/3],options),
if not member('y,kwds) then
options: endcons(['y,1.1*z1-.1*z2,1.1*z2-.1*z1],options),
if not member('xlabel,kwds) then
options: endcons(['xlabel,sconcat(listofvars(fun)[1],"(n)")],options),
if not member('ylabel,kwds) then
options: endcons(['ylabel,sconcat(listofvars(fun)[1],"(n+1)")],options),
if not member('legend,kwds) then options: endcons(['legend,false],options),
options: cons([['discrete, stair], fun, listofvars(fun)[1]], options),
apply(plot2d, options))$
/************************************************************************
Function evolution2d
Description: Maxima program to show the evolution of a two-dimensional
discrete dynamical system in a 2-dimensional graph.
Usage:
evolution2d([fun1, fun2], [u, v], [u0, v0], n, ..., [options], ...);
fun1 and fun2 must be expressions which depend only on two variables
(u and v) named on the second list. The third list must give two
initial numerical values for the two variables. n is the number of
points to be shown. The options can be any accepted by plot2d.
*/
evolution2d(fun, state, initial, n, [options]) :=
block
([x: initial[1], y: initial[2], xaux, data: [],
fx: false, fy: false, kwds: [], numer: true, float: true],
if length(listofvars(fun)) >2 then
error("fun should depend on two variables."),
for i thru length(options) do
(if not listp(options[i]) then error("Wrong option", options[i]),
if options[i][1]=state[1] then
(x1: options[i][2],
x2: options[i][3],
options[i][1]:'x,
fx: true),
if options[i][1]=state[2] then
(y1: options[i][2],
y2: options[i][3],
options[i][1]: 'y,
fy: true),
kwds: cons(options[i][1], kwds)),
for i thru n do
(if (fx and fy)
then
(if ((y >= y1) and (y <= y2) and (x >= x1) and (x <= y2)) then
data: cons([x, y], data))
else
(data: cons([x, y], data)),
xaux: ev(fun[1], state[1]=x, state[2]=y),
if not numberp(xaux) then
error("Function",fun[1],"gave a non-numerical value:", xaux),
y: ev(fun[2], state[1]=x, state[2]=y),
if not numberp(y) then
error("The function",fun[2],"gave a non-numerical value:", y),
x: xaux),
if not member('xlabel,kwds) then
options: endcons(['xlabel,string(state[1])],options),
if not member('ylabel,kwds) then
options: endcons(['ylabel,string(state[2])],options),
if not member('style,kwds) then options: endcons(['style,'points],options),
options: cons(['discrete, data], options),
print("Graph passed to plot2d..."),
apply(plot2d, options))$
/************************************************************************
Function chaosgame
Description: Maxima program to play the "chaos game".
Usage:
chaosgame([[x1,y1]...[xm,ym]], [x0,y0], beta, n, [options]);
where [x0,y0] are the coordinates of the initial point, [x1,y1]
... [xm,ym] are the attracting points, beta is the ratio of the
final to initial distance from the attracting point, and n the
number of points to show.
*/
chaosgame(point,p0,b,n,[options]) :=
block
([p:ev(p0,numer), j, m:length(point), data: [p0], kwds: [],
xlabel, ylabel, numer:true, float:true],
if length(p0) # 2 then
error("Initial point",p0,"should be a list with 2 components"),
if not (numberp(p[1]) and numberp(p[2])) then
error("The components of the initial point", p,"should be numbers"),
for i thru m do
if length(point[i]) # 2 then
error("Point",point[i],"should be a list with 2 components"),
for i thru n do
(j: random(m) + 1,
p: ev(point[j] + b*(p-point[j]), numer),
if not (numberp(p[1]) and numberp(p[2])) then
error("Non-numerical coordinates were obtained:", p),
data: cons(p, data)),
for i thru length(options) do
(if not listp(options[i]) then error("Wrong option", options[i]),
kwds: cons(options[i][1], kwds)),
if not member('xlabel,kwds) then
(xlabel: sconcat("The chaos game with ",m," points"),
options: endcons(['xlabel,xlabel],options)),
if not member('ylabel,kwds) then
(ylabel: sconcat("contraction factor: ",b),
options: endcons(['ylabel,ylabel],options)),
if not member('style,kwds) then options: endcons(['style,'points],options),
options: cons(['discrete, data], options),
apply(plot2d, options))$
/************************************************************************
Function ifs
Description: Maxima program to create 2-d fractals using Barnley's
IFS (Iterated Function Systems) method.
Usage:
ifs([r1,...,rm], [A1,...,Am], [p1,...,pm]], p0, n, [options]);
where r1,...,rm are cumulative weights for the attracting points.
For instance if there are 3 points with probabilities 0.2, 0.5 and
0.3, you can use [2,7,10].
A1,...,Am, are the matrices for the m attracting points and
p1,...,pm are the 2D coordinates of those points. p0 are the 2D
coordinates of the initial point and n the number of points to be
shown.
*/
ifs(prob, mat, point, p0, n, [options]) :=
block
([p:ev(p0,numer), s, r, w:last(prob), data:[p0], xlabel, kwds: [],
m:length(prob), plist, numer:true, float:true],
if length(p0) # 2 then
error("Initial point",p0,"should be a list with 2 components"),
if not (numberp(p[1]) and numberp(p[2])) then
error("The components of the initial point", p,"should be numbers"),
if not ((length(mat)=m) and (length(point)=m)) then
error("There should be the same number of probabilities, matrices and points"),
for i thru m do
(if length(point[i]) # 2 then
error("Point",point[i],"should be a list with 2 components"),
if not numberp(prob[i]) then
error("Cumulative probability",prob[i],"should be a number"),
if not ((length(mat[i])=2) and (length(mat[i][1])=2)) then
error("Matrix",mat[i],"should be a 2x2 matrix")),
for i thru n do
(r: random(w) + 1,
s: 0,
for j while (r-prob[j]) > 0 do s:j,
p: ev(mat[s+1].p + point[s+1], numer),
plist: first(transpose(p)),
if not (numberp(plist[1]) and numberp(plist[2])) then
error("Non-numerical coordinates were obtained:", plist),
data: cons(plist, data)),
for i thru length(options) do
(if not listp(options[i]) then error("Wrong option", options[i]),
kwds: cons(options[i][1], kwds)),
if not member('xlabel,kwds) then
(xlabel: sconcat("Iterated Function System of ",m," transformations"),
options: endcons(['xlabel,xlabel],options)),
if not member('ylabel,kwds) then options: endcons(['ylabel,""],options),
if not member('style,kwds) then options: endcons(['style,'points],options),
options: cons(['discrete, data], options),
apply(plot2d, options))$
/************************************************************************
Function orbits
Description: Maxima program to draw orbit diagrams (also dubbed as
bifurcation diagrams) for first-order discrete
dynamical systems
Usage:
orbits(f, initial, n1, n2, [u, u0, uf], ..., [options], ...)
It will plot the orbits diagram for a family of first order discrete
dynamical system, with a parameter.
f must be an expression which depends only on one variable and
the parameter. The parameter must be named in the first component of
the list [u, u0, uf] (in this example it would be u) that sets up
the range of the variation for the parameter, from u0, uf. The initial
value of the parameter can be smaller than the final value, in the
cases when the bifurcations appear as the parameter decreases.
"initial" is an initial value for the state variable.
The parameter will be represented in the horizontal axis; the range of
the parameter will be divided by the value given by the nticks option,
or by 200 if that option is not used. For each value of the parameter,
the system will be left to evolve during n1 steps, and then the next
n2 steps will be plotted in the vertical axis.
The program tuns faster if a range is given to the vertical axis.
By default, the vertical scale is divide in 600 pixels. That number
can be changed with the option pixels, for instance, [pixels, 700].
*/
orbits(f, y0, n1, n2, domain, [options]) :=
block
([x, y, data: [], y1, y2, yflag: false, kwds: [], var, points, plot: [],
x0: domain[2], s, n, pixels: 600, numer:true, float: true],
var: listofvars(f),
var: delete(domain[1],var),
if length(listofvars(fun)) >2 then
error("f should depend on two variables."),
for i thru length(options) do
(if not listp(options[i]) then error("Wrong option", options[i]),
if options[i][1]=var[1] then
(y1: options[i][2],
y2: options[i][3],
if not y1 < y2 then
error("The first number has to be smaller than the second in:",options[i]),
options[i][1]: 'y,
yflag: true),
if options[i][1]='nticks then
(if integerp(options[i][2]) then
n: options[i][2]
else
error("Option nticks should have an integer value")),
if options[i][1]='pixels then
(pixels: options[i][2],
delete(options[i],options))
else
plot: endcons(options[i], plot),
kwds: cons(options[i][1], kwds)),
if not (member('nticks,kwds) and integerp(n)) then n: 200,
s: (domain[3] - domain[2])/n,
if yflag then
(for i:0 thru n do
(x: x0+i*s,
y: y0,
points: [],
for j thru n1 do
(y: ev(f, domain[1]=x, var[1]=y),
if not numberp(y) then
error("The function gave a non-numerical value:", y)),
for j thru n2 do
(y: ev(f, domain[1]=x, var[1]=y),
if ((y >= y1) and (y <= y2)) then
(k: entier(pixels*(y-y1)/(y2-y1))+1,
if k>pixels then k: pixels,
if not member(k, points) then points: cons(k, points))),
for j thru length(points) do
data: cons([x,(y2-y1)*(points[j]-0.5)/pixels+y1], data)))
else
(for i:0 thru n do
(x: x0+i*s,
y: y0,
for j thru (n1+n2)/2 do
(y: ev(f, domain[1]=x, var[1]=y),
if not numberp(y) then
error("The function gave a non-numerical value:", y),
if not yflag then
(y1:y, y2:y, yflag: true)
else
(if y<y1 then y1: y,
if y>y2 then y2: y))),
if not y1 < y2 then error("It was not possible to find any points"),
for i:0 thru n do
(x: x0+i*s,
y: y0,
points: [],
for j thru n1 do y: ev(f, domain[1]=x, var[1]=y),
for j thru n2 do
(y: ev(f, domain[1]=x, var[1]=y),
if ((y >= y1) and (y <= y2)) then
(k: entier(pixels*(y-y1)/(y2-y1))+1,
if k>pixels then k: pixels,
if not member(k, points) then points: cons(k, points))),
for j thru length(points) do
data: cons([x,(y2-y1)*(points[j]-0.5)/pixels+y1], data))),
if (length(data) > 0) then
(if not member('x,kwds) then
(if domain[3] > domain[2] then
plot: cons(['x,domain[2],domain[3]],plot)
else
plot: cons(['x,domain[3],domain[2]],plot)),
if not member('xlabel,kwds) then
plot: endcons(['xlabel,string(domain[1])],plot),
if not member('ylabel,kwds) then
plot: endcons(['ylabel,string(var[1])],plot),
if not member('style,kwds) then plot:endcons(['style,'points],plot),
plot: cons(['discrete, data], plot),
print("Graph passed to plot2d..."),
apply(plot2d, plot)))$
|