Newton Method with R

f = function(x) x**2 * sin(x-3) * exp(-0.5*x)
  newton=function(,, x0, tol){

Hey guys, I am trying to implement the Newton Method with a single variable into R. I think the above code should be correct so far, however I have troubles defining that the variable increase with each iteration. So basically for


I dont know how to write that the 2 new variables should be checked for the while condition.
I hope its kind of clear what I mean. can anyone give me a hint? Thanks in advance

which variable are you wanting to iterate, and by how much?

This would iterate x by 1:

    x <- x+1

Thanks for your response.

Basically x0 should be kicked out after first Iteration and x1 becomes the new x0 in the equation. Thrn x2 gets Computed with x1 and so on until the while condition is false

maybe try googling newton's method in r ... this is the second result:

g <- function(x) {
    x^3 + 4*x^2 - 10

gPrime <- function(x) {
    3*x^2 + 8*x

guess <- 1.5

tolerance <- .00001

root <- function(g, gPrime, guess, tolerance) {
    x = guess
    while (abs(g(x)) > tolerance) {
        x = x - g(x)/gPrime(x)

root(g, gPrime, guess, tolerance)
1 Like

I've seen that, however the while function is different. I need the absolute value of | x_n+1 - x_n | there

its basically like this :

x_n+1 = x_n -
then calculate x_n+1 - x_n and check if its smaller than the tolerance, if not calculate
x_n+2 = x_n+1 ....

and so on.

notice how the other example handles the x_n problem: it doesn't do x_n... it does a calculation and reassigns it to x so that x has a new value. See if you can refactor your code to do that too.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.