• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
return newtonsMethod(list,1,iter);

instead if 1 there should be x1
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Start by telling us what the algorithm is. I haven't even tried Newton's approximation for the best part of 50 years.
 
Proudly marching to the beat of a different kettle of fish... while reading this tiny ad
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic