Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <cmath>
  4. #include <iomanip>
  5. using namespace std;
  6.  
  7. //=======================================================================
  8. //wektor funkcji
  9.  
  10. void function_vector(double *f_vector, double x, double y, double z){
  11.  
  12. f_vector[0] = x*x + y*y + z*z - 2;
  13. f_vector[1] = x*x + y*y - 1;
  14. f_vector[2] = x*x - y;
  15.  
  16. }
  17.  
  18.  
  19. //=======================================================================
  20. //macierz Jakobiego
  21.  
  22.  
  23. void Jacobi_Matrix(double **matrix, double x, double y, double z){
  24.  
  25. matrix[0][0] = 0;
  26. matrix[0][1] = 1/(2+(4*x*y));
  27. matrix[0][2] = y/(x+(2*x*y));
  28. matrix[1][0] = 0;
  29. matrix[1][1] = 1/(1+(2*y));
  30. matrix[1][2] = 1/(-1-(2*y));
  31. matrix[2][0] = (1+(2*y))/((2*z)+(4*y*z));
  32. matrix[2][1] = (1+(2*y))/((-2*z)-(4*y*z));
  33. matrix[2][2] = 0;
  34.  
  35. }
  36.  
  37.  
  38. //=======================================================================
  39.  
  40.  
  41. double max3(double x, double y, double z)
  42. {
  43. if (x > y)
  44. {
  45. if (x > z)
  46. return x;
  47. return z;
  48. }
  49. else if (y > z)
  50. return y;
  51. return z;
  52. }
  53.  
  54.  
  55. int main()
  56. {
  57. std::cout << std::setprecision(10);
  58. double tab[3], TOLX=1.0e-12, TOLF=1.0e-12, estX=1, estF=1, LastWynik[3];
  59. int max_iterations = 100, j=0;
  60. double **matrix = new double *[3];
  61. for(int i=0; i<3; i++) matrix[i] = new double [3];
  62.  
  63. double wynik[3]={1.0,1.0,1.0};
  64.  
  65.  
  66. //=====================================================
  67. //petla liczaca kolejne przyblizenia trojki (x, y, z)
  68.  
  69.  
  70. while ((estX > TOLX || estF > TOLF) && j < max_iterations)
  71. {
  72. Jacobi_Matrix(matrix, wynik[0], wynik[1], wynik[2]);
  73. function_vector(tab, wynik[0], wynik[1], wynik[2]);
  74.  
  75. for(int i=0; i<3; i++)
  76. {
  77. LastWynik[i]=wynik[i];
  78. wynik[i] = wynik[i] - (tab[0]*matrix[i][0] + tab[1]*matrix[i][1] + tab[2]*matrix[i][2]);
  79. cout<<"wynik["<<i<<"] = "<<wynik[i]<<endl;
  80.  
  81. }
  82.  
  83. cout<<"Estymatory :\nestX = "<<estX<<"\nestF = "<<estF<<endl;
  84. cout<<"\n=====================================\n\n";
  85. estX = fabs(max3(wynik[0], wynik[1], wynik[2]) - max3(LastWynik[0], LastWynik[1], LastWynik[2]));
  86. estF = fabs(max3(tab[0], tab[1], tab[2]));
  87.  
  88. j++;
  89. }
  90. cout<<"Liczba iteracji: "<<j<<endl;
  91.  
  92. return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement