user2983136
user2983136

Reputation: 17

How to use pcg with a function in MATLAB

I am going to solve an inverse problem, AX=b, using conjugate gradient method in MATLAB. I want to use pcg function in MATLAB and as I know instead of matrix A I can use a function.

I have a function for example afun which has some entries. In the documents, I have seen that the afun function is entered in pcg function without entries, however, when I do the same, the error not enough input arguments appears. I use a code like this:

b = afun(ent1,ent2);
x = pcg(@afun,b,tol,max_iter);

How should I use my function in pcg?

Upvotes: 0

Views: 1183

Answers (1)

Amro
Amro

Reputation: 124573

According to the documentation, the function handle should the have the signature afun(x) and return A*x.

Your function apparently takes two inputs... You need to use a anonymous function to wrap the call, something like this:

% I dont know what these ent1/ent2 represent exactly,
% so you must complete the ".." part first
fcn = @(x) afun(x, ..)

% now you can call PCG
x = pcg(fcn, b, tol, maxiter);

There is a doc page explaining how to parameterize functions to pass extra args using function handles.

Upvotes: 0

Related Questions