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.

有帮助吗?

解决方案

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.

其他提示

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top