简体   繁体   中英

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 ?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM