Question

In: Computer Science

Write a computer program using C++ that computes the value of a Lagrange Polynomial and accepts,...

Write a computer program using C++ that computes the value of a Lagrange Polynomial and accepts, as input: (n+1) data points (x0, f(x0)), (x1, f(x1)),...(xn, f(xn)).. at value x, which the polynomial is to evaluated. As output, the program should produce a statement that reads, "f(x) = (the value of f(x))". As a test, use the data values (1,2);(-1,1);(0,0);(2,4);(-2,3)

Solutions

Expert Solution

// C++ program that computes the value of a Lagrange Polynomial

#include<bits/stdc++.h>
using namespace std;
int N; //global variable for array size
//interpolation function
// x1 is the new data point whose value is to be evaluated
// n is the no. of known data points
double interpolate(int f[][2], int x1, int n)
{
   double result = 0; // Initialize the result with 0

   for (int i=0; i<n; i++)
   {
       // Compute the terms individually
       double term = f[i][1];
       for (int k=0;k<n;k++)
       {
           if (k!=i)
               term = term*(x1 - f[k][0])/double(f[i][0] - f[k][0]);
       }

       // cumulate terms to find the result
       result += term;
   }

   return result;
}

// main function
int main()
{
   cout<<"Enter size of the input array:";
   cin>>N;
   int f[N][2];
   cout<<"\nEnter data points(as{x,f(x)}):";
   for(int i=0;i<N;i++)
   cin>>f[i][0]>>f[i][1];
cout<<"\nEnter the value of x: ";
int x;
cin>>x;
   cout << "\nValue of f("<<x<<") is : " << interpolate(f, x, N);

/*call our interpolate function with array,value to be calculated and number of known values for interpolation*/
return 0;
}

OUTPUT:

Enter size of the input array:5
Enter data points(as{x,f(x)}):

1 2

-1 1

0 0

2 4

-2 3
Enter the value of x: 2
Value of f(2) is : 4


Related Solutions

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.
Write a C++ program that accepts a single integer value entered by user. If the value...
Write a C++ program that accepts a single integer value entered by user. If the value entered is less than one the program prints nothing. If the user enters a positive integer n. The program prints n x n box drawn with * characters. If the user enters 1 , for example the program prints *. If the user enter a 2, it prints ** ** that is , a 2x2 box of * symbols.
Using the MARIE computer assembly language, write a program that computes the following expression: z =...
Using the MARIE computer assembly language, write a program that computes the following expression: z = a * b * c. The computer will read in the input values a, b, and c from the keyboard and the final result (z) have to be displayed. In addition, every time an input value is read in, it must be displayed on the screen. Remember that the instruction set does not have an instruction to execute multiplication. Note: If any of the...
Using C Language Write a program segment that computes 1 + 2 + 3 + ......
Using C Language Write a program segment that computes 1 + 2 + 3 + ... + ( n - 1) + n , where n is a data value. Follow the loop body with an if statement that compares this value to (n * (n + 1)) / 2 and displays a message that indicates whether the values are the same or different. Please give me code to just copy and paste
Write a defining table and a computer program that computes and outputs the volume of a...
Write a defining table and a computer program that computes and outputs the volume of a torus with inner radius a and outer radius b. A doughnut is an example of a torus. Your program must read the inner radius and outer radius from two text fields and display the volume in a div. The formula for the volume of a torus is v =  π2(a + b)(a - b)2 4 where v is the volume, π is the constant pi,...
C++ program Overloaded Hospital Write a c++ program that computes and displays the charges for a...
C++ program Overloaded Hospital Write a c++ program that computes and displays the charges for a patient’s hospital stay. First, the program should ask if the patient was admitted as an inpatient or an outpatient. If the patient was an inpatient, the following data should be entered: The number of days spent in the hospital The daily rate Hospital medication charges Charges for hospital services (lab tests, etc.) The program should ask for the following data if the patient was...
Write a C++ program (using the pthread library) that accepts a phrase of unspecified length on...
Write a C++ program (using the pthread library) that accepts a phrase of unspecified length on the command line. For example: prompt$: ./vowcon Operating Systems Class at CSUN The main() in this program should read the phrase from the terminal. This phrase can be read into a global variable. This phrase or its parts can be directly accessed from the main() and from the threads. The main() has to create two threads running functions (vow and con). The main() can...
Write a C program without using if-else construct that does the following. It accepts a sequence...
Write a C program without using if-else construct that does the following. It accepts a sequence of positive integers between 1 and 9 both inclusive from the keyboard. The program will stop accepting input once an integer outside the range is entered. The program will finish by printing the total number multiples of 3 and total number of even integers entered. Test data and expected output: Enter integers between 1 & 9 both inclusive, outside range to stop Enter integer...
Write a program in C that computes the area of a circle (Area = pi *...
Write a program in C that computes the area of a circle (Area = pi * r2) and the volume of a sphere (Volume = 4/3 * pi * r3). Both formulas use r which is the radius. Declare a float variable pi = 3.14159. Get the value of r from the keyboard and store it in a float variable. Display both the area of the circle and the volume of the sphere.
#C. Write a program that accepts any number of homework scores ranging in value from 0...
#C. Write a program that accepts any number of homework scores ranging in value from 0 through 10. Prompt the user for a new score if they enter a value outside of the specified range. Prompt the user for a new value if they enter an alphabetic character. Store the values in an array. Calculate the average excluding the lowest and highest scores. Display the average as well as the highest and lowest scores that were discarded.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT