\[ f(x) = a_0 + a_1x + \cdots + a_{n-1}x^{n-1} + a_n x^n \]
\[ \begin{align*} f(x) & \cong \sum_{k=0}^{n} \frac{f^{(k)}(a)}{k!} (x-a)^k \\ & = f(a) + f'(a)(x-a) + \frac{f''(a)}{2}(x-a)^2 + \cdots + \frac{f^{(n)}(a)}{n!}(x-a)^n \end{align*} \]
(f <- c(1,-2,3))
[1] 1 -2 3
f <- c(1,-2,3)
(deg <- length(f) - 1)
[1] 2
\[ f(x) \cong f(a) + f'(a)(x-a) + \frac{f''(a)}{2}(x-a)^2 \]
\[ \begin{align*} s & = 1 \\ s & = s + (-2)*2 = 1 - 4 = -3\\ s & = s + 3*(2*2) = -3 + 3*4 = -3 + 12 = 9\ \end{align*} \]
\[ \begin{align*} f(x) & = 1 - 2x + 3x^2 + 4*x^3 + 5*x^4 \\ & = 1 - 2*x \\ & \hspace{1.0in} + 3*x*x \\ & \hspace{2.0in} +4*x*x*x \\ & \hspace{3.0in} +5*x*x*x*x \\ \end{align*} \]
\[ \begin{align*} f(x) & = 1 - 2x + 3x^2 + 4*x^3 + 5*x^4 \\ & = 1 - 2*x \\ & \hspace{1.0in} + 3*x*x \\ & \hspace{2.0in} +4*x*x*x \\ & \hspace{3.0in} +5*x*x*x*x \\ \end{align*} \]
naivepoly <- function(x, coefs ) {
y <- rep(0, length(x))
for(i in 1: length(coefs)) {
y <- y + coefs[i] * (x ^ (i - 1))
}
return (y)
}
rep(0,3)
[1] 0 0 0
x <- 2
f <- c(1,-2,3)
naivepoly(x,f)
[1] 9
naivepoly <- function(x, coefs ) {
y <- rep(0, length(x))
for(i in 1: length(coefs)) {
y <- y + coefs[i] * (x ^ (i - 1)) }
return (y) }
x <- c(0, 1, 2)
naivepoly(x,f)
[1] 1 2 9
naivepoly <- function(x, coefs ) {
y <- rep(0, length(x))
for(i in 1: length(coefs)) {
y <- y + coefs[i] * (x ^ (i - 1)) }
return (y) }
\[ \begin{align*} f(x) & = 1 - 2x + 3x^2 + 4*x^3 + 5*x^4 \\ & = 1 - 2*x \\ & \hspace{1.0in} + 3*\overbrace{x*x} \\ & \hspace{2.0in} + 4*\overbrace{(x^2)*x} \\ & \hspace{3.0in} +5*(x^3)*x \\ \end{align*} \]
\[ \begin{align*} f(x) & = 1 - 2x + 3x^2 + 4*x^3 + 5*x^4 \\ & = 1 - 2*x \\ & \hspace{1.0in} + 3*\overbrace{x*x} \\ & \hspace{2.0in} + 4*\overbrace{(x^2)*x} \\ & \hspace{3.0in} +5*(x^3)*x \\ \end{align*} \]
betterpoly <- function(x, coefs ) {
y <- rep(0, length(x))
cached.x <- 1
for(i in 1: length(coefs)) {
y <- y + coefs[i]*cached.x
cached.x <- cached.x * x
}
return (y)
}
naivepoly <- function(x, coefs ) {
y <- rep(0, length(x))
for(i in 1: length(coefs)) {
y <- y + coefs[i] * (x ^ (i - 1)) }
return (y) }
betterpoly <- function(x, coefs ) {
y <- rep(0, length(x))
cached.x <- 1
for(i in 1: length(coefs)) {
y <- y + coefs[i]*cached.x
cached.x <- cached.x * x}
return (y)}
\[ \begin{align*} f(x) & = 1 - 2x + 3x^2 \\ & = 1 + x(-2 + 3x) \\ \\ f(x) & = 1 - 2x + 3x^2 +4x^3 \\ & = 1 + x(-2 + x(3 + 4x)) \\ \\ f(x) & = 1 - 2x + 3x^2 +4x^3 +5x^4 \\ & = 1 + x(-2 + x(3 + x(4 + 5x))) \end{align*} \]
\[ \begin{align*} f(x) & = 1 - 2x + 3x^2 +4x^3 +5x^4 \\ & = 1 + x*(-2 + x*(3 + x*(4 + x*5)) \end{align*} \]
\[ \begin{align*} f(x) & = 1 - 2x + 3x^2 +4x^3 +5x^4 \\ & = 1 + x*(-2 + x*(3 + x*(4 + x*5)) \end{align*} \]
\[ f(x) = 1 + x*(-2 + x*(3 + x*(4 + x*5)) \]
\[ \begin{align*} s & = 5 \\ s & = 4 + x*s \\ s & = 3 + x*s \\ s & = -2 + x*s \\ s & = 1 + x*s \\ \end{align*} \]
\[ \begin{align*} f(x) & = 1 - 2x + 3x^2 +4x^3 +5x^4 \\ & = 1 + x(-2 + x(3 + x(4 + x5))) \end{align*} \]
horner <- function(x, coefs) {
y <- rep(0, length (x))
for(i in length(coefs):1)
y <- coefs [i] + x * y
return(y)
}