I'm a student and I want to solve a textbook exercise which is implementing Newton-Raphson Algorithm in R Programming.

This is the exercise:

#Inputs:
s0 <- 2.36
E <- 2.36
r <- 0.01
t <- 1
c <- 0.1875

#Initial values
sigma <-0.10
sig <- rep(0,10)
sig[1] <- sigma

#Newton-Raphson method:
for(i in 2:100){
d1 <- (log(s0/E)+(r+sigma^2/2)*t)/(sigma*sqrt(t))
d2 <- d1-sigma*sqrt(t)
f <- s0*pnorm(d1)-E*exp(r*t)*pnorm(d2)-c

#Derivative of d1 w.r.t. sigma:
d11 <- (sigma^2*t*sqrt(t)-(log(s0/E)+(r+sigma^2/2)*t)*sqrt(t))/(sigma^2*t)

#Derivative of d2 w.r.t. sigma:
d22 <- d11-sqrt(t)

#Derivative of f(sigma):
f1 <- s0*dnorm(d1)*d11-E*exp(-r*t)*dnorm(d2)*d22

#Update sigma:
sigma <- sigma - f/f1
sig[i] <- sigma
if(abs(sig[i]-sig[i-1]) < 0.00000001){sig <- sig[1:i]; break}}sig

The result for sig is [1] 0.1000000 0.2140636 0.2117527 0.2117864 0.2117859 0.2117859

But the answer in my textbook is: [1] 0.1000000 0.1877024 0.1876218 0.1876218

Can some one tell me where I've gone wrong?

Jan 7, 2019 1,479 views

## 1 answer to this question.

The problem is that the expressions for f and f1 aren't matching. The term exp(r*t) in the expression for f should read exp(-r*t) if the expression for f1 is correct. Define f like this:

f <- s0*pnorm(d1)-E*exp(-r*t)*pnorm(d2)-c

When you change this, you'll get an output:

[1] 0.1000000 0.1877024 0.1876218 0.1876218

• 8,700 points

## Why should I adopt R programming

R Programming is the best mechanism for ...READ MORE

## Error: could not find function - R Programming

Yes, Just like @Maverik said, It happens ...READ MORE

+1 vote

## R programming Web Scraping

Try something like this: library(rvest) library(rvest) library(tidyverse) urls <- read_html("http://dk.farnell.com/c/office-computer-networking-products/prl/results/") pag <- ...READ MORE

## R programming: Flip coin simulation

Make use of the ggplot2 package. Start by installing and ...READ MORE

+1 vote

## How to segment documents into phrases in text mining using R?

You can do this in R using ...READ MORE

## R programming logic

Use gsub to match the substring that we want ...READ MORE