I have been trying to solve a problem on Lagrange Interpolation from the book Numerical Analysis 10th Edition by Richard Burden. I have been stuck on the first question it for hours and cannot figure it out.
The question is:
For the given functions $f(x)$ let $x_0 = 0, x_1= 0.6, x_2 = 0.9$ Construct interpolation polynomials of degree at most one and at most two to approximate $f(0.45)$
$(a) f(x) = \cos x$
The answer I get for two approximate is:
$6.79012 x^2 – 7.40741x + 1.5$
The answer in the book is: $-0.452592x^2 – 0.0131009x +1$
I tried this in python using scipy interpolate Library's Lagrange function and the answer it gave was: $-0.4311x^2 – 0.03246x + 1$
May I ask if anyone can provide correct working of this question.
My steps:
Step 1 Calculate $L_0, L_1, L_2$
$L_0 = (1/0.36)*(x-0.6)*(x-0.9), L_1 = (1/0.36)*x*(x-0.9), L_2 =(1/0.81)*x*(x-0.6)$
Step 2:
$L_0+L_1+L_2$
Solving this I get = $6.79012 x^2 – 7.40741x + 1.5$
Best Answer
It is difficult to say what's the problem without seeing what you did exactly, but here are some tips
Make sure you're using radians, for the looks of it, that may be the problem
Your solution must pass for all the input points, at least $f(0) = \cos(0) = 1$
Here's just a plot showing your results, just to help you confirm the solution in the book is actually correct
This is the full procedure
\begin{eqnarray} L(x) &=& \cos(0) \frac{(x - 0.6)(x - 0.9)}{(0 - 0.6)(0 - 0.9)} + \cos(0.6)\frac{(x - 0)(x - 0.9)}{(0.6 - 0)(0.6 - 0.9)} + \cos(0.9) \frac{(x - 0)(x - 6)}{(0.9 - 0)(0.9 - 0.6)} \\ &=& \frac{\cos(0)}{0.54}(x - 0.6)(x - 0.9) - \frac{\cos(0.6)}{0.18}x(x - 0.9) + \frac{\cos(0.9)}{0.27} x (x - 0.6) \\ &=& 1.85185 (x - 0.6)(x - 0.9) - 4.5852x(x - 0.9) + 2.30226 x(x - 0.6) \\ &=& -0.431087 x^2 - 0.0324552 x + 1 \end{eqnarray}