Advertisement
Guest User

Untitled

a guest
Mar 20th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. class Interpolacja{
  2.  
  3. double Newton(double[] X, double[] Y, double wartoscX ){
  4.  
  5. int n=X.length;
  6. double[][] tab = new double[n][n+1];
  7. double y=0;
  8. double w=1;
  9. int k=0;
  10.  
  11. for(int i=0;i<Y.length;i++){
  12. tab[i][0]=Y[i];
  13. }
  14. for(int i=0;i<n;i++){
  15. try{
  16. for(int j=0; j<n-1;j++){
  17. tab[j][i+1]=((tab[j+1][i])-(tab[j][i]))/((X[j+1+k])-(X[j]));
  18. }
  19. }
  20. catch(Exception e){}
  21. k++;
  22. y=y+tab[0][i]*w;
  23. w=w*(wartoscX-X[i]);
  24. }
  25. return y;
  26. }
  27. }
  28.  
  29. public class Newton{
  30.  
  31. public static void main(String[] args){
  32.  
  33. double[] x = {-4,-2,0,2,4};
  34. double[] y = {-116,-20,4,4,28};
  35. double wartosc_x = 3;
  36.  
  37. Interpolacja interpolacja = new Interpolacja();
  38. System.out.println("Interpolacja Newtona wynosi: " +interpolacja.Newton(x,y,wartosc_x) + " dla x="+wartosc_x );
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement