How to Implement Newton's Method in R?

Click For Summary
The discussion revolves around implementing Newton's method in R, focusing on two main issues. First, the user encounters an error when trying to use `readline()` for user input to define functions `f` and `f.`, as R does not recognize them. The solution involves defining these functions beforehand or using `parse()` to convert user input into a format R can understand. Second, the user struggles with converting symbolic derivatives from `expression()` back into `function()`. They have a workaround for obtaining the derivative but need help transforming the output back into a callable function. The revised code attempts to address these issues by using `eval()` and `parse()`, allowing for dynamic function creation based on user input. The discussion highlights the challenges of type conversion in R and seeks guidance on effectively implementing these solutions.
spamiam
Messages
360
Reaction score
1
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
Bump? Anyone here know R?
 
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)
}
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K
Replies
7
Views
9K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K