[R] NEWTON ALGORITHM

2011-12-22 Thread Curtis Moyo
Hi,

My name is Curtis and I'm a 1st year student in Biochemistry at the
University of Geneva. I need some help completing the code for my NEWTON
ALGORITHM. It is a bonus exercice to our autumn semester maths exam and we
can hand it in or not. Usually people copy and paste but I decided to sit
down and review theory and ask for help left right and center.

My problem is that I cannot get my function to give me the solution. Here
is my code:

## PRIMARY FUNCTION
f=function(x){
out=(2/(3))*exp(x^3)-(10)*log(x)
return(out)
}
## 1ST DERIVATIVE
fp=function(x){
out=(2)*(x^2)*exp(x^3)-(10/x)
return(out)
}
## 2ND DERIVATIVE
fpp=function(x){
out=(4)*(x)*exp(x^3)+(6)*(x^4)*exp(x^3)+(10/x^2)
return(out)
}

I am trying to find the zero of  fp,my 1st derivative, with my newton
algorithm:

newbon=function(x0,epsi,f,fp){

## where a corresponds to x^n et

## b corresponds to x^n+1

## We are looking for x^n+1 such that f(x^n+1)=0

## NB: fp(a)*(a-b)=f(a) when f(b)=0

  a=x0
  b=(fp(a)*a-f(a))/fp(a)
while(abs(-fp(a)*(b-a))epsi){
  a=b
  b=(fp(a)*a-f(a))/fp(a)
}
  out=NULL
  out=a
return(out)
}

I must admit that my algorithm was painfully congested and I've been having
headaches to get it to work. However, I always get the same error message:

Erreur dans while (abs(-fp(a) * (b - a))  epsi) { :
  valeur manquante là où TRUE / FALSE est requis
De plus : Message d'avis :
In log(x) : production de NaN

Please forgive the French; it is my study language. Quite frankly I don't
mind if I don't get the bonus marks, what is really important is that I
understand why it doesn't work and what condition is missing.

You're help would be kindly appreciated.

Curtis MOYO

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] NEWTON ALGORITHM

2011-12-22 Thread Uwe Ligges



On 21.12.2011 22:43, Curtis Moyo wrote:

Hi,

My name is Curtis and I'm a 1st year student in Biochemistry at the
University of Geneva. I need some help completing the code for my NEWTON
ALGORITHM. It is a bonus exercice to our autumn semester maths exam and we
can hand it in or not. Usually people copy and paste but I decided to sit
down and review theory and ask for help left right and center.

My problem is that I cannot get my function to give me the solution. Here
is my code:

## PRIMARY FUNCTION
f=function(x){
out=(2/(3))*exp(x^3)-(10)*log(x)
return(out)
}
## 1ST DERIVATIVE
fp=function(x){
out=(2)*(x^2)*exp(x^3)-(10/x)
return(out)
}
## 2ND DERIVATIVE
fpp=function(x){
out=(4)*(x)*exp(x^3)+(6)*(x^4)*exp(x^3)+(10/x^2)
return(out)
}

I am trying to find the zero of  fp,my 1st derivative, with my newton
algorithm:

newbon=function(x0,epsi,f,fp){

## where a corresponds to x^n et

## b corresponds to x^n+1

## We are looking for x^n+1 such that f(x^n+1)=0

## NB: fp(a)*(a-b)=f(a) when f(b)=0

   a=x0
   b=(fp(a)*a-f(a))/fp(a)
while(abs(-fp(a)*(b-a))epsi){
   a=b
   b=(fp(a)*a-f(a))/fp(a)
}
   out=NULL
   out=a
return(out)
}

I must admit that my algorithm was painfully congested and I've been having
headaches to get it to work. However, I always get the same error message:

Erreur dans while (abs(-fp(a) * (b - a))  epsi) { :
   valeur manquante là où TRUE / FALSE est requis
De plus : Message d'avis :
In log(x) : production de NaN



This question (like all homework questions) is inappropriate on this 
mailing list and we usually do not give any answers in such cases! You 
should ask your instructor. She or he will certainly answer like this:


If you read that: log() produced NaN, hence was undefined. Now, you 
have only one function call for log, so think about when log(x) is 
undefined ...


Uwe Ligges






Please forgive the French; it is my study language. Quite frankly I don't
mind if I don't get the bonus marks, what is really important is that I
understand why it doesn't work and what condition is missing.

You're help would be kindly appreciated.

Curtis MOYO

[[alternative HTML version deleted]]




__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] NEWTON ALGORITHM

2011-12-22 Thread Berend Hasselman

delongman wrote
 
 Hi,
 
 My name is Curtis and I'm a 1st year student in Biochemistry at the
 University of Geneva. I need some help completing the code for my NEWTON
 ALGORITHM. It is a bonus exercice to our autumn semester maths exam and we
 can hand it in or not. Usually people copy and paste but I decided to sit
 down and review theory and ask for help left right and center.
 
 My problem is that I cannot get my function to give me the solution. Here
 is my code:
 
 ## PRIMARY FUNCTION
 f=function(x){
 out=(2/(3))*exp(x^3)-(10)*log(x)
 return(out)
 }
 ## 1ST DERIVATIVE
 fp=function(x){
 out=(2)*(x^2)*exp(x^3)-(10/x)
 return(out)
 }
 ## 2ND DERIVATIVE
 fpp=function(x){
 out=(4)*(x)*exp(x^3)+(6)*(x^4)*exp(x^3)+(10/x^2)
 return(out)
 }
 
 I am trying to find the zero of  fp,my 1st derivative, with my newton
 algorithm:
 
 newbon=function(x0,epsi,f,fp){
 
 ## where a corresponds to x^n et
 
 ## b corresponds to x^n+1
 
 ## We are looking for x^n+1 such that f(x^n+1)=0
 
 ## NB: fp(a)*(a-b)=f(a) when f(b)=0
 
   a=x0
   b=(fp(a)*a-f(a))/fp(a)
 while(abs(-fp(a)*(b-a))epsi){
   a=b
   b=(fp(a)*a-f(a))/fp(a)
 }
   out=NULL
   out=a
 return(out)
 }
 
 I must admit that my algorithm was painfully congested and I've been
 having
 headaches to get it to work. However, I always get the same error message:
 
 Erreur dans while (abs(-fp(a) * (b - a))  epsi) { :
   valeur manquante là où TRUE / FALSE est requis
 De plus : Message d'avis :
 In log(x) : production de NaN
 
 Please forgive the French; it is my study language. Quite frankly I don't
 mind if I don't get the bonus marks, what is really important is that I
 understand why it doesn't work and what condition is missing.
 
 You're help would be kindly appreciated.
 

As others have told you: no homework.

But to give you some additional hints:

1. are you sure that your function has a solution for f(x)=0?

2. make your code readable: indent and use - for assignment.

3. Try your newbon-function (shouldn't that be newton?) with this

g - function(x){
out - (2/(3))*exp(x^2)-(20)*log(x)
return(out)
}

gp - function(x) {
out - (4/3)*exp(x) - 20/x
return(out)
}

4.  do  curve(f, number,number) and the same for g.

The rest is up to you if you want to deserve a well-earned bonus.

Berend


--
View this message in context: 
http://r.789695.n4.nabble.com/NEWTON-ALGORITHM-tp4224558p4224816.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] NEWTON ALGORITHM

2011-12-22 Thread Berend Hasselman

Berend Hasselman wrote
 
 .
 
 g - function(x){ 
 out - (2/(3))*exp(x^2)-(20)*log(x) 
 return(out) 
 } 
 
 gp - function(x) {
 out - (4/3)*exp(x) - 20/x
 return(out)
 }
 

function gp is incorrect. It should be

gp - function(x) {
out - (4/3)*x*exp(x^2) - 20/x
return(out)
}

Berend


--
View this message in context: 
http://r.789695.n4.nabble.com/NEWTON-ALGORITHM-tp4224558p4225321.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.