Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- //x0=2, x1=2,75, x2=4, P(3)=0,32954
- public class Lagrange {
- public static int n;
- public static double fonk[];
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.println("Polinom derecesini giriniz:");
- n = sc.nextInt();
- fonk = new double[n + 1];
- for (int i = 0; i < n + 1; i++) {
- System.out.println("x " + n + "," + i + ":");
- fonk[i] = sc.nextDouble();
- }
- System.out.println("P(3) = " + P(3));
- }
- public static double P(double x) {
- double sonuc = 0;
- double l[] = new double[n + 1];
- for (int i = 0; i < n + 1; i++) {
- l[i] = 1;
- }
- for (int j = 0; j < n + 1; j++) {
- for (int i = 0; i < n + 1; i++) {
- if (i != j)
- l[j] *= (x - fonk[i]) / (fonk[j] - fonk[i]);
- }
- }
- for (int i = 0; i < n + 1; i++) {
- sonuc += l[i] * F(fonk[i]);
- }
- return sonuc;
- }
- public static double F(double x) {
- return 1 / x;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement