Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** Interpolacja Lagrange'a
- * Michał Ładanowski (c) 2011
- * www.algorytm.org
- */
- package com.company.zpo;
- public class Main {
- public static void main(String[] args) {
- // węzły interpolacji oraz odpowiadające im wartości
- // ( jak nie trudno się domyślić funkcja to x^2)
- double[] x = {1,2,3,4};
- double[]y = {9,45,141,327};
- //chcemy znaleźć wartość w x = 2
- double y2 = lagrangeInterpolation(x,y,5);
- 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++){
- t = 1.0;
- for(int j = 0; j < xs.length ; j++){
- if(j != k ){
- t=t*((x-xs[j])/(xs[k]-xs[j]));
- }
- }
- y += t*ys[k];
- }
- return y;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement