如何在MATLAB中使用带有函数的pcg(How to use pcg with a function in MATLAB)

编程入门 行业动态 更新时间:2024-10-28 06:33:09
如何在MATLAB中使用带有函数的pcg(How to use pcg with a function in MATLAB)

我将在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.

更多推荐

本文发布于:2023-04-29 09:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335848.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   如何在   function   MATLAB   pcg

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!