Adding central difference approx
Adding plot title
This commit is contained in:
parent
aa38089910
commit
2f9163eec2
@ -5,7 +5,7 @@
|
||||
|
||||
## 1. Approximate deriv. of sin(x) at x=pi from h=10^-1 -> 10^-20
|
||||
forward.approx <- function(func, x, h){
|
||||
approx <- (func(x+h) - func(x))/h
|
||||
approx <- (func(x+h)-func(x))/h
|
||||
}
|
||||
|
||||
forward.approx.table <- matrix(nrow = 0, ncol = 3)
|
||||
@ -17,9 +17,25 @@ for(h in 10^(seq(-1,-20,-1))){
|
||||
}
|
||||
|
||||
plot(abs(log(forward.approx.table[,"h"],10)), forward.approx.table[,"error"],
|
||||
xlab="h [10^-x]", ylab="error (logscale)", type="o", log="y")
|
||||
xlab="h [1e-x]", ylab="error (logscale)", type="o", log="y",
|
||||
main = "Forward Approximation of sin(x) at x=pi")
|
||||
|
||||
|
||||
# Repeat with central difference approx
|
||||
central.diff.approx <- function(func, x, h){
|
||||
approx <- (func(x+h)-func(x-h))/(2*h)
|
||||
}
|
||||
central.diff.table <- matrix(nrow = 0, ncol = 3)
|
||||
colnames(central.diff.table) <- c("h", "approximation", "error")
|
||||
for(h in 10^(seq(-1,-20,-1))){
|
||||
approx <- central.diff.approx(sin, pi, h)
|
||||
error <- abs(cos(pi)-approx)
|
||||
central.diff.table <- rbind(central.diff.table, c(h, approx, error))
|
||||
}
|
||||
|
||||
plot(abs(log(central.diff.table[,"h"],10)), central.diff.table[,"error"],
|
||||
xlab="h [1e-x]", ylab="error (logscale)", type="o", log="y",
|
||||
main = "Central Difference Approximation of sin(x) at x=pi")
|
||||
|
||||
## 2.
|
||||
# a) Runge-Kutta
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user