Advertisement
Vladislav_Bezruk

Task

Oct 3rd, 2021
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. #include<iostream>
  2. #include<conio.h>
  3. #include<cmath>
  4.  
  5. using namespace std;
  6.  
  7. struct Vector {
  8.     float x, y, z;
  9. };
  10. float norma(Vector X);
  11.  
  12. Vector gradient(Vector X);
  13.  
  14. float norma(Vector X) {
  15.     return sqrt(pow(X.x,2)) + (pow(X.y,2)) + (pow(X.z,2));
  16. }
  17. Vector gradient(Vector X) {
  18.     Vector grad;
  19.     grad.x = 4*((pow(X.x,2))+2*X.y-3*X.z-0.9)*X.x+16*X.x-4*(pow(X.y,2))+4*X.z-24.84+12*X.y-4*(pow(X.z,2));
  20.     grad.y = 4*(pow(X.x,2))+26*X.y-12*X.z-30.66-4*(2*X.x-(pow(X.y,2))+X.z-1.7)*X.y+12*X.x-6*(pow(X.z,2));
  21.     grad.z = -6*(pow(X.x,2))-12*X.y+20*X.z+2.0+4*X.x-2*(pow(X.y,2))-4*(2*X.x+3*X.y-pow(X.z,2)-4.51)*X.z;
  22.  
  23.     return grad;
  24. }
  25. int main() {
  26.     Vector X0, X1;
  27.     float I = 0.001, eps = 0.0001, b = 0;
  28.  
  29.     X0.x = X0.y = X0.z = 0;
  30.     X1.x = X1.y = X1.z = 0;
  31.  
  32.     while(norma(gradient(X1))>eps) {
  33.         b++;
  34.         if(b > 1) {
  35.             X0 = X1;
  36.         }
  37.         X1.x = X0.x -I * gradient(X0).x;
  38.         X1.y = X0.y -I * gradient(X0).y;
  39.         X1.z = X0.z -I * gradient(X0).z;
  40.     }
  41.     cout << " Our roots are " << endl;
  42.     cout << X1.x << endl <<  X1.y << endl << X1.z << endl;
  43.  
  44.     return 0;
  45. }
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement