我将在MATLAB中使用共轭梯度法求解反问题AX=b 。 我想在MATLAB中使用pcg函数,因为我知道而不是矩阵A我可以使用函数。
我有一个函数,例如afun ,它有一些条目。 在文档中,我已经看到afun函数在没有条目的情况下输入到pcg函数中,但是,当我这样做时,错误not enough input arguments出现。 我使用这样的代码:
b = afun(ent1,ent2); x = pcg(@afun,b,tol,max_iter);我该如何在pcg使用我的功能?
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?
最满意答案
根据文档,函数句柄应该具有签名afun(x)并返回A*x 。
您的函数显然需要两个输入...您需要使用匿名函数来包装调用,如下所示:
% 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);有一个doc页面解释了如何使用函数句柄 参数化函数来传递额外的args。
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.
更多推荐
发布评论