1A.
#######################################################
## Lagrange Interpolation
#######################################################
Lagrange.Interpolation.Vector =function(
pred.x, # vector x for eval Pn()
fn = NULL, # input function or
yvec = NULL, # input y-coordinates
xvec # input x-coordinates
){
#
if(length(yvec) == 0) yvec = fn(xvec) #
n = length(xvec) # input x-coordinates
m = length(pred.x) # number of input x values
PV = rep(0, m)
for (k in 1:m){
Pn = 0
for (i in 1:n){
LP = 1
for (j in (1:n)[-i]){
LP = LP * (pred.x[k] - xvec[j])/(xvec[i] - xvec[j])
}
Pn = Pn + LP * yvec[i]
}
PV[k] = Pn
}
PV
}
library(pander)
## Warning: package 'pander' was built under R version 4.2.3
fn=function(x) cos(x)
approx.value = Lagrange.Interpolation.Vector(fn=fn, xvec=c(0, 0.6, 0.9, 0.45),
pred.x = c(0.6, 0.9, 0.45))
true.value = fn(c(0.6, 0.9, 0.45))
pander(rbind(true.value=true.value, approx.value = approx.value))
| true.value | 0.8253 | 0.6216 | 0.9004 |
|---|---|---|---|
| true value - approx | value = | 0 since t | hey are both the same in this case so the error is 0 |
| 1B. | |||
| ```r ################### ## Lagrange ################### Lagrange.Interpolat | ######### Interpola ######### ion.Vecto pred fn = yvec xvec ){ | ######### tion ######### r =functi .x, NULL, = NULL, | ################## |
| ```r library(pander) fn=function(x) (1+x approx.value = Lagr |
true.value 1.265 1.378 1.204
| approx.value | 1.265 | 1.378 | 1.204 |
|---|---|---|---|
| We still get 0 for | the erro | r since | the true and approximate values are equal. |
| 1C. |
#######################################################
## Lagrange Interpolation
#######################################################
Lagrange.Interpolation.Vector =function(
pred.x, # vector x for eval Pn()
fn = NULL, # input function or
yvec = NULL, # input y-coordinates
xvec # input x-coordinates
){
#
if(length(yvec) == 0) yvec = fn(xvec) #
n = length(xvec) # input x-coordinates
m = length(pred.x) # number of input x values
PV = rep(0, m)
for (k in 1:m){
Pn = 0
for (i in 1:n){
LP = 1
for (j in (1:n)[-i]){
LP = LP * (pred.x[k] - xvec[j])/(xvec[i] - xvec[j])
}
Pn = Pn + LP * yvec[i]
}
PV[k] = Pn
}
PV
}
library(pander)
fn=function(x) log(x+1)
approx.value = Lagrange.Interpolation.Vector(fn=fn, xvec=c(0, 0.6, 0.9, 0.45),
pred.x = c(0.6, 0.9, 0.45))
true.value = fn(c(0.6, 0.9, 0.45))
pander(rbind(true.value=true.value, approx.value = approx.value))
| true.value | 0.47 | 0.6419 | 0.3716 |
|---|---|---|---|
| Error is still 0 si 3A. | nce tru | e and app | rox are the same |
| Find third derivati We define g(x) as ( On interval [0,.9] We find the maximum | ve of f x-0)(x- sin(z)/ error | (x) which .6)(x-.9) 6 has max | sin(z)/ | is sin(x) now use theorem 3.3 to create the polynomial sin(z)/3! + (x-0)(x-.6)(x-.9) |
| 3B. | |||
| Find third derivati We determined the m The max error for | | ve of ( ax for (1/16(x | 1+x)^(1/2 g(x) to b +1)^(5/2) | ) which is 3/(8(x+1)^(5/2)) to create the polynomial (1/3!)(3/(8(x+1)^(5/2)))(x-0)(x-.6)(x-.9) which simplifies to (1/16(x+1)^(5/2))(x)(x-.6)(x-.9) e .057 from the previous problem. We find the max for (1/16(x+1)^(5/2)) on interval x [0,.9] which is 0.0625 )(x)(x-.6)(x-.9)| has to be less than or equal to (.057)(.0625) which is .0036 |
| 3C. | |||
| Find the third deri We know the max for This means the max 19. | vative g(x) f error o | of ln(x+1 rom the p f |(1/(3( | )which is 2/(x+1)^3 which is used to create the polynomial (1/3!)(2/(x+1)^3)(x)(x-.6)(x-.9) simplified to (1/(3(x+1)^3))(x)(x-.6)(x-.9) revious problems which is .057. We find the max of (1/(3(x+1)^3)) on interval [0,.9] which is 1/3 x+1)^3))(x)(x-.6)(x-.9)| has to be less than or equal to (.057)(1/3) which is .019 |
r Divided.Dif = funct vec.x, vec.y = NUL fn = NULL, pred.x ){ n = length(vec.x if (length(vec.y node.x = vec.x A = matrix(c(rep A[1,] = vec.y # for(i in 2:(n)){ for(j in 1:(n- denominator = numerator = A A[i,j] = nume } } A } |
ion( # L, # | ||
r library(pander) pander(Divided.Dif( vec.x = c(0 vec.y = c(6 fn = NULL ) ) |
6.67 17.33 42.67 37.33 30.1 29.31 28.74
1.777 6.335 -1.78 -1.807 -0.2633 -0.07125 0
0.4558 -1.159 -0.003929 0.2206 0.01746 0 0
-0.1242 0.105 0.02245 -0.01354 0 0 0
0.01349 -0.005899 -0.002 0 0 0 0
-0.0009693 0.0001772 0 0 0 0 0
| 4.095e-05 | 0 | 0 | 0 | 0 | 0 | 0 |
|---|---|---|---|---|---|---|
| The polynomia | l we get is | 6.67+1.777(x | -0)+.4558(x | -0)(x-6)-. | 1242(x-0)(x | -6)(x-10)+.01349(x-0)(x-6)(x-10)(x-13)-.0009693(x-0)(x-6)(x-10)(x-13)(x-17)+.00004095(x-0)(x-6)(x-10)(x-13)(x-17)(x-20) |
Divided.Dif = function(
vec.x, # input nodes:
vec.y = NULL, # one of vec.y and fn must be given
fn = NULL,
pred.x # scalar x for predicting pn(pred.x)
){
n = length(vec.x)
if (length(vec.y) == 0) vec.y = fn(vec.x) #
node.x = vec.x
A = matrix(c(rep(0,n^2)), nrow = n, ncol = n, byrow = TRUE)
A[1,] = vec.y # fill the first row with vec.y
#
for(i in 2:(n)){
for(j in 1:(n-i+1)){
denominator = vec.x[j] - vec.x[j+1+(i-2)]
numerator = A[i-1,j]- A[i-1,j+1]
A[i,j] = numerator/denominator
}
}
A
}
library(pander)
pander(Divided.Dif(
vec.x = c(0, 6, 10, 13, 17, 20, 28),
vec.y = c(6.67, 16.11, 18.89, 15, 10.56, 9.44, 8.89),
fn = NULL
)
)
| 6.67 | 16.11 | 18.89 | 15 | 10.56 | 9.44 | 8.89 |
|---|---|---|---|---|---|---|
| The polynomia 19B. For the For the secon | l is 6.67+1. first polyno d polynomial | 573(x-0)+-.08 mial when the when the pol | 783(x-0)(x- polynomial ynomial is | 6)-0.01513 is graphe graphed we | (x-0)(x-6)( d, we get t get the ma | x-10)+.002554(x-0)(x-6)(x-10)(x-13)-.0002007(x-0)(x-6)(x-10)(x-13)(x-17)+.000008362(x-0)(x-6)(x-10)(x-13)(x-17)(x-20) he max of 42.711 which occurs at x=10.189 x of 19.413 which occurs at x=8.769 |
| 3B | ||||||
r Divided.Dif = vec.x vec.y fn = pred. ){ n = length if (length node.x = v A = matrix A[1,] = ve # for(i in 2 for(j in denomin numerat A[i,j] } } A } |
function( , # = NULL, # NULL, x # | |||||
| ```r library(pande pred.0.25 = D | r) ivided.Dif(v vec.y pred. | |||||
| pander(cbind( ``` | pred.0.25=pr | ed.0.25)) |
-0.6205 -0.284 0.006601 0.2484
3.365 2.906 2.418 0
-2.296 -2.438 0 0
| -0.4732 | 0 | 0 | 0 |
|---|---|---|---|
| The polyno | mial beco | mes -.6205+ | 3.365(x)-2.296(x)(x-.1)-.4732(x)(x-.1)(x-.2) |
| Now plug i | n x=.25 t | o find f(.2 | 5) which is .1338 |
| 7A. |
Divided.Dif = function(
vec.x, # input nodes:
vec.y = NULL, # one of vec.y and fn must be given
fn = NULL,
pred.x # scalar x for predicting pn(pred.x)
){
n = length(vec.x)
if (length(vec.y) == 0) vec.y = fn(vec.x) #
node.x = vec.x
A = matrix(c(rep(0,n^2)), nrow = n, ncol = n, byrow = TRUE)
A[1,] = vec.y # fill the first row with vec.y
#
for(i in 2:(n)){
for(j in 1:(n-i+1)){
denominator = vec.x[j] - vec.x[j+1+(i-2)]
numerator = A[i-1,j]- A[i-1,j+1]
A[i,j] = numerator/denominator
}
}
A
}
library(pander)
pander(Divided.Dif(
vec.x = c(-0.1, 0, 0.2, 0.3),
vec.y = c(5.3, 2, 3.19, 1),
fn = NULL
)
)
| 5.3 | 2 | 3.19 | 1 |
|---|---|---|---|
| interpola 7B. | ting poly | nomial b | ecomes 5.3-33(x+0.1)+129.8(x+0.1)(x-0)-556.7(x+0.1)(x-0)(x-0.2) |
| ```r Divided.D v v f p | if = func ec.x, ec.y = NU n = NULL, red.x ){ ngth(vec. ngth(vec. = vec.x trix(c(re = vec.y | tion( # LL, # | |
r library(p pander(Di v v f ) ) |
ander) vided.Dif ec.x = c( ec.y = c( n = NULL |
5.3 2 3.19 1 0.9726
-33 5.95 -21.9 -0.548 0
129.8 -92.83 142.3 0 0
-556.7 671.9 0 0 0
| 2730 | 0 | 0 | 0 | 0 |
|---|---|---|---|---|
| interpola | ting poly | nomial i | s 5.3-33( | x+0.1)+129.8(x+0.1)(x-0)-556.7(x+0.1)(x-0)(x-0.2)+2730(x+0.1)(x-0)(x-0.2)(x) |