In [1]:
require 'gsl'
require 'nyaplot'
require 'nyaplot3d'
require 'nyaplot_utils'


Out[1]:
Out[1]:
Out[1]:
true

Modified Chua's attractor


In [2]:
alpha = 10.82
beta = 14.286
a = 1.3
b = 0.1
d = 0.2
func = lambda {|x| return -b*Math.sin((Math::PI*x)/(2*a)+d)}

ode_func = Proc.new { |t, y, dydt, mu|
  dydt[0] = alpha*(y[1] - func.call(y[0]))
  dydt[1] = y[0] - y[1] + y[2]
  dydt[2] = -beta*y[1]
}

solver = GSL::Odeiv::Solver.alloc(GSL::Odeiv::Step::RK8PD, [1e-11, 0.0], ode_func, 3)

t = 0.0       # initial time
t1 = 250.0    # end time
h = 1e-11      # initial step
y = GSL::Vector.alloc([0, 0.49899, 0.2]) # initial value
mat = []

while t < t1
  t, h, status = solver.apply(t, t1, h, y)
  break if status != GSL::SUCCESS
  mat.push([t, y[0],y[1],y[2]])
end

In [3]:
mat = mat.transpose
df = Nyaplot::DataFrame.new({t: mat[0], x: mat[1], y: mat[2], z: mat[3]})
df


Out[3]:
txyz
1.0e-115.6140320159400467e-110.49898999999701010.1999999999287143
6.0e-113.368419209623403e-100.49898999998206060.19999999957228576
3.0999999999999996e-101.740349925125477e-090.498989999907313130.19999999779014296
1.5599999999999998e-098.757889949652093e-090.498989999533575650.19999998887942905
7.809999999999998e-094.384559016505851e-080.49898999766488810.19999994432585955
3.9059999999999994e-082.1928409356142367e-070.49898998832144970.19999972155801454
1.9530999999999997e-071.0964766685265609e-060.498989941604239930.19999860771885214
9.765599999999998e-075.482440992932909e-060.4989897080177460.1999930385246043
4.882809999999999e-062.7412298854215525e-050.498988540074147830.19996519259247092
2.4414059999999998e-050.00013706249410868940.498982700077961750.19982596390948346
0.000122070309999999980.00068533611492926640.49895349314433020.19912984494054012
0.00061035155999999990.00342726981237280630.49880728493128460.19564986174628682
0.00305175780999999940.017151012335056040.498071939295180950.17826529924258544
0.0140137955077610440.07905389043762990.49468497104190770.10052883063525046
0.049824108378933260.28418404390753220.48277542201961954-0.14954917089523478
0.091108183471849790.52481975795850290.4678452220897041-0.4299299960466015
............
250.0-5.4008553879228980.0090524597475086615.576075334785507

In [4]:
color = Nyaplot::Colors.GnBu


Out[4]:
rgb(247,252,240)rgb(224,243,219)rgb(204,235,197)rgb(168,221,181)rgb(123,204,196)rgb(78,179,211)rgb(43,140,190)rgb(8,104,172)rgb(8,64,129)
         

In [5]:
fill = color.to_a[4]


Out[5]:
"rgb(123,204,196)"

In [6]:
plot = Nyaplot::Plot.new
line = plot.add_with_df(df, :line, :x, :y)
line.color(fill)
line.stroke_width(1)
plot


Out[6]:

In [7]:
plot = Nyaplot::Plot3D.new
plot.add_with_df(df, :line, :x, :y, :z)
plot


Out[7]:

Tamari attractor


In [8]:
a=1.013; b=-0.021;c=0.019;d=0.96;e=0;f=0.01;g=1;u=0.05;i=0.05
ode_func = Proc.new { |t, y, dydt, mu|
  dydt[0] = (y[0]-a*y[1])*Math.cos(y[2])-b*y[1]*Math.sin(y[2])
  dydt[1] = (y[0]+c*y[1])*Math.sin(y[2])+d*y[1]*Math.cos(y[2])
  dydt[2] = e + f*y[2] + g*Math.atan(((1-u)*y[1]) / (1-i)*y[0])
}

solver = GSL::Odeiv::Solver.alloc(GSL::Odeiv::Step::RK8PD, [1e-10, 0.0], ode_func, 3)

t = 0.0
t1 = 800.0
h = 1e-10
y = GSL::Vector.alloc([0.9,1,1])
mat = []

while t < t1
  t, h, status = solver.apply(t, t1, h, y)
  break if status != GSL::SUCCESS
  mat.push([y[0],y[1],y[2]])
end

mat = mat.transpose
df = Nyaplot::DataFrame.new({x: mat[0], y: mat[1].map{|v| -v}, z:mat[2]})


Out[8]:
xyz
0.8999999999956617-1.00000000012920021.0000000000742815
0.8999999999739701-1.00000000077520121.000000000445689
0.8999999998655119-1.00000000400520641.000000002302727
0.899999999323221-1.00000002015523211.0000000115879157
0.8999999966117648-1.00000010090536141.0000000580138615
0.8999999830544468-1.00000050465603251.0000002901436267
0.8999999152669365-1.00000252341000831.0000014507933692
0.8999995763063614-1.000012617195381.0000072540650027
0.8999978809279204-1.00006308650952951.0000362709961745
0.8999893896488653-1.00031544275740131.000181369975517
0.8999465738691381-1.00157746528311691.0009072227557285
0.8997235465350497-1.00789352956742981.0045454082648322
0.8983894240339892-1.03961240640856921.0229561732510872
0.8875157865156315-1.19416002408576421.116262513449279
0.865546231137117-1.40201172884964651.2537947751345435
0.8421368106796103-1.6326309648957231.4343762499054211
.........
-0.1357538892315570.70472829847809474.567022194880662

In [9]:
color = Nyaplot::Colors.GnBu


Out[9]:
rgb(247,252,240)rgb(224,243,219)rgb(204,235,197)rgb(168,221,181)rgb(123,204,196)rgb(78,179,211)rgb(43,140,190)rgb(8,104,172)rgb(8,64,129)
         

In [10]:
plot = Nyaplot::Plot.new
line = plot.add_with_df(df, :line, :y, :x)
line.stroke_width(0.3)
line.color(color.to_a[5])
plot


Out[10]:

In [ ]:
plot.export_contents 'context.svg'

In [14]:
plot = Nyaplot::Plot3D.new
plot.add_with_df(df, :line, :x, :y, :z)
plot


Out[14]: