Conjugate Gradient Optimizer

Version (2.41 KB) by Peter
This routine lets you optimize large scale linear systems


Updated 22 Oct 2009

View License

% This example demonstrates the use of conjgrad.m
% The main advantage of conjgrad.m is that it takes handles to functions
% which perform the evaluation of the linear operator and its adjoint.
% The parameter space can be multidimensional.

%% Example #1 - matrix inversion

% generate well conditioned random matrix
N = 128;

% define the operator and its adjoint
operator = @(x) A*x;
adjoint = @(x) A'*x;
x0 = zeros(size(b));

res_limit = 1e-4;
max_steps = 100;
[x, Res] = conjgrad(x0, b, operator, adjoint, res_limit, max_steps);

%% Example #2 - deconvolution
N = 128;

% the convolution kernel is two dots. So the forward operator will make a
% twin-image shifted by 2 pixels.
kernel = zeros(N);
kernel(N/2,N/2) = 1;
kernel(N/2,N/2+2) = 1;
f_kernel = fft2(kernel);
test_image = randn(N);

% the adjoint of FFT is iFFT
% the adjoint of pointwise multiplication is multiplication by conjugate
operator = @(x) ifft2(f_kernel.*fft2(x));
adjoint = @(x) ifft2(conj(f_kernel).*fft2(x));
b = operator(test_image);
x0 = zeros(size(test_image));

res_limit = 1e-2;
max_steps = 100;
[x, Res] = conjgrad(x0, b, operator, adjoint, res_limit, max_steps);

Cite As

Peter (2023). Conjugate Gradient Optimizer (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2009b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Find more on Thermodynamics & Statistical Physics in Help Center and MATLAB Answers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes