Bisection Method
This commit is contained in:
parent
9856710934
commit
38c65fa776
@ -1,4 +1,76 @@
|
||||
# Project 2 for the University of Tulsa's CS-7863 Sci-Stat Course
|
||||
# Roots and Zeros of Functions
|
||||
# Professor: Dr. McKinney, Spring 2023
|
||||
# Noah L. Schrick - 1492657
|
||||
# Noah L. Schrick - 1492657
|
||||
|
||||
## Part 1: Bisection
|
||||
findZeroBisect <- function(func, xl, xr, tol, maxsteps=1e6){
|
||||
steps <- 0
|
||||
if (func(xl) * func(xr) >= 0){
|
||||
stop("Bad interval: try again with different endpoints")
|
||||
}
|
||||
|
||||
repeat{
|
||||
xm <- (xl + xr)/2
|
||||
if(abs(func(xm)) < tol || steps >= maxsteps){
|
||||
break
|
||||
}
|
||||
|
||||
ifelse(func(xl) * func(xm) > 0, xl<-xm, xr<-xm)
|
||||
steps <- steps + 1
|
||||
}
|
||||
return(c(xm, func(xm), steps))
|
||||
}
|
||||
|
||||
# Solve 1a
|
||||
fun.a <- function(x) {x^2-2}
|
||||
fun.a.string <- "x^2-2=0"
|
||||
sqrt2.estimate <- findZeroBisect(fun.a.string,1,2,1e-6)
|
||||
sqrt2.estimate[1]
|
||||
# Plot 1a
|
||||
if (!require("ggplot2")) install.packages("ggplot2")
|
||||
library(ggplot2)
|
||||
ggplot(data.frame(x=seq(1,2,.1)), aes(x)) +
|
||||
stat_function(fun=fun.a, aes(col=fun.string)) +
|
||||
geom_hline(aes(yintercept=0, col = "y=0"), show.legend=TRUE)+
|
||||
geom_vline(aes(xintercept=sqrt2.estimate[1],
|
||||
col = "bisection estimate"), show.legend=TRUE)+
|
||||
ggtitle("Zero Function") +
|
||||
xlab("x") + ylab("Zero Function") +
|
||||
theme(text = element_text(size=20), plot.title = element_text(hjust = 0.5))
|
||||
|
||||
# Solve 1b
|
||||
fun.b <- function(x) {-3+x+2*exp(-x)}
|
||||
fun.b.string <- "-3+x+2*exp(-x)=0"
|
||||
fun.b.estimate.left <- findZeroBisect(fun.b,-2.5,0,1e-6)
|
||||
fun.b.estimate.left[1]
|
||||
fun.b.estimate.right <- findZeroBisect(fun.b,1,4,1e-6)
|
||||
fun.b.estimate.right[1]
|
||||
# Plot 1b
|
||||
ggplot(data.frame(x=seq(-2.5,5,.1)), aes(x)) +
|
||||
stat_function(fun=fun.b, aes(col=fun.b.string)) +
|
||||
geom_hline(aes(yintercept=0, col = "y=0"), show.legend=TRUE)+
|
||||
geom_vline(aes(xintercept=fun.b.estimate.left[1],
|
||||
col = "first bisection estimate"), show.legend=TRUE)+
|
||||
geom_vline(aes(xintercept=fun.b.estimate.right[1],
|
||||
col = "second bisection estimate"), show.legend=TRUE)+
|
||||
ggtitle("Zero Function") +
|
||||
xlab("x") + ylab("Zero Function") +
|
||||
theme(text = element_text(size=20), plot.title = element_text(hjust = 0.5))
|
||||
|
||||
# Solve 1c
|
||||
fun.c <- function(x) {x^3-sin(x)^2}
|
||||
fun.c.string <- "x^3-sin(x)^2=0"
|
||||
fun.c.estimate <- findZeroBisect(fun.c,0.5,1,1e-6)
|
||||
fun.c.estimate[1]
|
||||
# Plot 1c
|
||||
ggplot(data.frame(x=seq(0.5,1,.1)), aes(x)) +
|
||||
stat_function(fun=fun.c, aes(col=fun.c.string)) +
|
||||
geom_hline(aes(yintercept=0, col = "y=0"), show.legend=TRUE)+
|
||||
geom_vline(aes(xintercept=fun.c.estimate[1],
|
||||
col = "bisection estimate"), show.legend=TRUE)+
|
||||
ggtitle("Zero Function") +
|
||||
xlab("x") + ylab("Zero Function") +
|
||||
theme(text = element_text(size=20), plot.title = element_text(hjust = 0.5))
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user