Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

How to make a function that calculates newtons quotient for f(x) = exp(x) in R?

$
0
0

I'm working on an assignment for class and I'm a little lost with trying to write a function out which will calculate Newton's quotient. This is what the questions is asking

The derivative of a function f(x) can be approximated by the Newton's quotient (f(x+h) - f(x))/h

where h is a small number. Write a function to calculate the Newton's quotient for f(x) = exp(x). The function should take two scalar arguments, x and h. Use a default value of h=1e-6. Test your function at the point x=1 using the default value of h, and compare to the true value of the derivative f'(1) = e^1.

So far I have written the code as so

x=1
newton = function(x, h = 1e-06){
   quotiant = ((x+h) - x)/h

   return(x = exp(x))
}

y = newton(1,h)
print(y)

I can see this is wrong, but I don't really understand how I can fix this, and what exactly I'm trying to calculate.

I have also tried this code

x=1
newton = function(x, h = 1e-06){
   quotiant = ((x+h) - x)/h


}

y = newton(1,h)
print(y)

which I think gives me the right answer, but again I don't really understand what I'm calculating.


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>