Velocity Reviews

Velocity Reviews (
-   Java (
-   -   Re: Algorithm to solve equation for variable (

Chris Smith 06-28-2003 03:59 PM

Re: Algorithm to solve equation for variable
Dan wrote:
> I am looking for an algorithm to solve an equation for any particular
> variable. For example, if I have the equation y = w * h + l (actually
> I have y = w h * l + because I'm using postfix notation), I need an
> algorithm that can solve the equation for any of w, h, l.
> I only support the four basic operators in my equations ( + - * / )
> and do not support functions. The way I store the equation is in a
> container; each item in the container is either a variable name or an
> Integer representing the operation, where + is 2, - is 3, * is 4 and /
> is 5.

Can you limit the order of the equation with respect to the variable for
which you're solving? If not, I'm afraid you'll have a tough time
finding a deterministic solution.

If you can (and the order is 1), then this isn't too difficult. You
would divide each side of the equation into terms (pieces that are all
multiplied together), and get all the terms *with* the variable on one
side, and all the terms without it on the other. Then you'd remove the
variable and divide the side that didn't have it by the side that did,
using polynomial division.

If you need to solve quadratic and other more complex equations, then
you've got harder work.

Is that what you wanted?

The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Dale King 06-30-2003 07:32 PM

Re: Algorithm to solve equation for variable
"Dan" <> wrote in message ...
> I obviously had not thought hard enough about what I wanted! I guess
> in my mind I had limited the problem so that each variable appeared
> only once in the equation. Sorry for the half-baked question.

I'm not sure your question is really half-baked, perhaps you just did not
explain it very well. Perhaps if you gave an example of what you mean by
solving the equation.

Your original problem statement gave one equation with 4 variables. There
are typically an infinite number of combination of the four variables for
which the equation will hold.

Perhaps what you meant was that given all but one of the variables solve for
the remaining one. That is doable, but there probably isn't much off the
shelf solvers for that except something like Maple.
Dale King

All times are GMT. The time now is 09:58 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.