# Polynomials ## Usage
require 'polynomials'
require 'gnuplot'
include Polynomials
polynomial = Polynomial.parse(ARGV[0])
points = polynomial.roots | polynomial.local_extrema | polynomial.inflection_points
max_x = points.max_by(&:x).x
min_x = points.min_by(&:x).x
start = (min_x - 0.1)
stop = (max_x + 0.1)
x = start
data_x,data_y = [],[]
while x < stop
data_x << x
data_y << polynomial.(x)
x += 0.005
end
Gnuplot.open do |gp|
Gnuplot::Plot.new(gp) do |plot|
plot.xrange "[#{start}:#{stop}]"
plot.title "Polynomial"
plot.ylabel "f(x)"
plot.xlabel "x"
plot.grid
plot.data << Gnuplot::DataSet.new( [data_x,data_y] ) do |ds|
ds.with = "lines"
ds.linewidth = 0.1
ds.title = "f(x) = #{polynomial}"
end
[:inflection_point,:root, :maximum, :minimum].each do |kind_of_point|
selected_points = points.select(&:"#{kind_of_point}?")
plot.data << Gnuplot::DataSet.new([selected_points.map(&:x), selected_points.map(&:y)]) do |ds|
ds.with = "points"
ds.linewidth = 2
ds.title = kind_of_point.to_s.pluralize.titleize
end
end
end
end
### Bash Command
% ruby examples/plot_only_mutual_data.rb '20.432 x^4 - 17.75 x^3 - 20x^2 + 5 x -50'
### Output:
## Contributing to polynomials
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
* Fork the project
* Commit and push until you are happy with your contribution
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
## Copyright
Copyright (c) 2011 Manuel Korfmann. See LICENSE.txt for
further details.