require 'gsl'
require 'nyaplot'
require 'nyaplot3d'
require 'nyaplot_utils'
true
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
mat = mat.transpose
df = Nyaplot::DataFrame.new({t: mat[0], x: mat[1], y: mat[2], z: mat[3]})
df
t | x | y | z |
---|---|---|---|
1.0e-11 | 5.6140320159400467e-11 | 0.4989899999970101 | 0.1999999999287143 |
6.0e-11 | 3.368419209623403e-10 | 0.4989899999820606 | 0.19999999957228576 |
3.0999999999999996e-10 | 1.740349925125477e-09 | 0.49898999990731313 | 0.19999999779014296 |
1.5599999999999998e-09 | 8.757889949652093e-09 | 0.49898999953357565 | 0.19999998887942905 |
7.809999999999998e-09 | 4.384559016505851e-08 | 0.4989899976648881 | 0.19999994432585955 |
3.9059999999999994e-08 | 2.1928409356142367e-07 | 0.4989899883214497 | 0.19999972155801454 |
1.9530999999999997e-07 | 1.0964766685265609e-06 | 0.49898994160423993 | 0.19999860771885214 |
9.765599999999998e-07 | 5.482440992932909e-06 | 0.498989708017746 | 0.1999930385246043 |
4.882809999999999e-06 | 2.7412298854215525e-05 | 0.49898854007414783 | 0.19996519259247092 |
2.4414059999999998e-05 | 0.0001370624941086894 | 0.49898270007796175 | 0.19982596390948346 |
0.00012207030999999998 | 0.0006853361149292664 | 0.4989534931443302 | 0.19912984494054012 |
0.0006103515599999999 | 0.0034272698123728063 | 0.4988072849312846 | 0.19564986174628682 |
0.0030517578099999994 | 0.01715101233505604 | 0.49807193929518095 | 0.17826529924258544 |
0.014013795507761044 | 0.0790538904376299 | 0.4946849710419077 | 0.10052883063525046 |
0.04982410837893326 | 0.2841840439075322 | 0.48277542201961954 | -0.14954917089523478 |
0.09110818347184979 | 0.5248197579585029 | 0.4678452220897041 | -0.4299299960466015 |
... | ... | ... | ... |
250.0 | -5.400855387922898 | 0.009052459747508661 | 5.576075334785507 |
color = Nyaplot::Colors.GnBu
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) |
---|---|---|---|---|---|---|---|---|
fill = color.to_a[4]
"rgb(123,204,196)"
plot = Nyaplot::Plot.new
line = plot.add_with_df(df, :line, :x, :y)
line.color(fill)
line.stroke_width(1)
plot
plot = Nyaplot::Plot3D.new
plot.add_with_df(df, :line, :x, :y, :z)
plot
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]})
x | y | z |
---|---|---|
0.8999999999956617 | -1.0000000001292002 | 1.0000000000742815 |
0.8999999999739701 | -1.0000000007752012 | 1.000000000445689 |
0.8999999998655119 | -1.0000000040052064 | 1.000000002302727 |
0.899999999323221 | -1.0000000201552321 | 1.0000000115879157 |
0.8999999966117648 | -1.0000001009053614 | 1.0000000580138615 |
0.8999999830544468 | -1.0000005046560325 | 1.0000002901436267 |
0.8999999152669365 | -1.0000025234100083 | 1.0000014507933692 |
0.8999995763063614 | -1.00001261719538 | 1.0000072540650027 |
0.8999978809279204 | -1.0000630865095295 | 1.0000362709961745 |
0.8999893896488653 | -1.0003154427574013 | 1.000181369975517 |
0.8999465738691381 | -1.0015774652831169 | 1.0009072227557285 |
0.8997235465350497 | -1.0078935295674298 | 1.0045454082648322 |
0.8983894240339892 | -1.0396124064085692 | 1.0229561732510872 |
0.8875157865156315 | -1.1941600240857642 | 1.116262513449279 |
0.865546231137117 | -1.4020117288496465 | 1.2537947751345435 |
0.8421368106796103 | -1.632630964895723 | 1.4343762499054211 |
... | ... | ... |
-0.135753889231557 | 0.7047282984780947 | 4.567022194880662 |
color = Nyaplot::Colors.GnBu
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) |
---|---|---|---|---|---|---|---|---|
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
plot.export_contents 'context.svg'
plot = Nyaplot::Plot3D.new
plot.add_with_df(df, :line, :x, :y, :z)
plot