Equation graphing software?

S

Steve Litt

Hi all,

Does Ruby have any modules useful in graphing equations like y=x**2+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

Steve Litt
http://www.troubleshooters.com
(e-mail address removed)
 
E

Eric Lavigne

Does Ruby have any modules useful in graphing equations like y=x**2+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.
 
G

Gary Watson

Steve said:
Hi all,

Does Ruby have any modules useful in graphing equations like y=x**2+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

Steve Litt
http://www.troubleshooters.com
(e-mail address removed)
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.
 
K

Kevin Brown

I already checked out gnuplot and had some problems:

1) It doesn't take y**2+x**2=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
y**2+x**2=16, not the right side solved for y.

I'm thinking I start by setting the right side to 0, so it would be
y**2+x**2-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 :)

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
you'll get your circle.

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?
 
D

Dan Diebolt

--0-1776890630-1134163389=:17432
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
Use the GNU Graph utility.
=20
Octave uses GNU Graphics and it can solve your implicit equations for p=
oints.

=09
 
C

Chad Perrin

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
you'll get your circle.

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?

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

Kevin Brown

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.
 
L

Larry White

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.
 
K

Kevin Brown

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.
 
S

Sky Yin

------=_Part_21651_16021855.1134332633682
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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

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.

------=_Part_21651_16021855.1134332633682--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top