Anda di halaman 1dari 2

import java.util.Scanner; import java.text.

*; public class InterpolasiLagrange{ static int N; static double ubah (double angka){ DecimalFormat lima = new DecimalFormat("#.#####"); angka=Double.valueOf(lima.format(angka)); return angka; } public static void main (String[] args) { Scanner in = new Scanner (System.in); double X,E; System.out.println("********* .: POLINOM LAGRANGE :. *********\n "); //masukkan banyak System.out.print("Masukkan Banyak x atau f(x): "); N=in.nextInt(); //pembuatan Array untuk penyimpanan double x[]=new double [N]; double fx[]=new double [N]; double y[]=new double [N]; //inisialisasi x -- f(x) -- X untuk nilai fungsi -- ? for(int i=0;i<N;i++){ System.out.print("Nilai x"+i+" = "); x[i]=in.nextDouble(); } for(int i=0;i<N;i++){ System.out.print("Nilai f(x"+i+") = "); fx[i]=in.nextDouble(); } // System.out.print("Menentukan nilai fungsi di X = "); X=in.nextDouble(); //kayaknya gag perlu ini //System.out.print("Dengan ?= "); //E=in.nextDouble(); System.out.println(""); System.out.println(""); //proses perhitungan for(int a=0;a<N;a++){ double atas=1,bawah=1; for(int b=0;b<N;b++){ if(a!=b){ System.out.println("atas : "+atas+"*("+X +"-"+x[b]+")"); atas=atas*(X-x[b]); System.out.println("bawah : "+bawah+"*(" +x[a]+"-"+x[b]+")"); bawah=bawah*(x[a]-x[b]); } }

System.out.println("y["+a+"]=("+fx[a]+"*"+atas+"/"+bawah +")"); y[a]=(fx[a]*atas)/bawah; System.out.println("y["+a+"]="+y[a]+"\n"); } System.out.println(); //menampilkan nilai y double Px=0; for(int i=0;i<N;i++){ System.out.println("y"+i+" = "+y[i]); // menampilkan yg TIDAK DIBULATKAN System.out.println("y"+i+" = "+ubah(y[i])); // menampilk an yg DIBULATKAN System.out.println("Px"+i+"="+Px+"+"+y[i]); Px=Px+y[i]; System.out.println("Px"+i+"="+Px+"\n"); } System.out.println(""); System.out.println("Jadi Nilai fungsi di x = "+X+" adalah"); System.out.print("P(x)="+ubah(Px)); } }