3.0

3.0 | 1 rating Rate this file 125 downloads (last 30 days) File Size: 2.41 KB File ID: #25636

Conjugate Gradient Optimizer

by Peter

 

22 Oct 2009

Code covered by BSD License  

This routine lets you optimize large scale linear systems

Download Now | Watch this File

File Information
Description

% 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;
[U,S,V]=svd(randn(N));
s=diag(S);
A=U*diag(s+max(s))*V;
b=randn(N,1);

% 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);
plot(log10(Res));

%% 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);
plot(log10(Res))

MATLAB release MATLAB 7.9 (2009b)
Zip File Content  
Other Files conjgrad.m,
example.m,
license.txt
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (2)
23 Oct 2009 Bruno Luong

Not sure what advantage of conjgrad over Matlab builtin function LSQR (where preconditioning is possible):

x = lsqr(@afun,b);
    
function y = afun(x,transp_flag)
        if strcmp(transp_flag,'transp')
            y = A'*x;
        else
            y = A*x;
        end
    end

25 Oct 2009 Peter

Well, the only advantage is that the vectors x and y don't necessarily have to be ordered in a row format, they can be multidimensional arrays. The internal function mydot can handle their dot products.

Otherwise you are right, there is no much difference.

The pcg matlab function is also an alternative.

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
optimization Peter 23 Oct 2009 10:34:55
image processing Peter 23 Oct 2009 10:34:55
mathematics Peter 23 Oct 2009 10:34:55
simulation Peter 23 Oct 2009 10:34:55
signal processing Peter 23 Oct 2009 10:34:55
modeling Peter 23 Oct 2009 10:34:55
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com