Polynomial that interpolate the function f at points using java
I have the values
x = 0 => y = 0
x = 1 => y = 1
x = 3 => y = 27
x = 4 => y = 64
I want to create a polynomial function using JAVA to create the function x^3. The program should create the function and display it, and if i give any values it should calculate the interpolated values. I have created a function which just produced the values using Aitken but it doesnt produce the function and it is really hard to understand how to do the fun开发者_StackOverflow社区ction. Because i dont know how to put the X value as X in the java program.
I have the same problem with you, and finally got the solution. Just look at Apache commons math API. You can use both Lagrange's or Newton's method to compute the coefficient of each polynomial degree.
You can download the .jar file of the API here, and the documentations here.
Suppose you have 4 pairs of (x,y) points. Consider the equation a0 + a1*x + a2*x^2 + a3*x^3 = y. Put the 4 pairs of (x,y) and you have 4 linear equations in 4 variables. Use a matrix solver or write your own to solve for (a0...a3). Now you can have a methode interpolate(double x) { return a0*x+a1*x*x+a2*x^2+a3*x^3; }. Hope that helps.
I have the values x = 0 => y = 0 x = 1 => y = 1 x = 2 => y = 27 x = 3 => y = 64
This is an odd way to list points.
Too bad that they aren't part of y = x^3. These are:
(x, y) = { (0,0), (1,1), (2, 8), (3, 27), (4, 64), (5, 125)...}
UPDATE:
I'd word your question differently. Your "example" set of points is incorrect and misleading. It sounds like you're really saying "I have an arbitrary set of points and I'd like to fit a function to them."
If you know the form of the funcion you need, the problem is merely about calculating the unknown coefficients. If you have as many points as coefficients you can solve for them (if a solution exists). If you have more points than coefficients you can do least squares fitting.
But all this depends on knowing what function you want beforehand.
Asking a computer to ferret out both the best form and the coefficient values for you is a daunting task.
You can certainly use Lagrange interpolation between points, but it still may not be the thing to tell you what the "best" function is to represent your points. It assumes polynomial forms, so mixing in other functions isn't part of the method. It could give you a very nice representation for sin(x), but it won't come out and tell you that a sine function would be easier to understand than a polynomial approximation.
精彩评论