Advertisement
Guest User

Untitled

a guest
Dec 25th, 2014
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. //x0=2, x1=2,75, x2=4, P(3)=0,32954
  4.  
  5. public class Lagrange {
  6.  
  7. public static int n;
  8. public static double fonk[];
  9.  
  10. public static void main(String[] args) {
  11.  
  12. Scanner sc = new Scanner(System.in);
  13. System.out.println("Polinom derecesini giriniz:");
  14. n = sc.nextInt();
  15.  
  16. fonk = new double[n + 1];
  17.  
  18. for (int i = 0; i < n + 1; i++) {
  19. System.out.println("x " + n + "," + i + ":");
  20. fonk[i] = sc.nextDouble();
  21. }
  22.  
  23. System.out.println("P(3) = " + P(3));
  24.  
  25. }
  26.  
  27. public static double P(double x) {
  28.  
  29. double sonuc = 0;
  30.  
  31. double l[] = new double[n + 1];
  32.  
  33. for (int i = 0; i < n + 1; i++) {
  34. l[i] = 1;
  35. }
  36.  
  37. for (int j = 0; j < n + 1; j++) {
  38. for (int i = 0; i < n + 1; i++) {
  39. if (i != j)
  40. l[j] *= (x - fonk[i]) / (fonk[j] - fonk[i]);
  41. }
  42. }
  43.  
  44. for (int i = 0; i < n + 1; i++) {
  45. sonuc += l[i] * F(fonk[i]);
  46. }
  47.  
  48. return sonuc;
  49. }
  50.  
  51. public static double F(double x) {
  52. return 1 / x;
  53. }
  54.  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement