Sign up with your email address to be the first to know about new products, VIP offers, blog features & more.

 




C Program implementing Lagrange Interpolation Formula

In numerical analysis, Lagrange polynomials are used for polynomial interpolation. For a given set of distinct points Xi and numbers Yi, the Lagrange polynomial is the polynomial of the least degree that at each point Xj assumes the corresponding value Yj (i.e. the functions coincide at each point). The interpolating polynomial of the least degree is unique, however, and it is therefore more appropriate to speak of “the Lagrange form” of that unique polynomial rather than “the Lagrange interpolation polynomial”, since the same polynomial can be arrived at through multiple methods.

The Lagrange interpolating polynomial is the polynomial P(X) of degree <=(n – 1) that passes through the n points (x1, y1 = f(x1)),(x2, y2 = f(x2)) ,.., (xn, yn = f(xn)), , and is given by

 P(x)=sum_(j=1)^nP_j(x),
(1)

where

 P_j(x)=y_jproduct_(k=1; k!=j)^n(x-x_k)/(x_j-x_k).
(2)

Written explicitly,

((x-x_2)(x-x_3)...(x-x_n))/((x_1-x_2)(x_1-x_3)...(x_1-x_n))y_1+((x-x_1)(x-x_3)...(x-x_n))/((x_2-x_1)(x_2-x_3)...(x_2-x_n))y_2+...+((x-x_1)(x-x_2)...(x-x_(n-1)))/((x_n-x_1)(x_n-x_2)...(x_n-x_(n-1)))y_n.
(3)

The formula was first published by Waring (1779), rediscovered by Euler in 1783, and published by Lagrange in 1795 (Jeffreys and Jeffreys 1988).

Objective: To write a program in C to find the functional value of any value entered by user( using Lagrange’s Interpolation)

Algorithm for Lagrange’s Interpolation
  1. Scan for the number of data available. (data)
  2. Scan value for which f(x) – (datay[]) is to be calculated
  3. loop for i=0 to number_of_data
    scan datax[i], scan datay[i], next i
  4. loop for i to number_of_data
    factor[i] = 1.0
    loop for j to number_of_data
    if i != j
    factor[i] = factor[i] * (value – datax[j])/(datax[i]-datax[j])
    end if, next j, next i
  5. loop for i to number_of_data
    sum = sum + factor[i] * datay[i] next i
  6. print the results
  7. Stop
C Program implementing Lagrange Interpolation Formula
Formula source: Lagrange Interpolating Polynomial

Thank you for your love.
share
1 Response

What do you think?

Your email address will not be published. Required fields are marked *

%d bloggers like this: