Advertisement
Guest User

Untitled

a guest
Nov 16th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.02 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.lang.Math;
  3. /**
  4. * Created by 1501290108 on 16.11.2018.
  5. */
  6. class Rextester{
  7. static class Lagrange{
  8. static Double lagrangeFuncSecond(double x, double x0, double x1, double x2) {
  9. return ((x - x1) * (x - x2)) / ((x0 - x1) * (x0 - x2)) * func(x0) +
  10. ((x - x0) * (x - x2)) / ((x1 - x0) * (x1 - x2)) * func(x1) +
  11. ((x - x0) * (x - x1)) / ((x2 - x0) * (x2 - x1)) * func(x2);
  12. }
  13.  
  14. static Double lagrangeFuncThird(double x, double x0, double x1, double x2, double x3) {
  15. return ((x - x1) * (x - x2) * (x - x3)) / ((x0 - x1) * (x0 - x2) * (x0 - x3)) * func(x0) +
  16. ((x - x0) * (x - x2)* (x - x3)) / ((x1 - x0) * (x1 - x2)*(x1 - x3)) * func(x1) +
  17. ((x - x0) * (x - x1)*(x-x3)) / ((x2 - x0) * (x2 - x1) * (x2 - x3)) * func(x2) +
  18. ((x - x0) * (x - x1)*(x-x2)) / ((x3 - x0) * (x3 - x1) * (x3 - x2)) * func(x3);
  19. }
  20.  
  21. static Double func(double x) {
  22. return x - Math.cos(x);
  23. }
  24.  
  25. static double AnalyticLagrangeFuncFaultSecond(double x, double x0, double x1, double x2){
  26. return (Math.abs(func(x) - lagrangeFuncSecond(x, x0, x1, x2)));
  27. }
  28.  
  29. static double FormulaLagrangeFuncFaultSecond(double x, double x0, double x1, double x2){
  30. return (Math.abs(func(x) - lagrangeFuncSecond(x, x0, x1, x2)));
  31. }
  32.  
  33. static double AnalyticLagrangeFuncFaultThird(double x, double x0, double x1, double x2, double x3){
  34. return (Math.abs(func(x) - lagrangeFuncThird(x, x0, x1, x2, x3)));
  35. }
  36.  
  37. static double FormulaLagrangeFuncFaultThird(double x, double x0, double x1, double x2, double x3){
  38. double max = Math.max((x0*(Math.pow(2,x0))*Math.log(2) + (Math.pow(2,x0))), (x1*(Math.pow(2,x1))*Math.log(2) + (Math.pow(2,x1))));
  39. max = Math.max((x2*(Math.pow(2,x2))*Math.log(2) + (Math.pow(2,x2))), max);
  40. max = Math.max((x3*(Math.pow(2,x3))*Math.log(2) + (Math.pow(2,x3))), max);
  41. return (max/24 * Math.abs((x-x0)*(x-x1)*(x-x2)*(x-x3)));
  42. }
  43.  
  44. static double LagrangeFuncValue(ArrayList<Double> x_list, double X) {
  45. for (int i = 0; i < x_list.size(); i++) {
  46. if (X >= x_list.get(i)) {
  47. System.out.println();
  48. System.out.println("X = " + X);
  49. try {
  50. System.out.println("Значение полинома в точке X: " + lagrangeFuncThird(X, x_list.get(i), x_list.get(i + 1), x_list.get(i + 2), x_list.get(i + 3)));
  51. System.out.println("Погрешность используя аналитическое задание функции: " + AnalyticLagrangeFuncFaultThird(X, x_list.get(i), x_list.get(i + 1), x_list.get(i + 2), x_list.get(i + 3)));
  52. System.out.println("Погрешность используя формулу: =< " + FormulaLagrangeFuncFaultThird(X, x_list.get(i), x_list.get(i + 1), x_list.get(i + 2), x_list.get(i + 3)));
  53. return lagrangeFuncThird(X, x_list.get(i), x_list.get(i + 1), x_list.get(i + 2), x_list.get(i + 3));
  54. } catch (Exception e) {
  55. System.out.println("Значение полинома в точке X: " + lagrangeFuncThird(X, x_list.get(i - 2), x_list.get(i - 1), x_list.get(i), x_list.get(i + 1)));
  56. System.out.println("Погрешность используя аналитическое задание функции: " + AnalyticLagrangeFuncFaultThird(X, x_list.get(i-2), x_list.get(i -1), x_list.get(i), x_list.get(i + 1)));
  57. System.out.println("Погрешность используя формулу: =< " + FormulaLagrangeFuncFaultThird(X, x_list.get(i-2), x_list.get(i -1), x_list.get(i), x_list.get(i + 1)));
  58. return lagrangeFuncThird(X,x_list.get(i - 2), x_list.get(i - 1), x_list.get(i), x_list.get(i + 1));
  59. }
  60. }
  61. }
  62. return -1;
  63. }
  64. }
  65.  
  66. static class Newton{
  67. static double[][] FiniteDifferences = new double[6][6];
  68.  
  69. static double func(double x){
  70. return x - Math.cos(x);
  71. }
  72.  
  73. static double AnalyticNewtonFuncFaultSecondX1(double x, double x1, double x2){
  74. return (Math.abs(func(x) - NewtonFuncSecondX1(x, x1, x2)));
  75. }
  76.  
  77. static double AnalyticNewtonFuncFaultSecondX2(double x, double x1, double x2){
  78. return (Math.abs(func(x) - NewtonFuncSecondX2(x, x1, x2)));
  79. }
  80.  
  81. static double AnalyticNewtonFuncFaultSecondX3(double x, double x1, double x2){
  82. return (Math.abs(func(x) - NewtonFuncSecondX3(x, x1, x2)));
  83. }
  84.  
  85. static double FormulaNewtonFuncFaultSecond(double x, double x1, double x2){
  86. double max = Math.max((x1*(Math.pow(2,x1))*Math.log(2) + (Math.pow(2,x1))), (x2*(Math.pow(2,x2))*Math.log(2) + (Math.pow(2,x2))));
  87. return (max/24 * Math.abs((x-x1)*(x-x2)));
  88. }
  89.  
  90. static double NewtonFuncSecondX1(double X, double x1, double x2){
  91. return(func(x1) + ((FiniteDifferences[0][0]/Math.PI/5) * (X - x1)) + ((FiniteDifferences[0][1]/(2 * Math.pow(Math.PI/5, 2))) * (X - x2) * (X - x1)));
  92. }
  93. static double NewtonFuncSecondX2(double X, double x1, double x2){
  94. return(func(x1) + ((FiniteDifferences[1][0]/Math.PI/5) * (X - x1)) + ((FiniteDifferences[1][1]/(2 * Math.pow(Math.PI/5, 2))) * (X - x2) * (X - x1)));
  95. }
  96. static double NewtonFuncSecondX3(double X, double x1, double x2){
  97. return(func(x1) + ((FiniteDifferences[3][0]/Math.PI/5) * (X - x1)) + ((FiniteDifferences[3][1]/(2 * Math.pow(Math.PI/5, 2))) * (X - x2) * (X - x1)));
  98. }
  99.  
  100. static double NewtonFuncValue(){
  101. return 1.2;
  102. }
  103.  
  104. static void FiniteDifferenceValue(ArrayList<Double> y_list){
  105. for(int i = 0; i < y_list.size() - 1; i++){
  106. FiniteDifferences[i][0] = y_list.get(i+1) - y_list.get(i);
  107. }
  108. for(int i = 0; i < y_list.size() - 2; i++){
  109. FiniteDifferences[i][1] = FiniteDifferences[i+1][0] - FiniteDifferences[i][0];
  110. }
  111. for(int i = 0; i < y_list.size() - 3; i++){
  112. FiniteDifferences[i][2] = FiniteDifferences[i+1][1] - FiniteDifferences[i][1];
  113. }
  114. for(int i = 0; i < y_list.size() - 4; i++){
  115. FiniteDifferences[i][3] = FiniteDifferences[i+1][2] - FiniteDifferences[i][2];
  116. }
  117. for(int i = 0; i < y_list.size() - 5; i++){
  118. FiniteDifferences[i][4] = FiniteDifferences[i+1][3] - FiniteDifferences[i][3];
  119. }
  120.  
  121. }
  122. }
  123.  
  124. public static void main(String[] args) {
  125.  
  126. double X1 = 0.71;
  127. double X2 = 1.54;
  128. double X3 = 3.01;
  129. double h = Math.PI/5;
  130. double x0 = 0;
  131. double x1 = Math.PI;
  132. ArrayList<Double> x_list = new ArrayList<>();
  133. ArrayList<Double> y_list = new ArrayList<>();
  134. while(x0 <= x1){
  135. x_list.add(x0);
  136. x0 = (x0 * 10 + h * 10) / 10;
  137. }
  138.  
  139. for(int i = 0; i < x_list.size(); i++){
  140. y_list.add(Lagrange.func(x_list.get(i)));
  141. }
  142. System.out.println();
  143.  
  144. System.out.println("x | y(x)");
  145. System.out.println("-------------------------");
  146. for(int i = 0; i < x_list.size(); i++){
  147. System.out.println(x_list.get(i) + " | " + y_list.get(i));
  148. System.out.println("-------------------------");
  149. }
  150. System.out.println("\nЛагранж");
  151. Lagrange.LagrangeFuncValue(x_list, X1);
  152. Lagrange.LagrangeFuncValue(x_list, X2);
  153. Lagrange.LagrangeFuncValue(x_list, X3);
  154. System.out.println("\nНьютон");
  155. System.out.println("\nТаблица конечных разностей");
  156.  
  157. Newton.FiniteDifferenceValue(y_list);
  158. for(int i = 0; i < 6; i++){
  159. System.out.println();
  160. for(int j = 0; j < 6; j++){
  161. double buf = Newton.FiniteDifferences[i][j];
  162. System.out.print(buf + " ");
  163. for(int f = 0; f < 25 - (buf + " ").length(); f++){
  164. System.out.print(" ");
  165. }
  166. }
  167. }
  168.  
  169. System.out.println("\n\nX = " + X1);
  170. System.out.println("Значение полинома в точке X: " + Newton.NewtonFuncSecondX1(X1, 1.0, 1.2));
  171. System.out.println("Погрешность используя аналитическое задание функции: " + Newton.AnalyticNewtonFuncFaultSecondX1(X1, 1.0, 1.2));
  172. System.out.println("Погрешность используя формулу: =< " + Newton.FormulaNewtonFuncFaultSecond(X1, 1.0, 1.2));
  173. System.out.println("\nX = " + X2);
  174. System.out.println("Значение полинома в точке X: " + Newton.NewtonFuncSecondX2(X2, 1.2, 1.4));
  175. System.out.println("Погрешность используя аналитическое задание функции: " + Newton.AnalyticNewtonFuncFaultSecondX2(X2, 1.2, 1.4));
  176. System.out.println("Погрешность используя формулу: =< " + Newton.FormulaNewtonFuncFaultSecond(X2, 1.2, 1.4));
  177. System.out.println("\nX = " + X3);
  178. System.out.println("Значение полинома в точке X: " + Newton.NewtonFuncSecondX3(X3, 1.6, 1.8));
  179. System.out.println("Погрешность используя аналитическое задание функции: " + Newton.AnalyticNewtonFuncFaultSecondX3(X3, 1.6, 1.8));
  180. System.out.println("Погрешность используя формулу: =< " + Newton.FormulaNewtonFuncFaultSecond(X3, 1.6, 1.8));
  181. }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement