programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

compute one estimated root of a polynomial using Newton's Method.

Egor Kozitski
Greenhorn
Posts: 3
i need to calculate a root of polynomial using a Newtons Method and also use recursion

This is my input [2, -1, -2, 1]
and on output I am getting 1.0

Egor Kozitski
Greenhorn
Posts: 3
return newtonsMethod(list,1,iter);

instead if 1 there should be x1

Campbell Ritchie
Marshal
Posts: 56536
172
Welcome to the Ranch

That looks too complicated for “Beginning” so I shall move it.
You cannot usually correct code like that. you should work out the algorithm on paper first and then convert that to code. Please start by writing down what the algorithm is.

Egor Kozitski
Greenhorn
Posts: 3
I enchanced my algorithm a bit and now I am getting a "better output" , but its not correct

Here is my algorithm
Evaluate the polynomial at root, x0 (starting with an initial guess):
result = f(x0)

2. If the maximum number of iterations was exceeded, or the
absolute value of result is less than or equal to some epsilon
of error difference, return the root, x0.

3. Otherwise, compute the new root using Newton's Method:
x1 = x0 - f(x0)/f'(x0)

4. Recurse with new root, x1.

Here is a new code

The rest of the code was not change
I am testing this input [3,-1] and my output is 3.524746445015649
But the right output is 3.0

Campbell Ritchie
Marshal
Posts: 56536
172
Start by telling us what the algorithm is. I haven't even tried Newton's approximation for the best part of 50 years.