Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. package interpolacja_fs;
  2.  
  3. import static java.lang.Math.pow;
  4. import java.util.Scanner;
  5.  
  6. public class Interpolacja_FS {
  7.  
  8. public static void main(String[] args) {
  9. Scanner od = new Scanner(System.in);
  10. System.out.println("Podaj wilekosc tabeli");
  11. int n = od.nextInt();
  12. System.out.println("Podaj jaki x ma byc wyliczony");
  13. double xx = od.nextDouble();
  14. double[] x = new double[n];
  15. double[] y = new double[n];
  16. for (int i = 0; i < n; i++) {
  17. System.out.println("Podaj x= ");
  18. x[i] = od.nextInt();
  19. System.out.println("Podaj y= ");
  20. y[i] = od.nextInt();
  21. }
  22.  
  23. // double[] a = lsolve(A, b);
  24. }
  25. //--------------------------------------------
  26.  
  27. public static double fun_skl(double[] a, double xx, double[] x) {
  28. double wynik = 0;
  29. double [] temp = new double [a.length-4];
  30. temp[0]=0;
  31. for(int i =0;i<temp.length;i++)
  32. {
  33. temp[i]= temp[i]+a[i+4]*(pow(xx-x[i+1],3));
  34. }
  35.  
  36. if (xx < x[1]) {
  37. for (int i = 0; i < a.length - 3; i++) {
  38. wynik = wynik + a[i] * pow(xx, i);
  39. }
  40. }
  41. if (xx > x[1] && xx < x[2]) {
  42. for (int i = 0; i < a.length - 3; i++) {
  43. wynik = wynik + a[i] * pow(xx, i)+temp[i];
  44. }
  45. }
  46. if (xx > x[2] && xx < x[3]) {
  47. for (int i = 0; i < a.length - 3; i++) {
  48. wynik = wynik + a[i] * pow(xx, i)+temp[i+1];
  49. }
  50. }
  51. if (xx > x[3] && xx < x[4]) {
  52. for (int i = 0; i < a.length - 3; i++) {
  53. wynik = wynik + a[i] * pow(xx, i)+temp[i+2];
  54. }
  55. }
  56. return wynik;
  57. }
  58.  
  59. public static double[] lsolve(double[][] A, double[] b) {
  60. int N = b.length;
  61.  
  62. for (int p = 0; p < N; p++) {
  63.  
  64. // find pivot row and swap
  65. int max = p;
  66. for (int i = p + 1; i < N; i++) {
  67. if (Math.abs(A[i][p]) > Math.abs(A[max][p])) {
  68. max = i;
  69. }
  70. }
  71. double[] temp = A[p];
  72. A[p] = A[max];
  73. A[max] = temp;
  74. double t = b[p];
  75. b[p] = b[max];
  76. b[max] = t;
  77.  
  78. // singular or nearly singular
  79. if (Math.abs(A[p][p]) <= Math.E) {
  80. throw new RuntimeException("Matrix is singular or nearly singular");
  81. }
  82.  
  83. // pivot within A and b
  84. for (int i = p + 1; i < N; i++) {
  85. double alpha = A[i][p] / A[p][p];
  86. b[i] -= alpha * b[p];
  87. for (int j = p; j < N; j++) {
  88. A[i][j] -= alpha * A[p][j];
  89. }
  90. }
  91. }
  92.  
  93. // back substitution
  94. double[] x = new double[N];
  95. for (int i = N - 1; i >= 0; i--) {
  96. double sum = 0.0;
  97. for (int j = i + 1; j < N; j++) {
  98. sum += A[i][j] * x[j];
  99. }
  100. x[i] = (b[i] - sum) / A[i][i];
  101. }
  102. return x;
  103. }
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement