# Newton's Method

**URL:** <https://forum.kirupa.com/t/newtons-method/652838>\
**Category:** programming\
**Created:** [July 28, 2022, 4:42am UTC](https://forum.kirupa.com/t/newtons-method/652838 "2022-07-28T04:42:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [July 28, 2022, 4:42am UTC](https://forum.kirupa.com/t/newtons-method/652838/1 "2022-07-28T04:42:34Z")

</div>

@kirupa, I liked your email about [calculating π](https://www.kirupa.com/hodgepodge/fun_look_calculating_pi.htm) so I came to share the “math heavy computation” I’m forcing my computer do for me. It’s [Newton’s method](https://en.wikipedia.org/wiki/Newton%27s_method), implemented in R, which is used to find the roots of a function:

```auto
nmet = function(f, g, t = 1e-10) {
  while(abs(f(g)) > t) {
    g = g - f(g) / nder(f, g)
  }
  return(g)
}

```

It also uses a [numerical approximation of the derivative](https://en.wikipedia.org/wiki/Numerical_differentiation):

```auto
nder = function(f, x, h = 1e-3) {
  return((f(x + h) - f(x - h)) / 2 / h)
}

```

And we can see how it approximates the root of x2-5 over three iterations from a starting guess of 4 (red) to a final value of ~√5 (blue):

 ![image](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/b/d/bd2e7d3add3b99b33560314a43f28e32f2a3aa2b.png)

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [July 28, 2022, 2:30pm UTC](https://forum.kirupa.com/t/newtons-method/652838/2 "2022-07-28T14:30:54Z")

</div>

That is really nice! I also like how nice and compact R is 🙂

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [July 28, 2022, 8:16pm UTC](https://forum.kirupa.com/t/newtons-method/652838/3 "2022-07-28T20:16:30Z")

</div>

R is all I use now, but everyday I find something new and confusing about it. If I could still use ActionScript, I would!

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [July 28, 2022, 8:36pm UTC](https://forum.kirupa.com/t/newtons-method/652838/4 "2022-07-28T20:36:17Z")

</div>

Did you ever explore Python? That’s where a lot of people seem to be jumping to these days.

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [July 28, 2022, 10:58pm UTC](https://forum.kirupa.com/t/newtons-method/652838/5 "2022-07-28T22:58:48Z")

</div>

No, but I have thought about looking into it. I believe there are already a lot of packages for Python relating to statistics, and even biology more generally.

There are also biology-related programs like PyMOL which I use frequently and can be scripted with Python.

So, I really probably should learn it. Do you use Python? What’s the best way to get into it?

Given how much I miss ActionScript, why don’t I just get into JavaScript? I never should’ve thrown away that disc for Macromedia Studio 8; maybe it would still work!

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [July 28, 2022, 11:16pm UTC](https://forum.kirupa.com/t/newtons-method/652838/6 "2022-07-28T23:16:26Z")

</div>

I’m strongly considering creating programming tutorials for Python this year. It’s a very forgiving language with a clean syntax. It is also one of the most popular languages - just a few hairs shy of JavaScript’s massive popularity!

> **[The Incredible Growth of Python - Stack Overflow](https://stackoverflow.blog/2017/09/06/incredible-growth-python/)**

For the types of activities you are describing, Python may be a better fit than JavaScript.

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [July 29, 2022, 12:17am UTC](https://forum.kirupa.com/t/newtons-method/652838/7 "2022-07-29T00:17:55Z")

</div>

I was surprised to see R was also growing quite rapidly, and even more surprised that Java is still so popular.

If you do make Python tutorials, I’ll be your first reader!

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [July 29, 2022, 12:19am UTC](https://forum.kirupa.com/t/newtons-method/652838/8 "2022-07-29T00:19:54Z")

</div>

Haha! Please don’t wait until I do that. My ability to generate new content keeps getting slower and slower! 🐢
