且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

一个变量的fzero和fsolve之间的差异

更新时间:2022-12-01 13:24:12

是的.我只想提一下两者之间最直接的区别:

Yes, there is. I'll just mention the most straightforward difference between the two:

fsolve 可用于解决单个变量方程的零.但是, fzero 会发现零并且仅当函数与x轴交叉时.

fsolve can be used to solve for the zero of a single variable equation. However, fzero will find the zero if and only if the function crosses the x-axis.

这是一个简单的示例:考虑函数f=x^2.对于x的所有实数值,该函数均为非负数.它的根目录为x=0.我们将匿名函数定义为f=@(x)x.^2;,并尝试使用这两种方法查找根.

Here's a simple example: Consider the function f=x^2. The function is non-negative for all real values of x. This has a root at x=0. We'll define an anonymous function as f=@(x)x.^2; and try to find the root using both the methods.

使用fsolve

options=optimset('MaxIter',1e3,'TolFun',1e-10);
fsolve(f,0.1,options)


Equation solved.

fsolve completed because the vector of function values is near zero
as measured by the selected value of the function tolerance, and
the problem appears regular as measured by the gradient.

<stopping criteria details>


ans =

   1.9532e-04

不是零,而是闭合.

使用fzero

fzero(f,0.1)
Exiting fzero: aborting search for an interval containing a sign change
    because NaN or Inf function value encountered during search.
(Function value at -1.37296e+154 is Inf.)
Check function or try again with a different starting value.

ans =

   NaN

找不到.

请考虑另一个具有xc轴且根在x=0上的函数f=@(x)x.^3;的示例.

Consider another example with the function f=@(x)x.^3; that crosses the x-axis and has a root at x=0.

fsolve(f,0.1)

ans =

    0.0444

fzero(f,0.1)

ans =

  -1.2612e-16

在这种情况下,

fsolve也不完全返回0.即使使用上面定义的options,也只能通过fsolve进入0.0017.但是,fzero的答案在机器精度范围内是正确的!答案的差异并不是因为算法效率低下.这是因为它们的目标不同.

fsolve doesn't return exactly 0 in this case either. Even using the options I defined above only gets me to 0.0017 with fsolve. However, fzero's answer is correct to within machine precision!. The difference in answers is not because of inefficient algorithms. It's because their objectives are different.

fzero有一个明确的目标:找到零!简单的.那里没有歧义.如果它与x轴交叉,则为零,它将找到它(仅真实).如果没有交叉,它会发牢骚.

fzero has a clear goal: find the zero! Simple. No ambiguities there. If it crosses the x-axis, there is a zero and it will find it (real only). If it doesn't cross, it whines.

但是,fsolve的范围更广.它旨在解决非线性方程组.通常,您无法找到这些方程式的精确解,因此必须设置一个公差级别,在此范围内,您愿意接受该解决方案作为答案.因此,需要手动设置许多选项和公差以找出确切的根源.当然,您可以进行更精细的控制,但是要找到单个var方程的零,我认为这很痛苦.在这种情况下,我可能会使用fzero(假设它穿过x轴).

However, fsolve's scope is broader. It is designed to solve a system of nonlinear equations. Often you can't find an exact solution to those equations and will have to set a tolerance level, within which you're willing to accept the solution as the answer. As a result, there are a host of options and tolerances that need to be set manually to massage out the exact root. Sure, you have finer control but for finding a zero of a single var equation, I consider it a pain. I'd probably use fzero in that case (assuming it crosses the x-axis).

除此主要区别之外,实现和所使用的算法也有所不同.为此,我将为您提供有关这些功能的在线文档(请参见上面的链接).

Apart from this major difference, there are differences in implementations and the algorithms used. For that, I'll refer you to the online documentation on the functions (see the links above).