Advertisement
STANAANDREY

conjgrad

Mar 6th, 2023
1,279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.37 KB | None | 0 0
  1. function x = conjgrad(A,b,x0,k)
  2.     x = x0;
  3.     d = b - A*x0;
  4.     lastR = b - A*x0;
  5.     for i=1:k
  6.          if lastR == 0
  7.              return
  8.          end
  9.          alfa=lastR' * lastR / (d' * A * d);
  10.          x = x + alfa * d;
  11.          r = lastR - alfa * A * d;
  12.          beta = r' * r / (lastR' * lastR);
  13.          d = r + beta*d;
  14.          lastR = r;
  15.     end
  16. end
  17.  
  18.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement