Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MexicanWave {
- public static void main(String[] args) {
- // ( jak nie trudno się domyślić funkcja to x^2)
- double[] x = {-2,1,2,4 };
- double[]y = {3, 1, -3, 8};
- //chcemy znaleźć wartość w x = 2
- double y2 = lagrangeInterpolation(x,y,0);
- System.out.println("x = 2 -> y = "+y2);
- }
- public static double lagrangeInterpolation(double[] xs, double[] ys, double x ){
- double t;
- double y = 0.0;
- for(int k = 0; k< xs.length; k++){
- StringBuilder builder = new StringBuilder();
- StringBuilder builder1 = new StringBuilder();
- builder.append(k+1).append(": ");
- t = 1.0;
- for(int j = 0; j < xs.length ; j++){
- if(j != k ){
- builder.append(String.format("(x-%.0f)/(%.0f-%.0f)*", xs[j], xs[k], xs[j]));
- builder1.append(String.format("(x-x%d)/(x%d-x%d)*", (j+1), (k+1), (j+1)));
- t = t * ((x-xs[j])/(xs[k]-xs[j]));
- }
- }
- System.out.println(builder.toString());
- System.out.println(builder1.toString());
- System.out.println();
- y += t*ys[k];
- }
- return y;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement