C # version of the Laplace Interpolation Algorithm

Source: Internet
Author: User

In practice, sometimes we need to analyze the data. Recently we have encountered a situation where a series of abscissa are time, while the ordinate is the data of the recorded value, but the abscissa is not an offset record. that is to say, record once in the first minute, once in the second minute, and once in the fourth minute... unequal. the requirement is to calculate based on the current data to obtain the record value corresponding to the same time. That is to say, there is no record in the third minute, and the value in the third minute must be obtained through calculation.

I originally wanted to display the data in the chart control, and then obtain the Y value corresponding to each X coordinate from the chart control. However, it was found that this was not feasible, only the value of the recorded data point can be obtained, and the value of the new point cannot be obtained. interpolation can only be used for computation. The following is the method of the Laplace interpolation (C ):

/// <Summary> /// perform a quadratic Laplace interpolation based on discrete points /// http://www.cnblogs.com/technology/// </Summary> class Laplace {// <summary> /// X coordinate of each point public int [] X {Get; set ;}//< summary >/// array composed of Y coordinate values corresponding to each point of X /// </Summary> Public double [] y {Get; set ;} /// <summary> /// number of elements in array X or array y. Note that the number of elements in the two arrays must be the same /// </Summary> Public int itemnum {Get; set ;}//< summary> /// initialize the Laplace interpolation /// </Summary> /// <Param name = "X"> X Array composed of the coordinates of each point </param> /// <Param name = "Y"> array composed of the Y coordinate values corresponding to each point x </param> Public Laplace (INT [] x, double [] Y) {This. X = x; this. y = y; this. itemnum = x. length ;} /// <summary> /// obtain the Y coordinate value corresponding to a specific X coordinate /// </Summary> /// <Param name = "xvalue"> X coordinate value </ param> // <returns> </returns> Public double getvalue (INT xvalue) {// used for the start and end subscript int start and end of the tired multiplication array; // return value double value = 0.0; // if the initial discrete point is null, 0 if (itemnum <1) is returned) {return value;} // If The initial discrete point has only one, and returns the corresponding y value of the point if (itemnum = 1) {value = Y [0]; return value ;} // if there are only two initial discrete points, perform linear interpolation and return interpolation if (itemnum = 2) {value = (Y [0] * (xvalue-X [1])-y [1] * (xvalue-X [0]) /(x [0]-X [1]); return value;} // If the interpolation point is smaller than the X coordinate of the first point, interpolation of the first three points of the array if (xvalue <= x [1]) {start = 0; end = 2 ;} // If the interpolation point is greater than or equal to the X coordinate of the last vertex, take the last three vertices of the array for interpolation else if (xvalue> = x [itemnum-2]). {start = itemnum-3; end = itemnum-1;} // In some special cases, else {start = 1; end = itemnum; int temp; // use the bipartite method to determine which three points are selected for interpolation while (end-Start )! = 1) {temp = (start + end)/2; If (xvalue <X [temp-1]) End = temp; else start = temp;} start --; end --; // you can see which point of the interpolation point is closer to If (math. ABS (xvalue-X [start]) <math. ABS (xvalue-X [end]) start --; else end ++;} // you have determined which three points are used for interpolation, the first vertex is X [start] Double valuetemp; // attention is the quadratic interpolation formula for (INT I = start; I <= end; I ++) {valuetemp = 1.0; for (Int J = start; j <= end; j ++) if (J! = I) valuetemp * = (double) (xvalue-X [J])/(double) (X [I]-X [J]); value + = valuetemp * Y [I];} return value ;}

This solves the problem of getting values at the same time, but note that this is a quadratic interpolation, that is, fitting the functions below the quadratic and quadratic equations.

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.