Question

In: Computer Science

Use Lagrange interpolation to find the polynomial p3(x) of degree 3 or less, that agree with...

Use Lagrange interpolation to find the polynomial p3(x) of degree 3 or less, that agree with the following data: p3(−1) = 3, p3(0) = −4, p3(1) = 5, and p3(2) = −6.

Using python to solve

Solutions

Expert Solution

The Lagrange’s Interpolation formula:
If, y = f(x) takes the values y0, y1, … , yn corresponding to x = x0, x1 , … , xn then,

This method is preferred over its counterparts like Newton’s method because it is applicable even for unequally spaced values of x.

# Python3 program for implementation
# of Lagrange's Interpolation

# To represent a data point corresponding to x and y = f(x)
class Data:
   def __init__(self, x, y):
       self.x = x
       self.y = y

# function to interpolate the given data points
# using Lagrange's formula
# xi -> corresponds to the new data point
# whose value is to be obtained
# n -> represents the number of known data points
def interpolate(f: list, xi: int, n: int) -> float:

   # Initialize result
   result = 0.0
   for i in range(n):

       # Compute individual terms of above formula
       term = f[i].y
       for j in range(n):
           if j != i:
               term = term * (xi - f[j].x) / (f[i].x - f[j].x)

       # Add current term to result
       result += term

   return result

# Driver Code
if __name__ == "__main__":

   # creating an array of 4 known data points
   f = [Data(0, 2), Data(1, 3), Data(2, 12), Data(5, 147)]

   # Using the interpolate function to obtain a data point
   # corresponding to x=3
   print("Value of f(3) is :", interpolate(f, 3, 4))

Output:

Value of f(3) is : 35

Complexity:
The time complexity of the above solution is O(n2) and auxiliary space is O(1).


Related Solutions

Find the lagrange polynomials that approximate f(x) = x3 a ) Find the linear interpolation polynomial...
Find the lagrange polynomials that approximate f(x) = x3 a ) Find the linear interpolation polynomial P1(x) using the nodes x0= -1 and x1 = 0 b) Find the quadratic interpolation polynomial P2(x) using the nodes x0= -1 and x1 = 0 and x2 = 1 c) Find the cubic interpolation polynomial P3(x) using the nodes x0= -1 and x1 = 0 and x2 = 1 and x3=2 d) Find the linear interpolation polynomial P1(x) using the nodes x0= 1...
Q11: Use the Lagrange interpolating polynomial of degree three or less and four-digit chopping arithmetic to...
Q11: Use the Lagrange interpolating polynomial of degree three or less and four-digit chopping arithmetic to approximate cos 0.750 using the following values. Find an error bound for the approximation. cos 0.698 = 0.7661 ,cos 0.733 = 0.7432 cos 0.768 = 0.7193 cos 0.803 = 0.6946.
find Lagrange polynomials that approximate f(x)=x^3, a) find the linear interpolation p1(x) using the nodes X0=-1...
find Lagrange polynomials that approximate f(x)=x^3, a) find the linear interpolation p1(x) using the nodes X0=-1 and X1=0 b) find the quadratic interpolation polynomial p2(x) using the nodes x0=-1,x1=0, x2=1 c) find the cubic interpolation polynomials p3(x) using the nodes x0=-1, x1=0 , x2=1 and x3=2. d) find the linear interpolation polynomial p1(x) using the nodes x0=1 and x1=2 e) find the quadratic interpolation polynomial p2(x) using the nodes x0=0 ,x1=1 and x2=2
Find the Chebyshev interpolation nodes on the interval [4,12] for an interpolating polynomial of degree 5
Find the Chebyshev interpolation nodes on the interval [4,12] for an interpolating polynomial of degree 5
Let f(x) = x + 2/x a) Use quadratic Lagrange interpolation based on the nodes x0=1,...
Let f(x) = x + 2/x a) Use quadratic Lagrange interpolation based on the nodes x0=1, x1=2, and x2=2.5 to approximate f(1.5) and f(1.2) b) Use cubic Lagrange interpolation based on the nodes x0=0.5, x1=1, and x2=2 to approximate f(1.5) and f(1.2)
Make a program for LAGRANGE INTERPOLATION METHOD using C++ program and can be evaluated both polynomial...
Make a program for LAGRANGE INTERPOLATION METHOD using C++ program and can be evaluated both polynomial and Transcendental Functions.
Find a polynomial f(x) of degree 3 that has the indicated zeros and satisfies the given...
Find a polynomial f(x) of degree 3 that has the indicated zeros and satisfies the given condition. −5, 1, 2;    f(3) = 48 Find a polynomial f(x) of degree 3 that has the indicated zeros and satisfies the given condition. −3, −2, 0;    f(−4) = 24 Find a polynomial f(x) of degree 3 that has the indicated zeros and satisfies the given condition. −2i, 2i, 5;    f(1) = 40 Find the zeros of f(x), and state the multiplicity of each zero. (Order your...
Use the Lagrange interpolating polynomial to approximate √3 with the function f(x)= 3x-0.181and the values x0=-2,...
Use the Lagrange interpolating polynomial to approximate √3 with the function f(x)= 3x-0.181and the values x0=-2, X1=-1, X2=0, X3=1 and X4=2.(Uses 4 decimal figures)
Given: Polynomial P(x) of degree 6 Given: x=3 is a zero for the Polynomial above List...
Given: Polynomial P(x) of degree 6 Given: x=3 is a zero for the Polynomial above List all combinations of real and complex zeros, but do not consider multiplicity for the zeros.
Task 3: a) A second-degree polynomial in x is given by the expression = + +...
Task 3: a) A second-degree polynomial in x is given by the expression = + + , where a, b, and c are known numbers and a is not equal to 0. Write a C function named polyTwo (a,b,c,x) that computes and returns the value of a second-degree polynomial for any passed values of a, b, c, and x. b) Include the function written in part a in a working program. Make sure your function is called from main() and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT