简体   繁体   English

如何在MATLAB中将pcg与函数一起使用

[英]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. 我将使用MATLAB中的共轭梯度法解决一个反问题AX=b I want to use pcg function in MATLAB and as I know instead of matrix A I can use a function. 我想在MATLAB中使用pcg函数,据我所知,我可以使用函数代替矩阵A

I have a function for example afun which has some entries. 我有一个功能,例如afun有一些条目。 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. 在文档中,我已经看到afun函数是在pcg函数中输入的,没有任何条目,但是,当我执行相同的操作时,出现的错误not enough input arguments 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 ? 我应该如何在pcg使用我的函数?

According to the documentation, the function handle should the have the signature afun(x) and return A*x . 根据文档,函数句柄应具有签名afun(x)并返回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 . 有一个文档页面介绍了如何使用函数句柄 参数化函数以传递额外的args。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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