Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lab_metody_numeryczne;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class Lagrange {
- static final Scanner s = new Scanner(System.in);
- static ArrayList<Double>podaj_x(int q)
- {
- ArrayList<Double> x=new ArrayList<>();
- for(int i=0;i<q;i++)
- {
- System.out.println("Podaj wartość:"+ (i+1)+":");
- x.add(s.nextDouble());
- }
- return x;
- }
- static ArrayList<Double>podaj_y(int q)
- {
- ArrayList<Double> y= new ArrayList<>();
- for(int t=0;t<q;t++)
- {
- System.out.println("Podaj y: "+(t+1)+";");
- y.add(s.nextDouble());
- }
- return y;
- }
- static public double lagrange(ArrayList<Double> y, ArrayList<Double> x, double t)
- {
- double inter=0.0;
- double k;
- for(int i=0;i<x.size();i++)
- {
- k=1.0;
- for(int h=0;h<x.size();h++)
- {
- if(h !=i)
- {
- k=k*((h-x.get(h))/(x.get(i)-x.get(h)));
- }
- }
- inter +=k*y.get(i);
- }
- return inter;
- }
- public static void main(String[] args)
- {
- ArrayList<Double> x=new ArrayList<>();
- ArrayList<Double> y=new ArrayList<>();
- double lagrange;
- int q;
- int p;
- System.out.println("Podaj ilość węzłów: ");
- q=s.nextInt();
- x=podaj_x(q);
- y=podaj_y(q);
- System.out.println("Podaj wartość dla której szukasz wartości funkcji: ");
- p=s.nextInt();
- lagrange = lagrange(y, x, p);
- System.out.println("Wartość funkcji wynosi: ");
- System.out.println("l("+p+")="+lagrange);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement