fzero
非线性函数的根
说明
示例
通过求正弦函数在 3 附近的零点计算 。
fun = @sin; % function x0 = 3; % initial point x = fzero(fun,x0)
x = 3.1416
求余弦函数在 1 和 2 之间的零点。
fun = @cos; % function x0 = [1 2]; % initial interval x = fzero(fun,x0)
x = 1.5708
请注意, 和 的符号不同。
创建一个函数和一个单精度标量起点 x0。使用 fzero 从 x0 开始求该函数的根。
f = @(x)cosh(x)-2*sinh(x); x0 = single(1); [x,fval,exitflag,output] = fzero(f,x0)
x = single
0.5493
fval = single
1.1921e-07
exitflag = single
1
output = struct with fields:
intervaliterations: 9
iterations: 3
funcCount: 21
algorithm: 'bisection, interpolation'
message: 'Zero found in the interval [0.547452, 1.32]'
创建一个二元素单精度向量作为围捕根的起点,并比较解和求解过程。
x0 = single([-1,1]); [x2,fval2,exitflag2,output2] = fzero(f,x0)
x2 = single
0.5493
fval2 = single
1.1921e-07
exitflag2 = single
1
output2 = struct with fields:
intervaliterations: 0
iterations: 5
funcCount: 7
algorithm: 'bisection, interpolation'
message: 'Zero found in the interval [-1, 1]'
结果相同。这次,fzero 使用的函数计算次数少得多。
将单精度结果与使用标准双精度数据的相同计算进行比较。
x0 = [-1,1]; [x3,fval3,exitflag3,output3] = fzero(f,x0)
x3 = 0.5493
fval3 = -2.2204e-16
exitflag3 = 1
output3 = struct with fields:
intervaliterations: 0
iterations: 7
funcCount: 9
algorithm: 'bisection, interpolation'
message: 'Zero found in the interval [-1, 1]'
使用双精度数据导致 fzero 需要多进行几次函数计算。但结果的精度要高得多,函数值的数量级为 2e–16,而单精度值的函数值数量级为 1e–7。
求函数 f(x) = x3 – 2x – 5. 的零值
首先,编写一个名为 f.m 的文件。
function y = f(x)
y = x.^3 - 2*x - 5;将 f.m 保存到 MATLAB® 路径中。
求 f(x) 在 2 附近的零点。
fun = @f; % function x0 = 2; % initial point z = fzero(fun,x0)
z =
2.0946因为 f(x) 是一个多项式,所以可以使用 roots 命令求出相同的实数零点和一对复共轭零点。
roots([1 0 -2 -5])
ans = 2.0946 -1.0473 + 1.1359i -1.0473 - 1.1359i
求具有额外参数的函数的根。
myfun = @(x,c) cos(c*x); % parameterized function c = 2; % parameter fun = @(x) myfun(x,c); % function of x alone x = fzero(fun,0.1)
x = 0.7854
通过设置一些绘图函数绘制求解过程。
定义函数和初始点。
fun = @(x)sin(cosh(x)); x0 = 1;
通过设置包括绘图函数的选项检查求解过程。
options = optimset(PlotFcns={@optimplotx,@optimplotfval});运行 fzero,且含带 options 参数。
x = fzero(fun,x0,options)

x = 1.8115
对问题结构体定义的问题求解。
定义对求根问题编码的结构体。
problem.objective = @(x)sin(cosh(x)); problem.x0 = 1; problem.solver = 'fzero'; % a required part of the structure problem.options = optimset(@fzero); % default options
求解。
x = fzero(problem)
x = 1.8115
求出 exp(-exp(-x)) = x 处的点,并显示有关求解过程的信息。
fun = @(x) exp(-exp(-x)) - x; % function x0 = [0 1]; % initial interval options = optimset(Display="iter"); % show iterations [x fval exitflag output] = fzero(fun,x0,options)
Func-count x f(x) Procedure
2 1 -0.307799 initial
3 0.544459 0.0153522 interpolation
4 0.566101 0.00070708 interpolation
5 0.567143 -1.40255e-08 interpolation
6 0.567143 1.50013e-12 interpolation
7 0.567143 0 interpolation
Zero found in the interval [0, 1]
x = 0.5671
fval = 0
exitflag = 1
output = struct with fields:
intervaliterations: 0
iterations: 5
funcCount: 7
algorithm: 'bisection, interpolation'
message: 'Zero found in the interval [0, 1]'
正如所期望的那样,fval = 0 表示 fun(x) = 0。
输入参数
要求解的函数,指定为标量值函数的句柄或此类函数的名称。fun 接受标量 x 并返回标量 fun(x)。
fzero 对 fun(x) = 0 求解。要对方程 fun(x) = c(x) 求解,请改为对 fun2(x) = fun(x) - c(x) = 0 求解。
要在函数中包括额外参数,请参阅示例具有额外参数的函数的根和参数化函数部分。
示例: "sin"
示例: @myFunction
示例: @(x)(x-a)^5 - 3*x + a - 1
数据类型: char | function_handle | string
初始值,指定为实数标量或二元素实数向量。
标量 -
fzero从x0开始并尝试找到fun(x1)具有相反符号fun(x0)的点x1。随后fzero迭代收缩fun变号的区间以得到一个解。二元素向量 -
fzero检查fun(x0(1))和fun(x0(2))的符号是否相反,如果不相反,则返回错误。它随后迭代收缩fun变号的区间以得到一个解。区间x0必须是有限的;它不能包含 ±Inf。
提示
用区间(含有两个元素的 x0)调用 fzero 通常比用标量 x0 调用速度更快。
提示
如果 fzero 在 x0 为标量时未发现符号变化,并且您可以定位一个点 x1,在该点上 fun(x1) 的符号与 fun(x0) 相反,则将向量 [x0,x1] 作为初始值传递。
示例: 3
示例: [2,17]
数据类型: single | double
求解过程的选项,指定为结构体。使用 optimset 创建或修改 options 结构体。fzero 使用下列 options 结构体字段。
| 显示级别:
|
| 检查目标函数值是否有效。
|
| 以函数句柄或函数句柄的元胞数组的形式来指定优化函数在每次迭代时调用的一个或多个用户定义函数。默认值是“无”( |
| 绘制算法执行过程中的各个进度测量值。从预定义绘图中选择,或者自行编写。传递函数名称、函数句柄或者函数名称或句柄的元胞数组。默认值是“无”(
有关编写自定义绘图函数的信息,请参阅优化求解器绘制函数。 |
| 关于正标量 |
示例: options = optimset("FunValCheck","on")
数据类型: struct
输出参量
根或变号位置,以标量形式返回。
x 处的函数值,以标量形式返回。
对退出条件编码的整数,表示 fzero 停止其迭代的原因。
| 函数收敛于解 |
| 算法由输出函数或绘图函数终止。 |
| 在搜索含有变号的区间时遇到 |
-4 | 在搜索含有变号的区间时遇到复数函数值。 |
-5 | 算法可能收敛于一个奇异点。 |
-6 |
|
有关求根过程的信息,以结构体形式返回。结构体字段有:
intervaliterations | 求包含根的区间所采取的迭代次数 |
iterations | 求零点迭代次数 |
funcCount | 函数计算次数 |
algorithm |
|
message | 退出消息 |
算法
fzero 命令是一个函数文件。此算法由 T. Dekker 创建,它结合使用了二分法、正割法和逆二次插值方法。[1] 中给出了 Algol 60 版及一些改进。[2] 中给出了 fzero 所基于的 Fortran 版本。
替代功能
App
优化实时编辑器任务为 fzero 提供可视化界面。
参考
[1] Brent, R., Algorithms for Minimization Without Derivatives, Prentice-Hall, 1973.
[2] Forsythe, G. E., M. A. Malcolm, and C. B. Moler, Computer Methods for Mathematical Computations, Prentice-Hall, 1976.
扩展功能
对于 C/C++ 代码生成:
fun输入参量必须是函数句柄,不能是结构体和字符向量。fzero忽略除TolX和FunValCheck之外的所有选项。fzero不支持第四个输出参量, 即输出结构体。所有数据都必须为双精度。
请参阅“C/C++ 代码生成”部分中的用法说明和限制。同样的用法说明和限制也适用于 GPU 代码生成。
fzero 函数完全支持基于线程的环境。有关详细信息,请参阅在基于线程的环境中运行 MATLAB 函数。
版本历史记录
在 R2006a 之前推出fzero 现在接受单精度参量 x0 和单精度函数值 fun(x)。默认终止容差 TolX 会根据 single 类型的数据自动调整。
优化实时编辑器任务不支持单精度数据。在 fzero 中代码生成不支持单精度数据。
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)