Skip to content

Instantly share code, notes, and snippets.

@t3chnoboy
Last active December 26, 2015 13:09
Show Gist options
  • Select an option

  • Save t3chnoboy/7156065 to your computer and use it in GitHub Desktop.

Select an option

Save t3chnoboy/7156065 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require "gnuplot"
require "integration"
def draw_plot
Gnuplot.open do |gp|
Gnuplot::Plot.new( gp ) do |plot|
plot.xrange "[-10:10]"
string = ""
for i in 1..6 do
string += (nth_fourier i).to_s + " "
p nth_fourier i
end
plot.title sting
plot.xlabel "x"
plot.ylabel "y"
# x = (1..6).collect { |v| v.to_f }
# y = x.collect { |v| nth_fourier v }
x = [-3.14, 0, 0, 3.14]
y = [-1, -1, 1, 1]
plot.data = [
#String function
Gnuplot::DataSet.new( "1.273*sin(x)+0.424*sin(3*x)+0.255*sin(5*x)" ) { |ds|
ds.with = "lines"
ds.title = "String function"
ds.linewidth = 4
},
#Points
Gnuplot::DataSet.new( [x, y] ) { |ds|
ds.with = "linespoints"
ds.title = "Array data"
}
]
end
end
end
def nth_fourier n
i = -(Math::PI)
b = 0.0
while i < 0
b += 0.01 * (-1) * Math.sin(n * i)
i += 0.01
end
while i < Math::PI
b += 0.01 * Math.sin(n * i)
i += 0.01
end
b = b*2/(2*Math::PI)
end
draw_plot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment