# Equation graphing software?

Hi all,

Does Ruby have any modules useful in graphing equations like y=x2+5,
y
2+x**2=16, 4y+3x=28, and the like? I suppose the two tasks involved
are
getting the points, and then graphing them. I’m on kind of a tight
schedule
so I hoped not to write either of those from scratch.

Thanks

SteveT

Does Ruby have any modules useful in graphing equations like y=x2+5,
y
2+x**2=16, 4y+3x=28, and the like?

gnuplot does this: http://gnuplot.info/
Gnuplot can create your graphs as files or display them on the screen.
Gnuplot is ordinarily used as a standalone program which reads a
script, but you can make it work with your program by putting gnuplot
at the end of a pipeline. Your program writes gnuplot commands to
standard-out:

ruby myprog.rb | gnuplot

If you need to do something more complicated (or if your OS doesn’t
support pipes), creating an object that acts as an output stream to a
gnuplot process would be fairly easy.

http://rubyforge.org/projects/rgplot/

Module providing useful methods for interfacing with a Gnuplot
process. The homepage provides the details of its use.

Steve L. wrote:

I might be misuderstanding what you want, but really all you need to do
is to evaluate the expression by supplying sucessive values for x.

for instance to print out the plot points for x**2 which should be a
porabola you could do something like the following

for x in -10…10
puts “x = #{x}: y = #{x**2}”
end

where you simply adjust the step value and range of the for loop to get
the granularity you want.

Ruby makes it even nicer becuase you could take in user input as a
string and then use the eval function to evalute it as in

string = gets

for x in -10…10
puts “x = #{x}: y = #{eval string}”
end

As far as plotting the values in a graphical way, you’re going to have
to use one of the GUI toolkits which are available for ruby. TK is a
very fine one which is easy to learn how to use and has the benefit of
coming with ruby by default. I blieve you can plot points and draw on a
TKCanvas widget but you’ll have to read the tk docs to figure that out.

Take a look at Octave - a Matlab Clone:

http://www.octave.org/

On Friday 09 December 2005 01:22 pm, Eric L. wrote:

ruby myprog.rb | gnuplot

If you need to do something more complicated (or if your OS doesn’t
support pipes), creating an object that acts as an output stream to a
gnuplot process would be fairly easy.

Thanks Eric,

1. It doesn’t take y2+x2=16, but instead requires you to solve for y
and
put in (16-x**2)**0.5, which gives you only the upper half of the
circle.

2. I thought, OK, I’ll make all the points in Ruby, and pipe them to
gnuplot.
No joy – gnuplot’s interpolation between points is stupid – all but
bezier
just produce a straight line, and bezier produces cusps where there
aren’t
any. I spoze I could send LOTS of points, and that might work.

Which brings up the next point – algorithm. Remember, I want the full
y2+x2=16, not the right side solved for y.

I’m thinking I start by setting the right side to 0, so it would be
y2+x2-16=0. That’s OK in this situation. Then I walk up each axis
until I
find a point where the right side shifts from positive to negative or
vice
versa. Interpolate, try again, interpolate again til I have a reasonably
close point. Now go out maybe 0.1 in each direction and find points. Now
calculate slopes, go out some more along that slope and do it again.
Pretty
soon I’ve traced out all relevant points on a continuous curve (this
won’t
work with discontinuous curves).

Now that you see the algorithm I was contemplating, you probably
understand
why I was hoping Ruby had a module that produces points for me

Thanks

SteveT

On Friday 09 December 2005 14:58, Steve L. wrote:

gnuplot. No joy – gnuplot’s interpolation between points is stupid – all
reasonably close point. Now go out maybe 0.1 in each direction and find
points. Now calculate slopes, go out some more along that slope and do it
again. Pretty soon I’ve traced out all relevant points on a continuous
curve (this won’t work with discontinuous curves).

Now that you see the algorithm I was contemplating, you probably understand
why I was hoping Ruby had a module that produces points for me

Use the GNU Graph utility. It has no problem plotting regions and all
kinds
of cool things. You just give it a list of points in the right order,
and
that’s hunky dory. (so if you got negative and positive, as is the case
for
your above equation, just give both (but obviously in the right order)
and

http://www.gnu.org/software/plotutils/manual/html_mono/plotutils.html#SEC2

And actually, I’m currently producing a ruby wrapper for this very
program
that will be LGPL’ed or BSD’ed, so how long can you wait to have this
functionality?

Use the GNU Graph utility.

Octave uses GNU Graphics and it can solve your implicit equations for
points.

On Friday 09 December 2005 16:04, Chad P. wrote:

And actually, I’m currently producing a ruby wrapper for this very
program that will be LGPL’ed or BSD’ed, so how long can you wait to have
this functionality?

Are you trying to decide between the two licenses, or are you releasing
it under both?

I’m going to release it under a commercially friendly license. Let me
know if
you have a preference and I’d be happy to be accomodating.

Generally speaking, I prefer BSD (or Apache) licenses for commercial
work, though for most purposes LGPL is fine. It really depends on
your intentions since you did the heavy lifting.

The fewer restrictions, the more commercial entities will like it -
the less I have to explain to the company lawyers the better - but you
need to decide whether you’d be happy in the (fairly unlikely) event
that someone goes off and tries to commercialize a version and keep
their changes proprietary.

On Sat, Dec 10, 2005 at 06:16:55AM +0900, Kevin B. wrote:

that will be LGPL’ed or BSD’ed, so how long can you wait to have this
functionality?

Are you trying to decide between the two licenses, or are you releasing
it under both?

Chad P. [ CCD CopyWrite | http://ccd.apotheon.org ]

unix virus: If you’re using a unixlike OS, please forward
this to 20 others and erase your system partition.

On Sunday 11 December 2005 09:07, Larry W. wrote:

Generally speaking, I prefer BSD (or Apache) licenses for commercial
work, though for most purposes LGPL is fine. It really depends on
your intentions since you did the heavy lifting.

The fewer restrictions, the more commercial entities will like it -
the less I have to explain to the company lawyers the better - but you
need to decide whether you’d be happy in the (fairly unlikely) event
that someone goes off and tries to commercialize a version and keep
their changes proprietary.

Well, I am a commercial entity, and this will be created for my company,
so
you’re guarenteed to have a permissive license. No worries.

A ruby noob question: If I want to run an external program within ruby
code
(instead of running it outside like the example you gave), how can I
pass a
string through the pipeline to the program?

Thanks

IO::popen(“myprogg”, “w”) do |pipe|
pipe.puts(“Look at me pass stuff through an interprocess pipe!”)
end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.