使用 fzero:未定义的函数或方法“isfinite”对于“sym”类型的输入参数

发布于 2024-11-08 17:13:01 字数 922 浏览 0 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

许久 2024-11-15 17:13:01

我认为您错误地使用了符号表示的函数句柄。

fzero 需要一个函数句柄。

因此,如果您这样做:

DELTA_xi = @(x) 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)
fzero(DELTA_xi, 3.5)

您会

3.8173

注意到,如果您想调用符号函数,则必须间接执行:

fzero(@(x)eval(DELTA_xi), 3.5)

I think you've mistaken function handles with symbolic representation.

fzero needs a function handle.

so if you do:

DELTA_xi = @(x) 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)
fzero(DELTA_xi, 3.5)

you get

3.8173

note that if you want to call a symbolic function, you'll have to do it indirectly:

fzero(@(x)eval(DELTA_xi), 3.5)
指尖微凉心微凉 2024-11-15 17:13:01

拉斯曼 已经为您的问题提供了一个解决方案,但我想我应该更详细地解释为什么您会收到错误并为您提供另一种可能的解决方案。

首先,考虑代码中的这个匿名函数:

fcn1 = @(x) besselj(0,x);

该函数采用一个输入参数 x 并将其传递给函数 BESSELJ 将被评估并作为输出返回。 FZERO 使用这个函数没有问题。

现在考虑代码中的另一个匿名函数,它的表达式是使用符号变量 'x' 创建的符号方程 DELTA_xi

fcn2 = @(x) DELTA_xi;

该函数采用输入参数 x,但是应该如何处理这个输入参数呢? MATLAB 不会自动知道将输入变量 x 连接到 DELTA_xi 中的符号变量 'x',因此当该函数通过以下方式求值时FZERO 它将简单地返回符号方程 DELTA_xi (FZERO 无法使用该方程,如您看到的错误所示)。

使用函数 EVAL 就像 Rasman 的解决方案 将评估 DELTA_xi 使用从 FZERO 传递到匿名函数的 x 的可用值,解决了您的问题。但是,另一种解决方案是使用 SUBS 函数,如下所示

fzero(@(x) subs(DELTA_xi,'x',x), 3.5)

:将用输入值 x 替换 DELTA_xi 中每次出现的符号变量 'x'本质上执行以下操作与 EVAL 相同,但明确指出输入参数替换哪个符号变量。区别?请注意,这会起作用:

fzero(@(y) subs(DELTA_xi,'x',y), 3.5)

但这不会:

fzero(@(y) eval(DELTA_xi), 3.5)

因为输入变量的名称和符号变量不匹配。

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:

fcn1 = @(x) besselj(0,x);

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':

fcn2 = @(x) DELTA_xi;

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 variable x to the symbolic variable 'x' in DELTA_xi, so when this function gets evaluated by FZERO it will simply return the symbolic equation DELTA_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 for x passed from FZERO to the anonymous function, solving your problem. However, another solution is to use the SUBS function like so:

fzero(@(x) subs(DELTA_xi,'x',x), 3.5)

Which will substitute the input value x for every occurrence of the symbolic variable 'x' in DELTA_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:

fzero(@(y) subs(DELTA_xi,'x',y), 3.5)

But this will not:

fzero(@(y) eval(DELTA_xi), 3.5)

Because the name of the input variable and the symbolic variable don't match.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文