with("plot.x"); x = linspace(-5, 5, 10); fig = figure.new(); /* draw small circles at (x,y) locations */ /* color is vector of 3 numbers between 0 and 1, in rgb format */ fig.plot{x=x, y=cos(x), linetype="circle", color=[1, 0, 0]}; /* connect the dots with solid lines, linetype is the default value "solid" */ fig.plot{x=x, y=cos(x), color=[0,1,0.5]}; /* draw circular dots at location underneath the previous curve */ fig.plot{x=x, y=cos(x) -1, linetype="dot", color=[1, 0, 1]}; /* connect the dots with smooth curve */ fig.plot{x=x, y=cos(x) -1, linetype="curve", color = [0, 0, 1]}; /* set the xrange and yrange of the picutre */ fig.setXrange([-5.5, 5.5]); fig.setYrange([-2.5, 1.5]); fig.save("curves.eps");
with("plot.x"); fig = figure.new(); for k = 0 : 99 theta = linspace(k * 2 * pi, (k + 1) * 2 * pi, 500); rho = exp(cos(theta)) - 2 * cos(4*theta)+sin(theta/12).^5; x = rho .* cos(theta); y = rho .* sin(theta); fig.plot{x=x, y=y, linetype="curve", color=rand(3)}; end fig.style = "empty"; fig.save("butterfly.eps"); fig = figure.new(); for k = 0 : 99 theta = linspace(k * 2 * pi, (k + 1) * 2 * pi, 500); rho = exp(cos(theta)) - 2.1 * cos(6*theta)+sin(theta/30).^7; x = rho .* cos(theta); y = rho .* sin(theta); fig.plot{x=x, y=y, linetype="curve", color=rand(3)}; end fig.style = "empty"; fig.save("butterfly2.eps");
![]() |
![]() |
fig = figure.new(); h = 10 * sin(pi/3); x = [-5, 5, 0]; y = [-h/3, -h/3, h*2/3]; /* the big solid triangle */ fig.polygon(x, y, "solid", [0.2, 1, 0.2]); /* recursively remove the center of triangle whose vertices are x and y */ /* figp is pointer to fig object. It needs to be a pointer since function remove_triangle_center will modify the figure object */ remove_triangle_center = function (figp, x, y) -> () L = norm([x[2] - x[1], y[2] - y[1]]); xo = x.mean; yo = y.mean; /* midpoints of the sides */ xm = [x[1] + x[2], x[2] + x[3], x[3] + x[1]] / 2; ym = [y[1] + y[2], y[2] + y[3], y[3] + y[1]] / 2; /* [1, 1, 1] is color white -- to remove the center */ figp >>. polygon(xm, ym, "solid", [1, 1, 1]); theta = pi/5; for k = 1 : 10 cx = xo + L * 0.08*cos(k * theta); cy = yo + L * 0.08*sin(k * theta); /* draw an ellipse; x and y are center coordinates 0.5 and 0.4 are long and short axis 0 is angle between long axis and horizontal "contour" is the style -- other option is "solid" [1, 0, 1] is the color -- r=1, g=0, b=1 */ figp >>. ellipse(cx, cy, 0.04 * L, 0.03 * L, 0, "contour", [1, 0, 1]); end if L >= 1 this(figp, [xm[1], x[2], xm[2]], [ym[1], y[2], ym[2]]); this(figp, [xm[2], x[3], xm[3]], [ym[2], y[3], ym[3]]); this(figp, [xm[3], x[1], xm[1]], [ym[3], y[1], ym[1]]); end end remove_triangle_center(>>fig, x, y); fig.setXrange([-5, 5]); fig.setYrange([-5, 9]); fig.title = "Sierpinsky Gasket with flower pattern"; fig.style = "empty"; fig.save("gasket.eps")
with("plot.x"); N = 50000; x = zeros(N); y = zeros(N); x[1] = 0.5; y[1] = 0.5; z = [0.5; 0.5]; p = [0.85, 0.92, 0.99, 1.00]; A1 = [0.85, 0.04; -0.04, 0.85]; b1 = [0; 1.6]; A2 = [0.20, -0.26; 0.23, 0.22]; b2 = [0; 1.6]; A3 = [-0.15, 0.28; 0.26, 0.24]; b3 = [0; 0.44]; A4 = [0, 0; 0, 0.16]; for k = 2 : N r = rand(1); if r < p[1] z = A1 * z + b1; elseif r < p[2] z = A2 * z + b2; elseif r < p[3] z = A3 * z + b3; else z = A4 * z; end x[k] = z[1]; y[k] = z[2]; end fig = figure.new(); fig.plot{x=x, y=y, linetype="dot", linewidth=0.2, color=[0.0, 0.9, 0.3]}; //fig.plot{x=x, y=y}; fig.style="empty"; fig.save("fern.eps");
with("plot.x"); /* generate a tree of n generations */ generate_tree = function (T0, fatom, xatom, n) -> T T = T0; for j = 1 : n t = T; T = ""; for k = 1 : t.length if t[k] == "F" T = T + fatom; elseif t[k] == "X" T = T + xatom; else T = T + t[k]; end end end end /* fatom = "FF-[-F+F+F]+[+F-F-F]"; xatom = ""; T0 = "F"; turning_angle = 22; */ /* draw the tree according to commands given in a string this function is recursive figp: pointer to the figure object. It needs to be a pointer since the function will modify the figure object x, y: the current position direction: the angle of the current direction angle: the angle in degree of turning when commands + and - are executed command: the string of commands kp: the pointer to the current position in the command string */ draw_tree = function (figp, x, y, direction, angle, command, kp, color) -> () k = kp>>; /* take the index of the fist command code */ while k <= command.length c = command[k]; if c == "]" kp >>= k + 1; /* let kp point to the next command code */ return; elseif c == "[" kp >>= k + 1; /* call itself to excute the commands up to the matching ] */ this(figp, x, y, direction, angle, command, kp, color); k = kp>>; continue; elseif c == "+" direction += angle/180*pi; elseif c == "-" direction -= angle/180*pi; elseif c == "F" nx = x + cos(direction); ny = y + sin(direction); figp >>. line([x, y], [nx, ny], *, *, *, color); x = nx; y = ny; end ++k; end end /* ------------- bush 1 ---------------------- */ fatom = "FF-[-F+F+F]+[+F-F-F]"; xatom = ""; T0 = "F"; turning_angle = 22; bush = generate_tree (T0, fatom, xatom, 4); /* let command pointer point to the beging of command code sequence */ k = 1; kp = >>k; fig = figure.new(); /* pass the pointer to fig to draw_tree */ draw_tree(>>fig, 0, 0, pi/2, turning_angle, bush, kp, colors.lime); fig.style="empty"; fig.save("bush1.eps"); /* ------------- bush 2 ---------------------- */ fatom = "FF"; xatom = "F[+X]F[-X]+X"; turning_angle=20; T0 = "X"; bush = generate_tree (T0, fatom, xatom, 6); k = 1; kp = >>k; fig = figure.new(); draw_tree(>>fig, 0, 0, pi/2, turning_angle, bush, kp, colors.blue); fig.style="empty"; fig.save("bush2.eps"); /* ------------- bush 3 ---------------------- */ fatom = "FF"; xatom = "F-[[X]+X]+F[+FX]-X"; turning_angle = 22.5; T0 = "X"; bush = generate_tree (T0, fatom, xatom, 6); k = 1; kp = >>k; fig = figure.new(); draw_tree(>>fig, 0, 0, pi/2, turning_angle, bush, kp, colors.purple); fig.style="empty"; fig.save("bush3.eps");
![]() |
![]() |
![]() |
with("plot.x"); /* the lorenz differential equation */ lorenz = function [a = 10, b=28, c=8/3] (t, y) -> dydt d1 = a * (y[2] - y[1]); d2 = b*y[1] - y[2] - y[1] * y[3]; d3 = y[1] * y[2] - c * y[3]; dydt = [d1; d2; d3]; end /* solve the ivp on t=[0,50] */ (x, y) = dsolve(lorenz, [0, 50], [8; 9; 10]); /* 2d plot */ fig = figure.new(); fig.plot{x = y[:,2], y = y[:,1], linetype = "curve", color = [0.2, 1, 0.1]}; fig.save("lorenz21.eps"); fig = figure.new(); fig.plot{x = y[:,2], y = y[:,3], linetype = "curve", color = [0.7, 0.2, 0.1]}; fig.save("lorenz23.eps"); fig = figure.new(); fig.plot{x = y[:,1], y = y[:,3], linetype = "curve", color = [0.2, 0.2, 0.7]}; fig.save("lorenz13.eps"); /* for 3d view plot */ cr = y.columnrange; fig = figure3d.new(); fig.light_direction = [2/3, 1/3, 3/5]; gx = [xv=cr[1,1]] (y, z) -> xv; gy = [yv=cr[1,2]] (x, z) -> yv; gz = [zv=cr[1,3]] (x, y) -> zv; fig.plot(gx, (y,z)->y, (y,z)->z, cr[:, 2], cr[:, 3], [4, 4], rand(3), rand(3)); fig.plot((x,y)->x, (x,y)->y, gz, cr[:, 1], cr[:, 2], [4, 4], rand(3), rand(3)); fig.plot((x,z)->x, gy, (x,z)->z, cr[:, 1], cr[:, 3], [4, 4], rand(3), rand(3)); fig.curve(y[:,1], y[:,2], y[:,3], rand(3)); fig.save("lorenz3d.eps");
with("plot.x"); // Solve Laplace's equation with Jacob's relaxation method // Usage: lpc(w,h,bv_up,bv_down,bv_left,bv_right,minchange) // w: width, h: height, bv_up...bv_down: boundary values, min_change: precision lpc = function (w, h, bv_up, bv_down, bv_left, bv_right, min_change = 1e-5) -> v % a crude guess initial value v=(bv_up+bv_down+bv_left+bv_right)*0.25*ones(w,h); vave=zeros(w-2,h-2); //boundary conditions v[1,:]=bv_left*ones(1,h); v[w,:]=bv_right*ones(1,h); v[:,1]=bv_down*ones(w,1); v[:,h]=bv_up*ones(w,1); change=10; while change>min_change vave=(v[2:w-1,1:h-2]+v[1:w-2,2:h-1]+v[2:w-1,3:h]+v[3:w,2:h-1])*0.25; change=max(max(abs(vave-v[2:w-1,2:h-1]))); v[2:w-1,2:h-1]=vave; end end v = lpc(30,30,5, 5, 9, 25,*); fig = figure3d.new(); fig.light_direction = [1/3, 2/3, 1/3]; c = rand(3); d = rand(3); fig.mesh(v, c, d); fig.save("laplace.eps");
with("plot.x"); fig = figure3d.new(); fx = (theta, phi)->(9 + 2 * cos(phi)) * cos(theta); fy = (theta, phi) -> (9 + 2 * cos(phi)) * sin(theta); fz = (theta, phi) -> 2 * sin(phi); fig.plot(fx, fy, fz, [0,2*pi], [0,2*pi]); fx = (theta, phi)->(4 + 1.2 * cos(phi)) * cos(theta); fy = (theta, phi) -> (4 + 1.2* cos(phi)) * sin(theta); fz = (theta, phi) -> 2+1.2 * sin(phi); fig.plot(fx, fy, fz, [0,2*pi], [0,2*pi]); fx = (theta, phi)->1+(9 + 3 * cos(phi)) * cos(theta); fy = (theta, phi) -> 3 * sin(phi); fz = (theta, phi) -> (9 + 3 * cos(phi)) * sin(theta); fig.plot(fx, fy, fz, [0.7,1.2*pi], [0,2*pi]); fx = (theta, phi)->7+(3 + 0.6 * cos(phi)) * cos(theta); fy = (theta, phi) -> 0.6 * sin(phi); fz = (theta, phi) -> (3 + 0.6 * cos(phi)) * sin(theta); fig.plot(fx, fy, fz, [0,2*pi], [0,2*pi]); fig.light_direction = [2/3, 2/3, 1/3]; fig.save("torus.eps");
with("plot.x"); /* it's not necessary to use global variable in order to use function parameter just to show it's possible */ global.n = 3; global.a = 8; global.b=35; global.c=5; fig = figure3d.new(); fig.light_direction = [2/3, -2/3, 1/4]; fx = (s, t)->a * (1-t/(2*pi))*cos(n*t) * (1+cos(s)) + c * cos(n*t); fy = (s, t)->a * (1-t/(2*pi))*sin(n*t) * (1+cos(s)) + c * sin(n*t); fz = (s, t)->b * t / (2*pi) + a *(1-t/(2*pi)) * sin(s); fig.plot(fx, fy, fz, [0,2*pi], [0,6], [50, 50]); fig.save("seashell.eps");
oz 2009-12-22