0

I'm looking to incorporate some sort of implementation of numerical solving for linear algebraic solutions in Java, like this:

5x + 4 = 2x + 3

Ideally, I would prefer to parse as little as possible, and avoid using traditional "human" methods of solutions (i.e. combine like terms, etc). I've been looking into Newton's Method and plugging in values for x to approximate a solution.

I'm having trouble getting it to work though.

Does anyone know the best general way to do this, and how it should be done in code (preferably Java)?

Additional

In Netwon's Method, you iterate until the approximation is to acceptable accuracy. The formula looks like this:

x1 = x0 - (f(x0) / (f '(x0))

where x1 is the next value for x in the iteration, and x0 is the current value (or starting guess if on first iteration).

What is f prime? Assuming f(x0) is the function of your current x estimation, what expression does f'(x0) represent?

Clarification

This is still a question of how to PROGRAM this math evaluation, not simply how to do the math.

bgroenks
  • 1,789
  • 4
  • 30
  • 59

2 Answers2

1

f'(x0) is the derivative of f evaluated at x0. You can compute an approximation to f' by evaluating:

f'(x0) ~ (f(x0+epsilon) - f(x0))/epsilon

for a suitably tiny value epsilon (because f is linear, any reasonable value of epsilon will give essentially the same result; for more general functions f the subtlety of choosing a good epsilon to use is entirely too subtle to be discussed in a S.O. post -- enroll in an upper-division undergraduate numerical analysis course).

However, since you want to avoid "human" methods, I should point out that for the specific case of linear equations, Newton's method always converges in a single iteration, and is in fact essentially equivalent to the usual algebraic solution technique.

To illustrate this, consider your example. To use Newton's method, one needs to transform the equation so that it looks like f(x) = 0:

5x + 4 = 2x + 3
5x + 4 - (2x + 3) = 0

So f(x) = 5x + 4 - (2x + 3). The derivative of f(x) is f'(x) = 5 - 2 = 3. If we start with an initial guess x0 = 0, then Newton's method gives us:

x1 = x0 - f(x0)/f'(x0)
   = 0 - (5*0 + 4 - (2*0 + 3))/3
   = 0 - (4-3)/3
   = -1/3

This is actually exactly the same operations that a human would use to solve the equation, somewhat subtly disguised. Taking the derivative isolated the x terms (5x - 2x = 3x), and evaluating at zero isolated the terms without an x (4-3 = 1). Then we divided the constant coefficient by the linear coefficient and negated to get x.

Stephen Canon
  • 97,302
  • 18
  • 172
  • 256
  • This seems to work very nicely. I was about to ask about practical ways of finding the slope (derivative) when you updated it. How much does it matter what I choose for epsilon? – bgroenks Jan 03 '13 at 21:51
  • For a linear function, it hardly matters at all -- assuming that you are using `double` to evaluate, so long as `epsilon` is within ~15 orders of magnitude of `x` you will get sane results. For more general functions, it matters tremendously, and is an extremely subtle problem. – Stephen Canon Jan 03 '13 at 21:53
  • In code, how would you choose epsilon? Fixed value, seed, etc? – bgroenks Jan 03 '13 at 21:54
  • If you're only going to be solving simple equations and aren't going to have extremely large or small values, set `epsilon = 1.0e-8` and call it a day. – Stephen Canon Jan 03 '13 at 21:56
  • Sound like a plan. Unfortunately, I discovered a bug in my expression parser, so I will have to fix that before truly implementing this. In the event that more complex equations come up, and the results begin to be inaccurate, should I just throw my hands up in defeat or is there a logical way to adjust the epsilon value? – bgroenks Jan 03 '13 at 21:59
  • Assuming that the expressions remain reasonably smooth (a technical mathematical term, but you might pretend that it means "nice"), and `x0` doesn't get too big or too small, you can stick with `1.0e-8`. If you want to be more sophisticated, you can use something like `max(1.0e-8, abs(x*1.0e-8))` which will better handle cases where `x` gets to be very very large; beyond that point, this gets to be a very subtle question that requires a lot of careful mathematics. – Stephen Canon Jan 03 '13 at 22:02
  • Ok. Thanks for your help. – bgroenks Jan 03 '13 at 22:11
0

Assuming that you don't want to use some new algorithms or rewrite old algorithms, you can use equation solver.

Mihai8
  • 2,871
  • 1
  • 18
  • 27