# In f0nzie/rNodal: Oil and Gas Well Nodal Analysis for Production Petroleum Engineering

```func2 <- function(x) {
x^3 - 2* x - 5
}
```
```curve(func2, xlim=c(-5,5), col='blue', lwd=2, lty=2, ylab='f(x)')
abline(h=0)
abline(v=0)
```
```uniroot(func2, c(2,3))
```

## One more example

```func3 <- function(x) {
exp(2 * x) - x - 6
}
```
```curve(func3, col = 'blue', lty = 2, lwd = 2, xlim=c(-5,5), ylim=c(-5,5), ylab='f(x)')
abline(h=0)
abline(v=0)
```
```uniroot(func3, c(.5, 1.5))
```

## Summary

The Newton-Raphson method is a powerful and relatively straightforward method for finding the roots of an equation. It has many advantages but suffers from several drawbacks such as a readily calculated derivative, inconsistencies when f′(x)=0f′(x)=0 and so on. As explored in the post, it often makes sense first to plot the function in question to visualize how it behaves before attempting to locate the root(s) as bad starting values can be detrimental to results. Other numerical methods for estimating roots of equations such as the Bisection, Secant and Brent’s methods will be examined in future posts.

```func4 <- function(y) {
(y + y^2 + y^3 - y^4) / (1 - y)^3 - y^2
}
```
```curve(func4, col = 'blue', lty = 2, lwd = 2, xlim=c(-2,2), ylim=c(-5,5), ylab='f(x)')
abline(h=0)
abline(v=0)
```
```uniroot(func4, c(-0.2, 0.999))
```
```library(rootSolve)

# a well-behaved case...
fun <- function (x) cos(2*x)^3
curve(fun(x), 0, 10,main = "uniroot.all")
All <- uniroot.all(fun, c(0, 10))
points(All, y = rep(0, length(All)), pch = 16, cex = 2)
```
```## 2 simultaneous equations
library(rootSolve)
model <- function(x) c(
F1 = x[1]^2+ x[2]^2 -1,
F2 = x[1]^2- x[2]^2 +0.5)

(ss <- multiroot(f = model, start = c(1, 1)))
```
```## 3 equations, two solutions
model <- function(x) c(
F1 = x[1] + x[2] + x[3]^2 - 12,
F2 = x[1]^2 - x[2] + x[3] - 2,
F3 = 2 * x[1] - x[2]^2 + x[3] - 1 )

# first solution
(ss <- multiroot(model, c(1, 1, 1), useFortran = FALSE))
(ss <- multiroot(f = model, start = c(1, 1, 1)))
# second solution; use different start values
(ss <- multiroot(model, c(0, 0, 0)))
model(ss\$root)
```

f0nzie/rNodal documentation built on Oct. 30, 2017, 4:03 a.m.