使用 fzero:未定义的函数或方法“isfinite”对于“sym”类型的输入参数
我使用 matlab 进行符号计算。经过长时间的计算,我得到了 x 的函数,它是贝塞尔函数的组合,我想找到它的零点。
为此,我在 Matlab 中使用 fzero
函数。但是,虽然它非常适合单贝塞尔函数,但它不适用于矿井函数。 <代码>
>> fzero(@(x)besselj(0,x), 3.5)
ans =
2.4048
>>> fzero(@(x)DELTA_xi, 3.5)
???对于“sym”类型的输入参数,未定义函数或方法“isfinite”。
错误==> fzero 333
elseif ~isfinite(fx) || 〜isreal(fx)
>>德尔塔_xi
贝塞利(1, (3*x)/10)*贝塞利(1, (3*x)/10)*贝塞利(1, x)*贝塞利(0, x) - 贝塞利(1, (3*x)/ 10)*贝塞利(1, (3*x)/10)*贝塞利(0, x)*贝塞利(1, x) - 贝塞利(1, (3*x)/10)*贝塞利(1, (3* x)/10)*贝塞利(0, x)*贝塞利(1, x) + 贝塞利(1, (3*x)/10)*贝塞利(1, (3*x)/10)*贝塞利(1, x)*贝塞尔克(0, x) - 贝塞尔j(1, (3*x)/10)*贝塞尔克(1, (3*x)/10)*贝塞利(0, x)*贝塞利(1, x) -贝塞利(1, (3*x)/10)*贝塞利(1, (3*x)/10)*贝塞利(1, x)*贝塞利(0, x) + 贝塞利(1, (3*x)/ 10)*贝塞利(1, (3*x)/10)*贝塞利(0, x)*贝塞利(1, x) + 贝塞利(1, (3*x)/10)*贝塞利(1, (3* x)/10)*贝塞利(1, x)*贝塞利(0, x)
为什么会出现这种情况?如何解决问题?
提前致谢
I use matlab for symbolic calculations. After long calculations I've got a function of x, which is the combination of bessel functions and I want to find it's zeros.
For that purpose I use fzero
function in Matlab. But while it works perfectly for single bessel functions, it wont work for the mine one.
>> fzero(@(x)besselj(0,x), 3.5) ans = 2.4048>> fzero(@(x)DELTA_xi, 3.5) ??? Undefined function or method 'isfinite' for input arguments of type 'sym'.
Error in ==> fzero at 333 elseif ~isfinite(fx) || ~isreal(fx)
>> DELTA_xi besseli(1, (3*x)/10)*besselj(1, (3*x)/10)*besselk(1, x)*bessely(0, x) - besseli(1, (3*x)/10)*besselj(1, (3*x)/10)*besselk(0, x)*bessely(1, x) - besseli(1, (3*x)/10)*bessely(1, (3*x)/10)*besselj(0, x)*besselk(1, x) + besseli(1, (3*x)/10)*bessely(1, (3*x)/10)*besselj(1, x)*besselk(0, x) - besselj(1, (3*x)/10)*besselk(1, (3*x)/10)*besseli(0, x)*bessely(1, x) - besselj(1, (3*x)/10)*besselk(1, (3*x)/10)*besseli(1, x)*bessely(0, x) + besselk(1, (3*x)/10)*bessely(1, (3*x)/10)*besseli(0, x)*besselj(1, x) + besselk(1, (3*x)/10)*bessely(1, (3*x)/10)*besseli(1, x)*besselj(0, x)
Why this happens? How to solve the issue?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您错误地使用了符号表示的函数句柄。
fzero 需要一个函数句柄。
因此,如果您这样做:
您会
注意到,如果您想调用符号函数,则必须间接执行:
I think you've mistaken function handles with symbolic representation.
fzero needs a function handle.
so if you do:
you get
note that if you want to call a symbolic function, you'll have to do it indirectly:
拉斯曼 已经为您的问题提供了一个解决方案,但我想我应该更详细地解释为什么您会收到错误并为您提供另一种可能的解决方案。
首先,考虑代码中的这个匿名函数:
该函数采用一个输入参数 x 并将其传递给函数 BESSELJ 将被评估并作为输出返回。 FZERO 使用这个函数没有问题。
现在考虑代码中的另一个匿名函数,它的表达式是使用符号变量
'x'
创建的符号方程DELTA_xi
:该函数采用输入参数
x
,但是应该如何处理这个输入参数呢? MATLAB 不会自动知道将输入变量x
连接到DELTA_xi
中的符号变量'x'
,因此当该函数通过以下方式求值时FZERO 它将简单地返回符号方程DELTA_xi
(FZERO 无法使用该方程,如您看到的错误所示)。使用函数 EVAL 就像 Rasman 的解决方案 将评估
DELTA_xi
使用从 FZERO 传递到匿名函数的x
的可用值,解决了您的问题。但是,另一种解决方案是使用 SUBS 函数,如下所示:将用输入值
x
替换DELTA_xi
中每次出现的符号变量'x'
,本质上执行以下操作与 EVAL 相同,但明确指出输入参数替换哪个符号变量。区别?请注意,这会起作用:但这不会:
因为输入变量的名称和符号变量不匹配。
Rasman already gave you one solution to your problem, but I thought I'd explain in more detail why you were getting your error and give you another possible solution.
First, consider this anonymous function from your code:
This function takes one input argument
x
and passes it to the function BESSELJ to be evaluated and returned as output. FZERO has no problem using this function.Now consider this other anonymous function from your code, which has as its expression the symbolic equation
DELTA_xi
created using the symbolic variable'x'
:This function takes an input argument
x
, but what should be done with this input argument? MATLAB doesn't automatically know to connect the input variablex
to the symbolic variable'x'
inDELTA_xi
, so when this function gets evaluated by FZERO it will simply return the symbolic equationDELTA_xi
(which FZERO is unable to use, as indicated by the error you see).Using the function EVAL like in Rasman's solution will evaluate
DELTA_xi
using the available value forx
passed from FZERO to the anonymous function, solving your problem. However, another solution is to use the SUBS function like so:Which will substitute the input value
x
for every occurrence of the symbolic variable'x'
inDELTA_xi
, essentially doing the same thing as EVAL but explicitly stating which symbolic variable the input argument replaces. The difference? Note that this will work:But this will not:
Because the name of the input variable and the symbolic variable don't match.