Solving Newton's Method with R

In summary, R doesn't recognize the functions f and f, but it works fine if I define them beforehand and then use readline() to prompt for user input. I've been able to calculate the derivative of a function using D(), but I'm having trouble turning it back into a function. I've also been struggling to type conversion between expression() and function(). I've come up with an ugly but effective solution for going from function() to expression(). However, what I want is for f(0) to be 0 since sin(0) = 0. I'm not sure if I need to use eval() or something else, but I've included the code for my revised Newton function below.
  • #1
spamiam
360
0
Hello,

I've been trying to write a program in R that implements Newton's method. I've been mostly successful, but there are two little snags that have been bothering me. Here's my code:

Code:
Newton<-function(f,f.,guess){
	#f<-readline(prompt="Function? ")
	#f.<-readline(prompt="Derivative? ")
	#guess<-as.numeric(readline(prompt="Guess? "))
	a<-rep(NA, length=1000)
	a[1]<-guess
	a[2]<-a[1]-f(a[1])/f.(a[1])
	for(i in 2:length(a)){
		if(a[i]==a[i-1]){break
		}else{
		a[i+1]<-a[i]-f(a[i])/f.(a[i])
		}
	}	
a<-a[complete.cases(a)]
return(a)
}

1) I can't get R to recognize the functions f and f. if I try using readline() to prompt for user input. I get the error "Error in Newton() : could not find function "f."" However, if I comment out the readlines (as above), define f and f. beforehand, and then everything works fine.

2) I've been trying to make R calculate the derivative of a function. The problem is that the class object with which R can take symbolic derivatives is expression(), but I want to take the derivative of a function() and have it give me a function(). In short, I'm having trouble with type conversion between expression() and function(). I have an ugly but effective solution for going from function() to expression(). Given a function f, D(body(f)[[2]],"x") will give the derivative of f. However, this output is an expression(), and I haven't been able to turn it back into a function(). Do I need to use eval() or something? I've tried subsetting, but to no avail. For instance:

Code:
> g<-expression(sin(x))
> g[[1]]
sin(x)
> f<-function(x){g[[1]]}
> f(0)
sin(x)

when what I want is f(0) = 0 since sin(0) = 0.
 
Physics news on Phys.org
  • #2
Bump? Anyone here know R?
 
  • #3
In case anyone was curious, the answers to my questions can be found here: http://stackoverflow.com/questions/8857042/r-type-conversion-expression-function.

Here's my revised code:

Code:
Newton<-function(f,f.,guess){
    g<-readline(prompt="Function? ")
    g<-parse(text=g)
    g.<-D(g,"x")
    f<-function(x){eval(g[[1]])}
    f.<-function(x){eval(g.)}
    guess<-as.numeric(readline(prompt="Guess? "))
    a<-rep(NA, length=1000)
    a[1]<-guess
    a[2]<-a[1]-f(a[1])/f.(a[1])
    for(i in 2:length(a)){
        if(a[i]==a[i-1]){break
        }else{
        a[i+1]<-a[i]-f(a[i])/f.(a[i])
        }
    }   
a<-a[complete.cases(a)]
#a<-a[1:(length(a)-1)]
return(a)
}
 

Related to Solving Newton's Method with R

1. What is Newton's Method and how does it work?

Newton's Method is a mathematical algorithm used to find roots of a given function. It involves using the derivative of the function to iteratively approach the root. The basic steps of Newton's Method are: 1) Make an initial guess for the root, 2) Calculate the slope of the function at that point, 3) Use the slope to find a new estimate for the root, 4) Repeat the process until the desired level of accuracy is achieved.

2. What is the role of R in solving Newton's Method?

R is a programming language and environment commonly used in scientific research and data analysis. In the context of solving Newton's Method, R can be used to write and execute the necessary code for implementing the algorithm and finding the root of a given function.

3. What are the advantages of using R for solving Newton's Method?

R has a wide range of built-in functions and packages that can be used to perform complex mathematical calculations and data analysis. It also has a user-friendly interface and is widely used and supported by the scientific community, making it a reliable tool for solving Newton's Method.

4. Can R be used to solve any type of function using Newton's Method?

Yes, R can be used to solve any type of function using Newton's Method as long as the function is differentiable and has a continuous derivative. This means that the function can be represented by a smooth curve and the slope at any point can be calculated.

5. Are there any limitations to using R for solving Newton's Method?

Like any other programming language, R has its own limitations. The accuracy of the results obtained using R for Newton's Method depends on the initial guess and the level of precision used in the calculations. Additionally, R may not be the most efficient tool for solving extremely complex functions or those with multiple roots. In such cases, other methods may be more suitable.

Similar threads

  • Calculus
Replies
13
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
292
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
18
Views
3K
Back
Top