# Newton's Method

@kirupa, I liked your email about calculating π so I came to share the “math heavy computation” I’m forcing my computer do for me. It’s Newton’s method, implemented in R, which is used to find the roots of a function:

``````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:

``````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):

1 Like

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

1 Like

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

1 Like

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

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!

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!

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

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!

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