Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.82 KB | None | 0 0
  1. package lab_metody_numeryczne;
  2. import java.util.ArrayList;
  3. import java.util.Scanner;
  4.  
  5. public class Lagrange {
  6.     static final Scanner s = new Scanner(System.in);
  7.    
  8.    
  9.      static ArrayList<Double>podaj_x(int q)
  10.     {
  11.        
  12.         ArrayList<Double> x=new ArrayList<>();
  13.         for(int i=0;i<q;i++)
  14.         {
  15.             System.out.println("Podaj wartość:"+ (i+1)+":");
  16.             x.add(s.nextDouble());
  17.         }
  18.         return x;
  19.     }
  20.     static ArrayList<Double>podaj_y(int q)
  21.     {
  22.         ArrayList<Double> y= new ArrayList<>();
  23.         for(int t=0;t<q;t++)
  24.         {
  25.             System.out.println("Podaj y: "+(t+1)+";");
  26.             y.add(s.nextDouble());
  27.         }
  28.         return y;
  29.     }
  30.     static public double lagrange(ArrayList<Double> y, ArrayList<Double> x, double t)
  31.     {
  32.         double inter=0.0;
  33.         double k;
  34.         for(int i=0;i<x.size();i++)
  35.         {
  36.             k=1.0;
  37.             for(int h=0;h<x.size();h++)
  38.             {
  39.                 if(h !=i)
  40.                 {
  41.                     k=k*((h-x.get(h))/(x.get(i)-x.get(h)));
  42.                 }
  43.             }
  44.             inter +=k*y.get(i);
  45.         }
  46.         return inter;
  47.     }
  48.    
  49.    
  50.     public static void main(String[] args)
  51.     {
  52.         ArrayList<Double> x=new ArrayList<>();
  53.         ArrayList<Double> y=new ArrayList<>();
  54.        
  55.         double lagrange;
  56.         int q;
  57.         int p;
  58.         System.out.println("Podaj ilość węzłów: ");
  59.         q=s.nextInt();
  60.         x=podaj_x(q);
  61.         y=podaj_y(q);
  62.         System.out.println("Podaj wartość dla której szukasz wartości funkcji: ");
  63.         p=s.nextInt();
  64.         lagrange = lagrange(y, x, p);
  65.         System.out.println("Wartość funkcji wynosi: ");
  66.         System.out.println("l("+p+")="+lagrange);
  67.                
  68.        }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement