# Chapter-18 : Interpolation¶

## Ex:18.1 Pg: 490¶

In [1]:
from math import log
#f1(x) = f0(x) +(f(x1) - f(x0) *(x - x0)/ (x1 - x0)
x = 2#
x0 = 1#
x1 = 6#
m = 1.791759#
n = 0#
r = log(2)#
f = 0 + (m - n) * (x - x0) / (x1 - x0)#
print "value of ln2 for interpolation region 1 to 6 =",f
e = (r - f) * 100/r#
print "error by interpolation for interval[1,6] =",e,"%"
x2 = 4#
p = 1.386294#
f = 0 + (p - n) * (x - x0) / (x2 - x0)#
print "value of ln2 for interpolation region 1 to 6 =",f
e = (r - f) * 100/r#
print "error by interpolation for interval[1,6] =",e,"%"

value of ln2 for interpolation region 1 to 6 = 0.3583518
error by interpolation for interval[1,6] = 48.3007635246 %
value of ln2 for interpolation region 1 to 6 = 0.462098
error by interpolation for interval[1,6] = 33.3333506995 %


## Ex:18.2 Pg: 492¶

In [2]:
from math import log
x = 2#
x0 = 1#
m = 0#
x1 = 4#
n = 1.386294#
x2 = 6#
p = 1.791759#
b0 = m#
b1 = (n - m)/(x1 - x0)#
b2 = ((p - n)/(x2 - x1) - (n - m)/(x1 - x0))/(x2 - x0)#
print "b0 = ",b0
print "b1 = ",b1
print "b2 = ",b2
f = b0 + b1*(x - x0) + b2*(x - x0)*(x - x1)#
print "f(2) = ",f
r = log(2)#
e = (r -f)*100/r#
print "error = ",e,"%"

b0 =  0
b1 =  0.462098
b2 =  -0.0518731
f(2) =  0.5658442
error =  18.3659378744 %


## Ex:18.3 Pg: 494¶

In [3]:
from math import log
x = 2#
x0 = 1#
m = 0#
x1 = 4#
n = 1.386294#
x3 = 5#
p = 1.609438#
x2 = 6#
o = 1.791759#
f01 = (m - n)/(x0 - x1)#
f12 = (n - o)/(x1 - x2)#
f23 = (p - o)/(x3 - x2)#
f210 =  (f12 - f01)/(x2 - x0)#
f321 = (f23 - f12)/(x3 - x1)#
f0123 = (f321 - f210) / (x3 - x0)#
b0 = m#
b1 = f01#
b2 = f210#
b3 = f0123#
print "b0 = ",b0
print "b1 = ",b1
print "b2 = ",b2
print "b3 = ",b3
f = b0 + b1*(x - x0) + b2*(x - x0)*(x - x1) + b3*(x - x0)*(x - x1)*(x - x2)#
print "f(2) = ",f
r = log(2)#
e = (r -f)*100/r#
print "error = ",e,"%"

b0 =  0
b1 =  0.462098
b2 =  -0.0518731
b3 =  0.0078654
f(2) =  0.6287674
error =  9.28803901474 %


## Ex:18.4 Pg: 496¶

In [5]:
x = 2#
x0 = 1#
m = 0#
x1 = 4#
n = 1.386294#
x3 = 5#
p = 1.609438#
x2 = 6#
o = 1.791759#
f01 = (m - n)/(x0 - x1)#
f12 = (n - o)/(x1 - x2)#
f23 = (p - o)/(x3 - x2)#
f210 =  (f12 - f01)/(x2 - x0)#
f321 = (f23 - f12)/(x3 - x1)#
f0123 = (f321 - f210) / (x3 - x0)#
b0 = m#
b1 = f01#
b2 = f210#
b3 = f0123#
R2 = b3 * (x - x0) * (x - x1)*(x-x2)#
print "error R2 = ",R2

error R2 =  0.0629232


## Ex:18.6 Pg: 501¶

In [6]:
x = 2#
x0 = 1#
m = 0#
x1 = 4#
n = 1.386294#
x2 = 6#
p = 1.791759#
f1 = (x - x1)*m/((x0 - x)) + (x- x0) * n/(x1 - x0)#
print "first order polynomial f1(2) = ",f1
f2 = (x - x1)*(x - x2)*m/((x0 - x1)*(x0 - x2)) + (x - x0)*(x - x2)*n/((x1-x0)*(x1-x2)) + (x - x0)*(x - x1)*p/((x2 - x0)*(x2 - x1))#
print "second order polynomial f2(2) = ",f2

first order polynomial f1(2) =  0.462098
second order polynomial f2(2) =  0.5658442