⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 conjgrad1.m

📁 Mathematical Methods by Moor n Stiling.
💻 M
字号:
function [x,D] = conjgrad1(Q,b)
% function [x,D] = conjgrad1(Q,b)
%
% Solve the equation Qx = b using conjugate gradient, where Q is symmetric
%
% Q = symmetric matrix
% b = right-hand side

% x = solution
% D = (optional) array of conjugate direction vectors

% Copyright 1999 by Todd K. Moon

[m,n] = size(Q);  % assume square and symmetric; should test

% Initial values
x = zeros(n,1); x(1) = 1;  g = Q*x - b;
d = -g;
if(nargout==2)  D = [];  end
while(norm(g) > 1.e-8)
  alpha = -g'*d/(d'*Q*d);
  x = x + alpha*d;
  if(nargout == 2)  D = [D d];  end
  g = Q*x - b;
  beta = g'*Q*d/(d'*Q*d);
  d = -g + beta*d;
end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -