CoCalc Shared FilesMTH 505 / sec_3_1_lagrange.sagews
Author: Jonathan Mitchell
Views : 55
X = [0.6, 0.7, 0.8, 1]
f(x) = sin(e^(x) - 2)
Y = [0,0,0,0]
for j in(0..3):
Y[j] = round(f(X[j]),8)
L0(x) = (x-X[1])*(x-X[2])*(x-X[3])/((X[0] - X[1])*(X[0]-X[2])*(X[0] -X[3]))
L1(x) = (x-X[0])*(x-X[2])*(x-X[3])/((X[1] - X[0])*(X[1]-X[2])*(X[1] -X[3]))
L2(x) = (x-X[0])*(x-X[1])*(x-X[3])/((X[2] - X[0])*(X[2]-X[1])*(X[2] -X[3]))
L3(x) = (x-X[0])*(x-X[1])*(x-X[2])/((X[3] - X[0])*(X[3]-X[1])*(X[3] -X[2]))
P(x) = Y[0]*L0(x) + Y[1]*L1(x) + Y[2]*L2(x) + Y[3]*L3(x)
p = plot(P(x), 0.5,1.1,x,color = 'red')
q = plot(f(x),0.5,1.1,x,color = 'blue')
show(expand(P(x)))
show(p+q)


$\displaystyle -1.78574124999995 \, x^{3} + 4.70928062499987 \, x^{2} - 1.94720472499990 \, x - 0.318242680000020$
show(P(0.9))
show(f(0.9))
abserror = abs(P(0.9) - f(0.9))
relerror = abserror/abs(f(0.9))
show(abserror)
show(relerror)

$\displaystyle 0.441985002500000$
$\displaystyle 0.443592438770281$
$\displaystyle 0.00160743627028176$
$\displaystyle 0.00362367824559378$

f4(x) = diff(f(x),x,4)
show(f4(x))

$\displaystyle -6 \, \cos\left(e^{x} - 2\right) e^{\left(3 \, x\right)} + \cos\left(e^{x} - 2\right) e^{x} + e^{\left(4 \, x\right)} \sin\left(e^{x} - 2\right) - 7 \, e^{\left(2 \, x\right)} \sin\left(e^{x} - 2\right)$
P = plot(f4(x),0.5,1.1,x)
show(P)
round(f4(1),8)

-86.80037916
90*(0.9 - 0.6)*(0.9-0.7)*(0.9-0.8)*(0.9-1)/24

-0.00225000000000000