Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. public class MetodaGaussaSeidla {
  2.  
  3. static double h=0.00001;
  4. static double xn,x1;
  5. static double epsilon=0.0001;
  6.  
  7. static double[] gradient = new double[2];
  8.  
  9. static double funkcja(double x, double y){
  10. return 10*x*x+12*x*y+10*y*y;
  11. }
  12.  
  13. static double dfdx(double x, double y){
  14. //return (1/h)*(funkcja(x+h,y) - funkcja(x,y));
  15. return 20*x+12*y;
  16. }
  17.  
  18. static double dfdy(double x, double y){
  19. //return (1/h)*(funkcja(x,y+h) - funkcja(x,y));
  20. return 12*x+20*y;
  21. }
  22.  
  23. static double d2fdx2(double x, double y){
  24. //return (1/Math.pow(h,2))*((funkcja(x+h,y)-2*funkcja(x,y)+funkcja(x-h,y)));
  25. return 20;
  26. }
  27.  
  28. static double d2fdy2(double x, double y){
  29. //return (1/Math.pow(h,2))*(funkcja(x,y+h)-2*funkcja(x,y)+funkcja(x,y-h));
  30. return 20;
  31. }
  32.  
  33. static double d3fdx3(double x, double y){
  34. //return (1/Math.pow(h,3))*(funkcja(x+3*h,y)-3*funkcja(x+2*h,y)+3*funkcja(x+h,y)-funkcja(x,y));
  35. return 0;
  36. }
  37.  
  38. static double d3fdy3(double x, double y){
  39. //return (1/Math.pow(h,3))*(funkcja(x,y+3*h)-3*funkcja(x,y+2*h)+3*funkcja(x,y+h)-funkcja(x,y));
  40. return 0;
  41. }
  42.  
  43. static double MetodaStycznychPoX(double a, double b,double x){
  44. int licznik = 0;
  45. licznik++;
  46. if(dfdx(a,x) * d3fdx3(a,x) >= 0){
  47. x1 = a;
  48. }
  49. else{
  50. x1 = b;
  51. }
  52. while(dfdx(x1,x) >= epsilon){
  53. licznik++;
  54. xn = x1;
  55. x1 = xn - ( dfdx(xn,x) / d2fdx2(xn,x) );
  56. }
  57. //System.out.println("Iteracje = " + licznik);
  58. System.out.println("x1 = " + x1);
  59. return x1;
  60. }
  61.  
  62. static double MetodaStycznychPoY(double a, double b,double y){
  63.  
  64. int licznik = 0;
  65. //licznik++;
  66. if(dfdy(a,y) * d3fdy3(a,y) >= 0){
  67. x1 = a;
  68. }
  69. else{
  70. x1 = b;
  71. }
  72. while(dfdy(x1,y) >= epsilon ){
  73. licznik++;
  74. xn = x1;
  75. x1 = xn - ( dfdy(xn,y) / d2fdy2(xn,y) );
  76. }
  77. //System.out.println("Metoda Stycznych x1 = " + x1);
  78. //System.out.println("Iteracje = " + licznik);
  79. return x1;
  80. }
  81.  
  82. public static void liczGaussaSeidla(double x0, double y0, double epsilon){
  83. int licznik = 0;
  84. double x;
  85. double y;
  86. y = MetodaStycznychPoY(-100,100,y0);
  87. x = MetodaStycznychPoX(-100,100,x0);
  88.  
  89. //gradient[1] = MetodaStycznychPoY(-100,100,10);
  90. //gradient[0] = MetodaStycznychPoX(-100,100,10);
  91. //System.out.print(MetodaStycznychPoY(-100,100,10));
  92.  
  93. while(Math.abs(funkcja(x,y)) >= epsilon){
  94. x = MetodaStycznychPoY(-100,100,x);
  95. y = MetodaStycznychPoX(-100,100,y);
  96. licznik++;
  97. System.out.println(x);
  98. System.out.println(y);
  99. }
  100. //System.out.println(x);
  101. //System.out.println(y);
  102. }
  103.  
  104. public static void main(String[] args) {
  105. //System.out.print(dfdy(-100,10));
  106. liczGaussaSeidla(10, 10, 0.0001);
  107. //double x,y;
  108. //x = MetodaStycznychPoX(-100,100,10);
  109. //y = MetodaStycznychPoX(-100,100,x);
  110. //System.out.println(x);
  111. }
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement