Showing posts with label Examples. Show all posts
Showing posts with label Examples. Show all posts

Tuesday, December 12, 2017

Rotational Symmetry of Order 4

Making Patterns with Rotational Symmetry of Order 4 Using a n by n Grid of Random Shaded Squares

(1) n is even

(n = 12)

gnuplot script:

n = 12
m = n/2
max(x,y) = x>y?x:y
a = 0.875
f(t) = a*0.5*cos(2*pi*t) / max(abs(cos(2*pi*t)), abs(sin(2*pi*t)))
g(t) = a*0.5*sin(2*pi*t) / max(abs(cos(2*pi*t)), abs(sin(2*pi*t)))
funX(i,t) = f(t) + 0.5 + i
funY(j,t) = g(t) + 0.5 + j
set xrange [-m-1:m+1]
set yrange [-m-1:m+1]
unset key
unset tics
unset border
set margins 0,0,0,0
set size ratio -1
set samples 1000
set parametric
set trange [0:1]
Color = "#00cc00"
set style fill solid noborder
set terminal wxt 0 size 600,600 position 0,0 title "n = ".n
seed = ceil(2147483646*rand(time(0)))
random_test = rand((time(0),seed))
set multiplot
do for [i=0:m-1]{
    do for [j=0:m-1]{
        b = rand(0)>0.5?1:0
        if (!b) {continue}
        plot funX(i,t),  funY(j,t) w filledc closed lw 1 lc rgb Color, \
            -funY(j,t),  funX(i,t) w filledc closed lw 1 lc rgb Color, \
            -funX(i,t), -funY(j,t) w filledc closed lw 1 lc rgb Color, \
             funY(j,t), -funX(i,t) w filledc closed lw 1 lc rgb Color
    }
}
unset multiplot


(2) n is odd

(n = 11)

gnuplot script:

n = 11
m = (n+1)/2.
max(x,y) = x>y?x:y
a = 0.875
f(t) = a*0.5*cos(2*pi*t) / max(abs(cos(2*pi*t)), abs(sin(2*pi*t)))
g(t) = a*0.5*sin(2*pi*t) / max(abs(cos(2*pi*t)), abs(sin(2*pi*t)))
funX(i,t) = f(t) + i
funY(j,t) = g(t) + j
set xrange [-n/2.-1:n/2.+1]
set yrange [-n/2.-1:n/2.+1]
unset key
unset tics
unset border
set margins 0,0,0,0
set size ratio -1
set samples 1000
set parametric
set trange [0:1]
Color = "#00cc00"
set style fill solid noborder
set terminal wxt 0 size 600,600 position 0,0 title "n = ".n
seed = ceil(2147483646*rand(time(0)))
random_test = rand((time(0),seed))
set multiplot
b = rand(0)>0.5?1:0
if (b) {plot f(t), g(t) w filledc closed lw 1 lc rgb Color}
do for [i=1:m-1]{
    do for [j=0:m-1]{
        b = rand(0)>0.5?1:0
        if (!b) {continue}
        plot funX(i,t),  funY(j,t) w filledc closed lw 1 lc rgb Color, \
            -funY(j,t),  funX(i,t) w filledc closed lw 1 lc rgb Color, \
            -funX(i,t), -funY(j,t) w filledc closed lw 1 lc rgb Color, \
             funY(j,t), -funX(i,t) w filledc closed lw 1 lc rgb Color
    }
}
unset multiplot


(3) n is even (Non-Reflectional Symmetry)

(n = 12)

gnuplot script:

n = 12
m = n/2
max(x,y) = x>y?x:y
f(t) = 0.5*cos(2*pi*t) / max(abs(cos(2*pi*t)), abs(sin(2*pi*t)))
g(t) = 0.5*sin(2*pi*t) / max(abs(cos(2*pi*t)), abs(sin(2*pi*t)))
funX(i,t) = f(t) + 0.5 + i
funY(j,t) = g(t) + 0.5 + j
set xrange [-m-1:m+1]
set yrange [-m-1:m+1]
unset key
unset tics
unset border
set margins 0,0,0,0
set size ratio -1
set samples 1000
set parametric
set trange [0:1]
Color = "#00cc00"
set terminal wxt 0 size 600,600 position 0,0 title "n = ".n
seed = ceil(2147483646*rand(time(0)))
random_test = rand((time(0),seed))
set multiplot
do for [i=0:m-1]{
    do for [j=0:i]{
        b = rand(0)>0.5?1:0
        if (b) {
           plot funX(i,t),  funY(j,t) w filledc closed lw 1 lc rgb Color, \
               -funY(j,t),  funX(i,t) w filledc closed lw 1 lc rgb Color, \
               -funX(i,t), -funY(j,t) w filledc closed lw 1 lc rgb Color, \
                funY(j,t), -funX(i,t) w filledc closed lw 1 lc rgb Color
        }
        if (j==i) {continue}
        if (!b) {
           plot funX(j,t),  funY(i,t) w filledc closed lw 1 lc rgb Color, \
               -funY(i,t),  funX(j,t) w filledc closed lw 1 lc rgb Color, \
               -funX(j,t), -funY(i,t) w filledc closed lw 1 lc rgb Color, \
                funY(i,t), -funX(j,t) w filledc closed lw 1 lc rgb Color
        }
    }
}
unset multiplot


(4) n is odd (Non-Reflectional Symmetry)

(n = 11)

gnuplot script:

n = 11
m = (n+1)/2.
max(x,y) = x>y?x:y
f(t) = 0.5*cos(2*pi*t) / max(abs(cos(2*pi*t)), abs(sin(2*pi*t)))
g(t) = 0.5*sin(2*pi*t) / max(abs(cos(2*pi*t)), abs(sin(2*pi*t)))
funX(i,t) = f(t) + i
funY(j,t) = g(t) + j
set xrange [-n/2.-1:n/2.+1]
set yrange [-n/2.-1:n/2.+1]
unset key
unset tics
unset border
set margins 0,0,0,0
set size ratio -1
set samples 1000
set parametric
set trange [0:1]
Color = "#00cc00"
set terminal wxt 0 size 600,600 position 0,0 title "n = ".n
seed = ceil(2147483646*rand(time(0)))
random_test = rand((time(0),seed))
set multiplot
do for [i=0:m-1]{
    do for [j=0:i]{
        b = rand(0)>0.5?1:0
        if (b) {
           plot funX(i,t),  funY(j,t) w filledc closed lw 1 lc rgb Color, \
               -funY(j,t),  funX(i,t) w filledc closed lw 1 lc rgb Color, \
               -funX(i,t), -funY(j,t) w filledc closed lw 1 lc rgb Color, \
                funY(j,t), -funX(i,t) w filledc closed lw 1 lc rgb Color
        }
        if (j==0 || j==i) {continue}
        if (!b) {
           plot funX(j,t),  funY(i,t) w filledc closed lw 1 lc rgb Color, \
               -funY(i,t),  funX(j,t) w filledc closed lw 1 lc rgb Color, \
               -funX(j,t), -funY(i,t) w filledc closed lw 1 lc rgb Color, \
                funY(i,t), -funX(j,t) w filledc closed lw 1 lc rgb Color
        }
    }
}
unset multiplot


Graph files:


( Mathematical softwares used: Graph, gnuplot )

Friday, January 27, 2017

Plotting Slope Fields using Graph

y' = x² + y² - r²  (r = 0, 1, 2, ..., 7)


y' = (x + y)/(x - y)  (method 1)


y' = (x + y)/(x - y)  (method 2)


y' = sin(x)/y + sin(y)/x


y' = cos(x) + cos(y)


y' = cos(x) sqrt(1+x²) + cos(y) sqrt(1+y²)


Download Examples:


( Mathematical software used: Graph )

Monday, October 17, 2016

Equation of a Rounded Rectangle



Equations:


The equation of a rounded rectangle centered at (0,0) with width 2(a+c), height 2(b+c)
and corner radius c can be expressed as

       max(|x| - a, 0)² + max(|y| - b, 0)² = c² ...... (I)

or

       (|x| - a + │|x| - a│)² + (|y| - b + │|y| - b│)² = (2c)² ...... (II)

, where a, b, and c are positive numbers.




Proof:


The rounded rectangle can be broken down into four parts (see the image below):
       ╭
       │ (|x| - a)² + (|y| - b)² = c²,  if |x|≧a and |y|≧b ...... (1)
       │ |x| = a + c                  ,  if |x|≧a and |y|<b ...... (2)
       │ |y| = b + c                  ,  if |x|<a and |y|≧b ...... (3)
       │ no graph                    ,  if |x|<a and |y|<b ...... (4)
       ╰

Since |x|≧a in part (2) and |y|≧b in part (3),
the above expression is equivalent to
       ╭
       │ (|x| - a)² + (|y| - b)² = c²,  if |x|≧a and |y|≧b;
       │ (|x| - a)² = c²               ,  if |x|≧a and |y|<b;
       │ (|y| - b)² = c²               ,  if |x|<a and |y|≧b;
       │ 0 = c²                        ,  if |x|<a and |y|<b.
       ╰

Now make the equations of the four parts look similar:
       ╭
       │ (|x| - a)² + (|y| - b)² = c²,  if |x|≧a and |y|≧b;
       │ (|x| - a)² +   (b - b)² = c²,  if |x|≧a and |y|<b;
       │   (a - a)² + (|y| - b)² = c²,  if |x|<a and |y|≧b;
       │   (a - a)² +   (b - b)² = c²,  if |x|<a and |y|<b.
       ╰

Since max(|x|, a) =
               ╭
               │ |x|,  if |x|≧a
               │  a,  if |x|<a
               ╰
and max(|y|, b) =
               ╭
               │ |y|,  if |y|≧b
               │  b,  if |y|<b,
               ╰
the four equations can be combined into a single one as follows:

       (max(|x|, a) - a)² + (max(|y|, b) - b)² = c²,

or equivalently,

       max(|x| - a, 0)² + max(|y| - b, 0)² = c² ...... (I)


Using the formula max(z, 0) = (z + |z|) / 2,
the above equation can be expanded to

       (|x| - a + │|x| - a│)² + (|y| - b + │|y| - b│)² = (2c)² ...... (II)




Note:


  • When a or b is zero (but not both), the equation (I) describes a stadium (curve).
  • When c is zero (a and b are positive), the equation (I) describes a rectangular region rather than a rectangle.
  • The equation  max(|x| - a, |y| - b) = 0  describes a rectangle.
  • The equation  max(|x| - a, 0)² + max(|y| - b, 0)² + max(|z| - c, 0)² = d²  describes a rounded cuboid.

max(|x|, 0)² + max(|y| - 1, 0)² = 2²
(a = 0)

max(|x| - 3, 0)² + max(|y|, 0)² = (1.5)²
(b = 0)

max(|x| - 3, 0)² + max(|y| - 2, 0)² = 0
(c = 0)

max(|x| - 4, 0)² + max(|y| - 3, 0)² + max(|z| - 1, 0)² = 1


Related posts:

Friday, June 17, 2016

Folding Stars using Equations (用方程式摺星星)














Example 1.

(n = 5, ratio = 1)

gnuplot script 1:


n = 5
ratio = 1.0
A = 2*pi/n
c = 2*cos(A)
d = ratio*c*(1 - cos(A))/(1 + cos(A))
max(x,y) = x < y ? y : x
f1(x,y) = max(max(abs(y - c) - d, y*cos(A) - x*sin(A) - c/cos(A)), \
   max(d - abs(y*cos(A) - x*sin(A) - c), y*cos(3*A) - x*sin(3*A) - c + d))
f2(x,y) = f1(y*sin(A) + x*cos(A), y*cos(A) - x*sin(A))
f3(x,y) = f1(y*sin(2*A) + x*cos(2*A), y*cos(2*A) - x*sin(2*A))
f4(x,y) = f1(y*sin(3*A) + x*cos(3*A), y*cos(3*A) - x*sin(3*A))
f5(x,y) = f1(y*sin(4*A) + x*cos(4*A), y*cos(4*A) - x*sin(4*A))
set xrange [-2.5:2.5]
set yrange [-2.5:2.5]
set size ratio -1
set samples 256
set isosamples 256
set contour base
set cntrparam levels discrete 0.0
unset key
unset surface
set table 'f1.dat'
splot f1(x,y)
unset table
set table 'f2.dat'
splot f2(x,y)
unset table
set table 'f3.dat'
splot f3(x,y)
unset table
set table 'f4.dat'
splot f4(x,y)
unset table
set table 'f5.dat'
splot f5(x,y)
unset table
plot 'f1.dat' w l lw 2 lc rgb "#0099ff", \
       'f2.dat' w l lw 2 lc rgb "#0099ff", \
       'f3.dat' w l lw 2 lc rgb "#0099ff", \
       'f4.dat' w l lw 2 lc rgb "#0099ff", \
       'f5.dat' w l lw 2 lc rgb "#0099ff"


Example 2.

(n = 6, ratio = 1)

gnuplot script 2:


n = 6
ratio = 1.0
A = 2*pi/n
c = 1
d = ratio*c/3
max(x,y) = x < y ? y : x
f1(x,y) = max(max(abs(y - c) - d, y - sqrt(3)*x - 4*c), \
   max(d - abs(y*cos(A) - x*sin(A) - c), y*cos(4*A) - x*sin(4*A) - c + d))
f2(x,y) = f1(y*sin(A) + x*cos(A), y*cos(A) - x*sin(A))
f3(x,y) = f1(y*sin(2*A) + x*cos(2*A), y*cos(2*A) - x*sin(2*A))
f4(x,y) = f1(y*sin(3*A) + x*cos(3*A), y*cos(3*A) - x*sin(3*A))
f5(x,y) = f1(y*sin(4*A) + x*cos(4*A), y*cos(4*A) - x*sin(4*A))
f6(x,y) = f1(y*sin(5*A) + x*cos(5*A), y*cos(5*A) - x*sin(5*A))
set xrange [-2.5:2.5]
set yrange [-2.5:2.5]
set size ratio -1
set samples 256
set isosamples 256
set contour base
set cntrparam levels discrete 0.0
unset key
unset surface
set table 'f1.dat'
splot f1(x,y)
unset table
set table 'f2.dat'
splot f2(x,y)
unset table
set table 'f3.dat'
splot f3(x,y)
unset table
set table 'f4.dat'
splot f4(x,y)
unset table
set table 'f5.dat'
splot f5(x,y)
unset table
set table 'f6.dat'
splot f6(x,y)
unset table
plot 'f1.dat' w l lw 2 lc rgb "#0099ff", \
       'f2.dat' w l lw 2 lc rgb "#0099ff", \
       'f3.dat' w l lw 2 lc rgb "#0099ff", \
       'f4.dat' w l lw 2 lc rgb "#0099ff", \
       'f5.dat' w l lw 2 lc rgb "#0099ff", \
       'f6.dat' w l lw 2 lc rgb "#0099ff"


Example 3.

(n = 8, ratio = 1)

Download Graph files:



( Mathematical softwares used: Graph, gnuplot )


Related posts:

Sunday, May 29, 2016

Head of a Little Girl - using the real part of a complex function





Consider the polar equation

    r³ - 3r + a = sin³θ  (where a is a fixed number, 1.7 ≦ a ≦ 1.8).

First, we plot the polar equation by plotting the equivalent rectangular equation(s).
The graph (See Image 1) consists of two disjoint closed curves, and the inner curve
looks like a little girl's hair!


Second, since the equation is a cubic equation in the variable r, we can solve it
for r in terms of θ, and plot the three solution functions r = r1(θ), r = r2(θ) and
r = r3(θ) (See Image 2). Note that the union of their graphs is the same as the
graph of the polar equation.


Finally, replace r3(θ) with its real part Re(r3(θ)) and plot again (See Image 3).
We see that the union of the graphs of r = r1(θ) and r = Re(r3(θ)) looks like
a little girl's head!



Image 1 -- Graph of
the polar equation r³ - 3r + a = sin³θ



Image 2 -- Graphs of
r = r1(θ)r = r2(θ)r = r3(θ)  (r∈R, 0≦θ≦2π)



Image 3 -- Graphs of
r = r1(θ)r = r2(θ)r = Re(r3(θ))  (r∈R, 0≦θ≦2π)



gnuplot script for image 1:

reset
a = 1.8
fun1(x,y) = (x**2 + y**2 - 3)*(x**2 + y**2)**2 \
    + a*(x**2 + y**2)*sqrt(x**2 + y**2) - y**3
fun2(x,y) = (x**2 + y**2 - 3)*(x**2 + y**2)**2 \
    - a*(x**2 + y**2)*sqrt(x**2 + y**2) - y**3
set xrange [-2.25:2.25]
set yrange [-2.25:2.25]
set size ratio -1
set samples 256
set isosamples 256
set contour base
set cntrparam levels discrete 0.0
unset key
unset surface
set table 'fun1.dat'
splot fun1(x,y)
unset table
set table 'fun2.dat'
splot fun2(x,y)
unset table
plot 'fun1.dat' w l lw 2 lc rgb "red", \
       'fun2.dat' w l lw 2 lc rgb "green"



gnuplot script for image 2:

reset
a = 1.8
h(t) = (sin(t)**3 - a + sqrt((sin(t)**3 - a)**2 - 4))**(1.0/3)
s = 2**(1.0/3)
p = (-0.5)*(1 + sqrt(3)*{0,1})
q = (-0.5)*(1 - sqrt(3)*{0,1})
r1(t) = s/h(t) + h(t)/s
r2(t) = p*s/h(t) + q*h(t)/s
r3(t) = q*s/h(t) + p*h(t)/s
set xrange [-2.25:2.25]
set yrange [-2.25:2.25]
set size ratio -1
set samples 4000
set polar
unset key
unset raxis
unset rtics
plot r1(t) w l lw 2 lc rgb "#0099ff", \
       r2(t) w l lw 2 lc rgb "green", \
       r3(t) w l lw 2 lc rgb "#ff00ff"



gnuplot script for image 3:

The script is the same as that of (2), except the last line:

       real(r3(t)) w l lw 2 lc rgb "#ff00ff"



Graph exampleHead of a Little Girl




( Mathematical softwares used: Graph, gnuplot )

Sunday, June 29, 2014

Sinusoidal Strings - made from a ring of circles

Sinusoidal Strings of Pebbles (r=0.1, n=32)

(r=0.15, n=42)

(r=0.4, n=60)

A ring of circles can be expressed as follows:
x = cos(t-mod(t,s)) + r*cos(n*mod(t,s))
y = sin(t-mod(t,s)) + r*sin(n*mod(t,s))
r=0.1, n=32, s=2*pi/n, t=0...2*pi.

Note that mod(t,s) = t - s*floor(t/s).

gnuplot Examples:


Method 1:
r = 0.1
n = 32
s = 2*pi/n
f(t) = cos(s*floor(t/s)) + r*cos(n*(t-s*floor(t/s)))
g(t) = sin(s*floor(t/s)) + r*sin(n*(t-s*floor(t/s)))
theta(t) = atan2(g(t), f(t))
set terminal wxt enhanced font "Arial, 10"
set xtics ('-2π' -2*pi, '-π' -pi, '0' 0, 'π' pi, '2π' 2*pi)
set xrange [-2*pi-0.4:2*pi+0.4]
set yrange [-pi-0.2:pi+0.2]
set size ratio -1
set samples 10000
set multiplot
set parametric
unset key
do for [k=-2:2:2]{
  plot [-pi:pi] theta(t) + k*pi, f(t) w p pt 0.5 lc rgb "#0099cc"
}
do for [k=-2:2:2]{
  plot [-pi:pi] theta(t) + k*pi, g(t) w p pt 0.5 lc rgb "#ff00ff"
}
unset multiplot


Method 2:
r = 0.1
n = 32
s = 2*pi/n
f(t) = cos(s*floor(t/s)) + r*cos(n*(t-s*floor(t/s)))
g(t) = sin(s*floor(t/s)) + r*sin(n*(t-s*floor(t/s)))
angle(t) = atan2(g(t), f(t))
theta(t) = angle(t - 2*pi*floor((t+pi)/(2*pi))) + 2*pi*floor((t+pi)/(2*pi))
set terminal wxt enhanced font "Arial, 10"
set xtics ('-2π' -2*pi, '-π' -pi, '0' 0, 'π' pi, '2π' 2*pi)
set xrange [-2*pi-0.4:2*pi+0.4]
set yrange [-pi-0.2:pi+0.2]
set size ratio -1
set samples 10000
set parametric
unset key
plot [-4*pi:4*pi] theta(t), f(t) w p pt 0.5 lc rgb "#0099cc", \
                      theta(t), g(t) w p pt 0.5 lc rgb "#ff00ff"


The result plot:

Graph Examples:

  • Method 1 ( sinusoidal_strings_01.grf )
  • Method 2 ( sinusoidal_strings_02.grf )


( Mathematical softwares used: Graph, gnuplot )

Tuesday, December 4, 2012

Sierpinski Triangle Using Inequalities

(Sierpinski triangle with hearts)


Example 1:
(iterations: 6)

(GIF animation)

gnuplot script 1:

triangle(x,y) = sqrt(3)*abs(x) - 1.5 + abs(sqrt(3)*abs(x) + 2*y - 0.5)
S0(x,y) = sqrt(3)*abs(x) - 0.75 + abs(sqrt(3)*abs(x) - 2*y - 0.25)
S1(x,y) = S0(2*x, 2*y-1) * S0(2*abs(x)-0.5*sqrt(3), 2*y+0.5)
S2(x,y) = S1(2*x, 2*y-1) * S1(2*abs(x)-0.5*sqrt(3), 2*y+0.5)
S3(x,y) = S2(2*x, 2*y-1) * S2(2*abs(x)-0.5*sqrt(3), 2*y+0.5)
S4(x,y) = S3(2*x, 2*y-1) * S3(2*abs(x)-0.5*sqrt(3), 2*y+0.5)
S5(x,y) = S4(2*x, 2*y-1) * S4(2*abs(x)-0.5*sqrt(3), 2*y+0.5)
fun(x,y) = triangle(x,y)>0 || S0(x,y)<0 || S1(x,y)<0 || S2(x,y)<0 || \
               S3(x,y)<0 || S4(x,y)<0 || S5(x,y)<0 ? 0:1
set xrange [-0.5*sqrt(3):0.5*sqrt(3)]
set yrange [-0.5:1]
set pm3d map
unset colorbox
set palette model RGB defined (0 'white', 1 'red')
set size ratio -1
set samples 640
set isosamples 640
unset key
unset xtics
unset ytics
unset border
splot fun(x,y)



Example 2:
(iterations: 6)

(GIF animation)

gnuplot script 2:

S0(x,y) = sqrt(3)*abs(x) - 0.75 + abs(sqrt(3)*abs(x) - 2*y - 0.25)
S1(x,y) = S0(2*x, 2*y - 1) * S0(2*abs(x) - 0.5*sqrt(3), 2*y + 0.5)
S2(x,y) = S1(2*x, 2*y - 1) * S1(2*abs(x) - 0.5*sqrt(3), 2*y + 0.5)
S3(x,y) = S2(2*x, 2*y - 1) * S2(2*abs(x) - 0.5*sqrt(3), 2*y + 0.5)
S4(x,y) = S3(2*x, 2*y - 1) * S3(2*abs(x) - 0.5*sqrt(3), 2*y + 0.5)
S5(x,y) = S4(2*x, 2*y - 1) * S4(2*abs(x) - 0.5*sqrt(3), 2*y + 0.5)
fun(x,y) = (S0(x,y)<0 || S2(x,y)<0 || S4(x,y)<0) ? 2 : \
               ( (S1(x,y)<0 || S3(x,y)<0 || S5(x,y)<0) ? 1 : 0 )
set xrange [-0.5*sqrt(3):0.5*sqrt(3)]
set yrange [-0.5:1]
set pm3d map
unset colorbox
set palette model RGB defined (0 'white', 1 'green', 2 'magenta')
set size ratio -1
set samples 640
set isosamples 640
unset key
unset xtics
unset ytics
unset border
splot fun(x,y)


Graph examples:




( Mathematical softwares used: Graph, gnuplot )

Monday, December 3, 2012

Sierpinski Carpet Using Inequalities


gnuplot script:

square(x,y) = abs(x + y) + abs(x - y) - 3
S0(x,y) = abs(x + y) + abs(x - y) - 1
f(x) = 3*abs(x) - 3
S1(x,y) = S0(3*x, f(y)) * S0(f(x), 3*y) * S0(f(x), f(y))
S2(x,y) = S1(3*x, f(y)) * S1(f(x), 3*y) * S1(f(x), f(y))
S3(x,y) = S2(3*x, f(y)) * S2(f(x), 3*y) * S2(f(x), f(y))
S4(x,y) = S3(3*x, f(y)) * S3(f(x), 3*y) * S3(f(x), f(y))
fun(x,y) = square(x,y)>0 || S0(x,y)<0 || S1(x,y)<0 || \
               S2(x,y)<0 || S3(x,y)<0 || S4(x,y)<0 ? 0:1
set xrange [-1.5:1.5]
set yrange [-1.5:1.5]
set pm3d map
unset colorbox
set palette model RGB defined (0 'white', 1 'red')
set size ratio -1
set samples 640
set isosamples 640
unset key
unset xtics
unset ytics
unset border
splot fun(x,y)



Graph example:

   Click here to download the example file (sierpinski_carpet.grf).



( Mathematical softwares used: Graph, gnuplot )

Sunday, September 30, 2012

Gnuplot example: Math Invaders - Remember Space Invaders and Galaxian?



This is a parameter animation (plotted upside down) for the modified Thomae function:



gnuplot script:

d = 0.001
m = 250
f(n,k,x) = k > m ? 3.2 : ( abs(k*x - int(k*x)) < d ? \
3.2-(1+0.1*n*n)/(k*k-abs(k*x)) : f(n,k+1,x) )
set xrange [-1.6:1.6]
set yrange [0:3.2]
set size ratio -1
set samples 10000
unset key
unset xtics
unset ytics
unset border
set bmargin 0
set lmargin 0
set rmargin 0
set tmargin 0
set term gif size 400, 400 animate background rgb "black"
set output "C:\math_invaders.gif"
set pointsize 0.5
do for [i=0:150]{
  plot f(i,1,sin(x)) with points pt 11 lc rgb "white"
}
set output


Note that we set less samples(10000). By incresing samples to 200000,
we can obtain a more accurate (but less fun) picture.



( Mathematical software used: gnuplot )


Related posts:

Tuesday, September 25, 2012

Gnuplot examples: plotting functions defined on rational numbers

Consider the Thomae function (or modified Dirichlet function):


This function is continuous at all irrational numbers and discontinuous at all rational numbers.

We will define f approximately as a gnuplot function (see f(1,x) below) such that we can plot
the graph of y = f(sin(x)) directly.



1. y = f(sin(x)) with linespoints
(Click on the image for a larger view.)

Script 1:
d = 0.001
m = 250
f(k,x) = k>m ? 0 : (abs(k*x-int(k*x))<d ? 1.0/k : f(k+1,x))
set xrange [-pi:pi]
set yrange [-0.5:1.5]
set samples 100000
set size ratio -1
unset key
set pointsize 0.25
plot f(1,sin(x)) with linespoints ls 7 lw 0.25 lc rgb "red"



2. y = f(sin(x)) with points
(Click on the image for a larger view.)

Script 2:
d = 0.001
m = 250
f(k,x) = k>m ? 0 : (abs(k*x-int(k*x))<d ? 1.0/k : f(k+1,x))
set xrange [-pi:pi]
set yrange [-0.5:1.5]
set samples 100000
set size ratio -1
unset key
set pointsize 0.1
plot f(1,sin(x)) with points pt 7 lc rgb "#00cc00"



3. y = f(sin(x)) with dots
(Click on the image for a larger view.)

Script 3:
d = 0.001
m = 250
f(k,x) = k>m ? 0 : (abs(k*x-int(k*x))<d ? 1.0/k : f(k+1,x))
set xrange [-pi:pi]
set yrange [-0.5:1.5]
set samples 100000
set size ratio -1
unset key
plot f(1,sin(x)) with dots lc rgb "black"



4. Golden Ring (parametric plot)
(Click on the image for a larger view.)

Script 4:
d = 0.001
m = 250
f(k,x) = k>m ? 0 : (abs(k*x-int(k*x))<d ? 1.0/k : f(k+1,x))
set xrange [-1.3:1.3]
set yrange [-1.05:1.55]
set size ratio -1
set samples 100000
set parametric
unset key
unset xtics
unset ytics
unset border
set term svg size 800, 800
set bmargin 1
set lmargin 1
set rmargin 1
set tmargin 1
set output 'C:\golden_ring.svg'
set pointsize 0.125
set multiplot
plot [0:2*pi] -f(1,sin(t))*sin(t), 1+f(1,sin(t))*cos(t) \
w p pt 7 lc rgb "#ffd700"
plot [0:2*pi] -(f(1,sin(t))*sin(t)+sin(2*t)), \
f(1,sin(t))*cos(t)+cos(2*t) w p pt 7 lc rgb "#ffd700"


Friday, October 14, 2011

Regular Polygons and Regular Star Polygons (正多邊形與正星形多邊形)



A polar representation of the regular star polygon {n/k} (or the regular polygon {n} when k = 1)
can be given by


Example files:

Monday, September 12, 2011

Flag of the United States - plotting contour maps using gnuplot (graphing inequalities using Graph)



gnuplot script:

a = 1.0
b = 1.9
c = 7.0/13.0
d = 0.76
f = 7.0/130.0
g = 19.0/300.0
k = 0.0308
l = 1.0/13.0

star(x,y) = \
( (5+sqrt(5))*abs(x) - sqrt(10-2*sqrt(5))*(y+4*k) \
   + abs( (5+3*sqrt(5))*abs(x) + sqrt(50+10*sqrt(5))*y ) \
   + abs( 2*sqrt(5)*abs(x) - 2*sqrt(25+10*sqrt(5))*y ) \
   - abs( (5-3*sqrt(5))*abs(x) - sqrt(50-10*sqrt(5))*y \
             + abs( (5+3*sqrt(5))*abs(x) + sqrt(50+10*sqrt(5))*y ) \
             - abs( 2*sqrt(5)*abs(x) - 2*sqrt(25+10*sqrt(5))*y ) ) ) / k

set xrange [0:b]
set yrange [-a:0]

fun(x,y) = \
( x < d && y > -c \
   && star( x - g, y + f ) > 0 \
   && star( x - 3*g, y + f ) > 0 \
   && star( x - 5*g, y + f ) > 0 \
   && star( x - 7*g, y + f ) > 0 \
   && star( x - 9*g, y + f ) > 0 \
   && star( x - 11*g, y + f ) > 0 \
   && star( x - g, y + 3*f ) > 0 \
   && star( x - 3*g, y + 3*f ) > 0 \
   && star( x - 5*g, y + 3*f ) > 0 \
   && star( x - 7*g, y + 3*f ) > 0 \
   && star( x - 9*g, y + 3*f ) > 0 \
   && star( x - 11*g, y + 3*f ) > 0 \
   && star( x - g, y + 5*f ) > 0 \
   && star( x - 3*g, y + 5*f ) > 0 \
   && star( x - 5*g, y + 5*f ) > 0 \
   && star( x - 7*g, y + 5*f ) > 0 \
   && star( x - 9*g, y + 5*f ) > 0 \
   && star( x - 11*g, y + 5*f ) > 0 \
   && star( x - g, y + 7*f ) > 0 \
   && star( x - 3*g, y + 7*f ) > 0 \
   && star( x - 5*g, y + 7*f ) > 0 \
   && star( x - 7*g, y + 7*f ) > 0 \
   && star( x - 9*g, y + 7*f ) > 0 \
   && star( x - 11*g, y + 7*f ) > 0 \
   && star( x - g, y + 9*f ) > 0 \
   && star( x - 3*g, y + 9*f ) > 0 \
   && star( x - 5*g, y + 9*f ) > 0 \
   && star( x - 7*g, y + 9*f ) > 0 \
   && star( x - 9*g, y + 9*f ) > 0 \
   && star( x - 11*g, y + 9*f ) > 0 \
   && star( x - 2*g, y + 2*f ) > 0 \
   && star( x - 4*g, y + 2*f ) > 0 \
   && star( x - 6*g, y + 2*f ) > 0 \
   && star( x - 8*g, y + 2*f ) > 0 \
   && star( x - 10*g, y + 2*f ) > 0 \
   && star( x - 2*g, y + 4*f ) > 0 \
   && star( x - 4*g, y + 4*f ) > 0 \
   && star( x - 6*g, y + 4*f ) > 0 \
   && star( x - 8*g, y + 4*f ) > 0 \
   && star( x - 10*g, y + 4*f ) > 0 \
   && star( x - 2*g, y + 6*f ) > 0 \
   && star( x - 4*g, y + 6*f ) > 0 \
   && star( x - 6*g, y + 6*f ) > 0 \
   && star( x - 8*g, y + 6*f ) > 0 \
   && star( x - 10*g, y + 6*f ) > 0 \
   && star( x - 2*g, y + 8*f ) > 0 \
   && star( x - 4*g, y + 8*f ) > 0 \
   && star( x - 6*g, y + 8*f ) > 0 \
   && star( x - 8*g, y + 8*f ) > 0 \
   && star( x - 10*g, y + 8*f ) > 0 ) \
? 2 : ( (x > d || y < -c) && sin(y*pi/l) < 0 ) ? 0 : 1

set pm3d map
unset colorbox
set palette model RGB defined (0 '#BB133E', 1 'white', 2 '#002664')

set size ratio -1
set samples 760, 400
set isosamples 760, 400

set bmargin 0
set lmargin 0
set rmargin 0
set tmargin 0

unset key
unset xtics
unset ytics
unset border

splot fun(x,y)


Graph example:

The grf file download page: Flag of the United States.