Question

In: Computer Science

Write a function forward_eval that evaluates the interpolating polynomial using Newton’s Forward Difference table. The function...

Write a function forward_eval that evaluates the interpolating polynomial using Newton’s Forward Difference table. The function prototype and descriptive comments have been provided:

function y = forward_eval(X, T, x)

%FORWARD_EVAL Evaluate Newton's forward difference form of the

%interpolating polynomial

% y = FORWARD_EVAL(X, T, x) returns y = Pn(x), where Pn is the

% interpolating polynomial constructed using the abscissas X and % forward difference table T.

Solutions

Expert Solution

#include <bits/stdc++.h>

using namespace std;

  

// calculating u mentioned in the formula

float u_cal(float u, int n)

{

    float temp = u;

    for (int i = 1; i < n; i++)

        temp = temp * (u - i);

    return temp;

}

  

// calculating factorial of given number n

int fact(int n)

{

    int f = 1;

    for (int i = 2; i <= n; i++)

        f *= i;

    return f;

}

  

int main()

{

    // Number of values given

    int n = 4;

    float x[] = { 45, 50, 55, 60 };

      

    // y[][] is used for difference table

    // with y[][0] used for input

    float y[n][n];

    y[0][0] = 0.7071;

    y[1][0] = 0.7660;

    y[2][0] = 0.8192;

    y[3][0] = 0.8660;

  

    // Calculating the forward difference

    // table

    for (int i = 1; i < n; i++) {

        for (int j = 0; j < n - i; j++)

            y[j][i] = y[j + 1][i - 1] - y[j][i - 1];

    }

  

    // Displaying the forward difference table

    for (int i = 0; i < n; i++) {

        cout << setw(4) << x[i]

             << "\t";

        for (int j = 0; j < n - i; j++)

            cout << setw(4) << y[i][j]

                 << "\t";

        cout << endl;

    }

  

    // Value to interpolate at

    float value = 52;

  

    // initializing u and sum

    float sum = y[0][0];

    float u = (value - x[0]) / (x[1] - x[0]);

    for (int i = 1; i < n; i++) {

        sum = sum + (u_cal(u, i) * y[0][i]) /

                                 fact(i);

    }

  

    cout << "\n Value at " << value << " is "

         << sum << endl;

    return 0;

}


Related Solutions

Consider polynomial interpolation of the function f(x)=1/(1+25x^2) on the interval [-1,1] by (1) an interpolating polynomial...
Consider polynomial interpolation of the function f(x)=1/(1+25x^2) on the interval [-1,1] by (1) an interpolating polynomial determined by m equidistant interpolation points, (2) an interpolating polynomial determined by interpolation at the m zeros of the Chebyshev polynomial T_m(x), and (3) by interpolating by cubic splines instead of by a polynomial. Estimate the approximation error by evaluation max_i |f(z_i)-p(z_i)| for many points z_i on [-1,1]. For instance, you could use 10m points z_i. The cubic spline interpolant can be determined in...
Write a code to approximate the derivative of a function f(x) using forward finite difference quotient...
Write a code to approximate the derivative of a function f(x) using forward finite difference quotient f( x + h ) - f( x ) f'(x) ≈ ------------------- (for small h). h For the function f(x) = sin(x), at x=1 , compute the FD quotients for h = 1/2k, k=5,...,N, with N=30 and compare with the exact derivative cos(x). Output k , h , error. Where SHOULD the error tend as h → 0 ? 1. Look at the numbers....
Using C++ 1) Write a program that repeatedly evaluates a n-th order polynomial p(x) = a0...
Using C++ 1) Write a program that repeatedly evaluates a n-th order polynomial p(x) = a0 + a1*x + a2*x^2 + ... + an*x^n where n <= 10 The program inputs n, ai, and x from the keyboard and then prints out the corresponding value of p to the screen. The program continues to input new values of n, ai, x and evaluates p until a negative value for n is input, at which point the program stops.
USING C++ 1) Write a program that repeatedly evaluates a n-th order polynomial p(x) = a0...
USING C++ 1) Write a program that repeatedly evaluates a n-th order polynomial p(x) = a0 + a1*x + a2*x^2 + ... + an*x^n where n <= 10 The program inputs n, ai, and x from the keyboard and then prints out the corresponding value of p to the screen. The program continues to input new values of n, ai, x and evaluates p until a negative value for n is input, at which point the program stops. 2)Write a...
Use Newton's forward and backward difference formula to construct interpolating polynomials of degree 1,2, and 3...
Use Newton's forward and backward difference formula to construct interpolating polynomials of degree 1,2, and 3 from the following data and approximate the function at the specified value using these polynomials: f(0.43) if f(0) =1 f(0.25) = 1.6487, f(0.5) = 2.7182, and f(0.75) = 4.4819
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)
Write a MATLAB function, called arbpoly, that computes a polynomial arbitrary nth degree. The function will...
Write a MATLAB function, called arbpoly, that computes a polynomial arbitrary nth degree. The function will take 2 inputs: 1) the first input will be a row vector, c, containing the coefficients of the polynomial, starting with the coefficient of the highest - degree term; 2) the second input will be a scalar, x, which is a real number at which the polynomial will be evaluated. The function's only output, y, will be the scalar value of the polynomial computed...
For the following exercises, use the graphs to write a polynomial function of least degree.
For the following exercises, use the graphs to write a polynomial function of least degree.
Write a Racket function "combine" that takes two functions, f and g, as parameters and evaluates...
Write a Racket function "combine" that takes two functions, f and g, as parameters and evaluates to a new function. Both f and g will be functions that take one parameter and evaluate to some result. The returned function should be the composition of the two functions with f applied first and g applied to f's result. For example (combine add1 sub1) should evaluate to a function equivalent to (define (h x) (sub1 (add1 x))). You will need to use...
Write one a MATLAB function that implements the Bisection method, Newton’s method and Secant Method (all...
Write one a MATLAB function that implements the Bisection method, Newton’s method and Secant Method (all in one function). Your function must have the following signature function output = solve(f,options) % your code here end where the input is • f: the function in f(x) =0. options: is a struct type with the following fields o method: bisection, newton or secant tol: the tolerance for stopping the iterations. maximum_iterations: the maximum number of iterations allowed. initial_guess: that is P_0; if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT