[Algorithm analysis] interpolation methods: Laplace interpolation and Newton Interpolation

Source: Internet
Author: User

Algorithm flow chart algorithm code [cpp] # include <iostream> # include <string> # include <vector> using namespace std; double Laplace (int N, vector <double> & X, vector <double> & Y, double x); int main () {char a = 'n '; do {cout <"Enter the value of the number of difference times n:" <endl; int N; cin> N; vector <double> X (N, 0 ); vector <double> Y (N, 0); cout <"Enter the value of the interpolation point and the function value (Xi, Yi):" <endl; for (int a = 0; a <N; a ++) {cin> X [a]> Y [a];} cout <"Enter the required value x:" <endl; double X; cin> x; double result = Laplace (N, X, Y, x); cout <"result obtained by using the Laplace interpolation method:" <result <endl; cout <"do you want to continue? (Y/n): "; cin> a;} while (a = 'y'); return 0;} double Laplace (int N, vector <double> & X, vector <double> & Y, double x) {double result = 0; for (int I = 0; I <N; I ++) {double temp = Y [I]; for (int j = 0; j <N; j ++) {if (I! = J) {temp = temp * (x-X [j]); temp = temp/(X [I]-X [j]);} result + = temp;} return result;}; Newton Interpolation Method the formula of Newton Interpolation method is as follows. For details, see (Baidu document) algorithm flow algorithm code [cpp] # include <iostream> # include <string> # include <vector> using namespace std; double ChaShang (int n, vector <double> & X, vector <double> & Y); double Newton (double x, vector <double> & X, vector <double> & Y); int main () {int n; cin> n; vector <double> X (n, 0); vector <double> Y (n, 0); for (int I = 0; I <n; I ++) {cin> X [I]> Y [I];} double x; cin> x; cout <Newton (x, X, Y);} double ChaShang (int n, vector <double> & X, vector <double> & Y) {double f = 0; double temp = 0; for (int I = 0; I <n + 1; I ++) {temp = Y [I]; for (int j = 0; j <n + 1; j ++) if (I! = J) temp/= (X [I]-X [j]); f + = temp;} return f;} double Newton (double x, vector <double> & X, vector <double> & Y) {double result = 0; for (int I = 0; I <X. size (); I ++) {double temp = 1; double f = ChaShang (I, X, Y); for (int j = 0; j <I; j ++) {temp = temp * (x-X [j]);} result + = f * temp;} return result ;} in the experiment, the original data of four points of a given function is recorded as follows: Use the Laplace interpolation to determine the function value at x = 2.101 and 4.234. Run the command to obtain the approximate values obtained by using the Newton Interpolation Formula. Results obtained by running the program: 2.26667 Experiment Analysis 1. Solve the problem of providing function evaluation for complex discrete data only by using the method of Laplace interpolation and Newton Interpolation, by simplifying the tested functions, the approximate function P (x) of the actual function f (x) of discrete data can be constructed to calculate the function value from unknown points, it is the basic idea of interpolation. 2. In fact, the Laplace interpolation method and the Newton Interpolation method are two types of deformation of the same method. The idea of constructing a fitting function is the same, the two actual problems in the experiment are calculated using the two algorithms and the results are the same. 3. The accuracy of the experiment results is not high. On the one hand, it is because the given data is relatively small, and on the other hand, it is mainly because the data type double in Win32 C ++ has only 7 digits, when a computer performs a floating-point operation, truncation may cause errors. In practice, measurement data may also cause errors. 4. accurate and efficient computer solutions are used to solve practical problems. Therefore, when solving the problem, we should not only construct an algorithm that can be solved, but more importantly, construct a reasonable algorithm that can be compiled into a computer to solve the problem. The algorithm optimization can not only save time, more accurate and valuable results.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.