Skip to content

Instantly share code, notes, and snippets.

@Atreyagaurav
Created July 14, 2019 15:22
Show Gist options
  • Save Atreyagaurav/b787d13c2888617ddc469d8e250c0d72 to your computer and use it in GitHub Desktop.
Save Atreyagaurav/b787d13c2888617ddc469d8e250c0d72 to your computer and use it in GitHub Desktop.
Matlab script for solving linear equations by conjugate gradient method.
function f=sol(a,x,b)
%conjugate gradient method
g=a*x-b;
d=-g;
while(1):
alpha=(g'*g)/(d'*a*d);
x=x+alpha*d
beta=g'*g;
g=g+alpha*a*d;
beta=g'*g/beta;
d=-g+beta*d;
if alpha<0.00001 then
break;
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment