FindRoot
更多信息和选项
- 如果变量的初始值是以列表形式给出,变量值采用相同维数的列表.
- FindRoot 返回 x,y,… ,的替换列表,这和从 Solve 获得的形式相同.
- FindRoot 首先局部化所有变量值,然后在变量保持符号形式的情况下计算 f,然后以数值法重复计算结果.
- FindRoot 具有属性 HoldAll,实际上用 Block 局部化变量.
- FindRoot[lhs==rhs,{x,x0,x1}] 用 x0 和 x1 作为 x 的前 2 个值进行求解,避免使用导数.
- FindRoot[lhs==rhs,{x,xstart,xmin,xmax}] 搜索一个解,如果 x 超出了 xmin 到 xmax 的范围,就停止搜索.
- 如果用户只指定了 x 的一个初值,FindRoot 就用牛顿法求解. 如果用户指定两个初值,FindRoot 会采用割线法的变体来求解.
- 如果所有方程和初值都是实数,则 FindRoot 仅搜索实数根. 如果出现复数,则还要搜索复数根.
- 可以通过对初值加上 0.I,使 FindRoot 搜索复数根.
- 可以给出下列选项:
-
AccuracyGoal Automatic 搜索的准确度 EvaluationMonitor None 计算方程时要计算的表达式 Jacobian Automatic 方程组的 Jacobian MaxIterations 100 最大迭代次数
Automatic 使用的方法 PrecisionGoal Automatic 搜索精度 StepMonitor None 每个步骤计算的表达式 WorkingPrecision MachinePrecision 内部计算的精度 - AccuracyGoal 和 PrecisionGoal 的默认设置是 WorkingPrecision/2.
- AccuracyGoal 的设置指定了搜索根的位置和计算函数在根处的值时要达到多少位的准确度.
- PrecisionGoal 的设置指定了搜索根的位置时要采用多少位的精度.
- FindRoot 会持续搜索直到实现 AccuracyGoal 或 PrecisionGoal 指定的目标中的任何一个.
- 如果 FindRoot 在 MaxIterations 步骤内,没有搜索到指定准确度的解,它将返回最后找到的解的近似值. 可用这个近似值为起点,再次应用 FindRoot.
范例
打开所有单元 关闭所有单元基本范例 (3)
范围 (4)
FindRoot[{y == Exp[x], x + y == 2}, {{x, 1}, {y, 1}}]FindRoot[{Sin[x + y], Cos[x - y], x ^ 2 + y ^ 2 - z}, {{x, 1}, {y, 0}, {z, 0}}]FindRoot[Zeta[z], {z, 1 / 2 + 14 I}]如果输入为实数,而函数为复变函数,则一个实数初值可能会给出一个复数结果:
FindRoot[(Cos[z + I] - 2)(z + 2), {z, 1}]FindRoot[(Cos[z + I] - 2)(z + 2), {z, -1}]推广和延伸 (1)
选项 (10)
AccuracyGoal (1)
10 - x /. FindRoot[Sin[x - 10] - x + 10, {x, 0}]10 - x /. FindRoot[Sin[x - 10] - x + 10, {x, 0}, AccuracyGoal -> 4, PrecisionGoal -> 4]10 - x /. FindRoot[Sin[x - 10] - x + 10, {x, 0}, AccuracyGoal -> Infinity, PrecisionGoal -> 8]DampingFactor (1)
EvaluationMonitor (1)
EvaluationMonitor 可用来跟踪所用的函数计算:
{res, {evx}} = Reap[FindRoot[x ^ 2 == Exp[x], {x, 0}, EvaluationMonitor :> Sow[x]]]Show[Plot[{x ^ 2, Exp[x]}, {x, -1, 0}], ListPlot[{Transpose[{evx, evx ^ 2}], Transpose[{evx, Exp[evx]}]}]]Jacobian (1)
n = 1000;
A = SparseArray[{{i_, i_} -> -2., {i_, j_} /; Abs[i - j] == 1 -> 1.}, {n, n}, 0.];
blackbox[x_ ? VectorQ] := n ^ 2 A.x + x - x ^ 3 - 1;J[x_ ? VectorQ] := Module[{d = 1 - 3 x ^ 2}, n ^ 2 A + SparseArray[{i_, i_} :> d[[i]], {n, n}]]sc = ConstantArray[0., n];
Block[{e = 0}, Timing[FindRoot[blackbox[x], {x, sc}, Jacobian -> J[x], EvaluationMonitor :> e++];e]]如果没有指定 Jacobian,要进行额外运算来计算有限差分:
Block[{e = 0}, Timing[FindRoot[blackbox[x], {x, sc}, EvaluationMonitor :> e++];e]]sp = SparseArray[{{i_, j_} /; Abs[i - j] ≤ 1 -> _}, {n, n}]Block[{e = 0}, Timing[FindRoot[blackbox[x], {x, sc}, Jacobian -> {"FiniteDifference", Sparse -> sp}, EvaluationMonitor :> e++];e]]Block[{j = 0}, FindRoot[blackbox[x], {x, sc}, Jacobian -> {J[x], EvaluationMonitor :> j++}];"Jacobian Evaluations" -> j]Block[{j = 0}, FindRoot[blackbox[x], {x, sc},
Method -> "AffineCovariantNewton", Jacobian -> {J[x], EvaluationMonitor :> j++}];"Jacobian Evaluations" -> j]MaxIterations (1)
Method (2)
方法选项在无约束条件优化中也有解释.
f[x_] = ArcTan[1000 Cos[x]];
Plot[f[x], {x, 0, 12}, Exclusions -> None]定义一个函数,监视 FindRoot 所用的步骤和计算:
monitoredFindRoot[args__] := Module[{s = 0, e = 0, j = 0},
{FindRoot[args, StepMonitor :> s++, EvaluationMonitor :> e++, Jacobian -> {Automatic, EvaluationMonitor :> j++}], "Steps" -> s, "Evaluations" -> e, "Jacobian Evaluations" -> j}]monitoredFindRoot[ArcTan[1000Cos[x]], {x, 1}]monitoredFindRoot[ArcTan[1000Cos[x]], {x, 1, 2}, Method -> "Brent"]monitoredFindRoot[ArcTan[1000Cos[x]], {x, 1, 2}, Method -> "Secant"]monitoredFindRoot[ArcTan[1000Cos[x]], {x, 1}, Method -> "AffineCovariantNewton"]PrecisionGoal (1)
10 - x /. FindRoot[Sin[x - 10] - x + 10, {x, 0}]10 - x /. FindRoot[Sin[x - 10] - x + 10, {x, 0}, AccuracyGoal -> 4, PrecisionGoal -> 4]10 - x /. FindRoot[Sin[x - 10] - x + 10, {x, 0}, AccuracyGoal -> Infinity, PrecisionGoal -> 8]StepMonitor (1)
f[x_, y_] = {x - 1, 10(y - Cos[2x] + 1)};{res, {stxy}} = Reap[FindRoot[f[x, y], {{x, -1}, {y, -1}}, StepMonitor :> Sow[{x, y}]]]ContourPlot[f[x, y].f[x, y], {x, -1, 1}, {y, -2, 1}, Epilog -> {Red, Map[Point, stxy]}]显示步骤 (红色) 和计算 (绿色). 一个步骤可能需要几次计算:
{res, {exy}} = Reap[FindRoot[f[x, y], {{x, -1}, {y, -1}}, EvaluationMonitor :> Sow[{x, y}]]];ContourPlot[f[x, y].f[x, y], {x, -1, 1}, {y, -2, 1}, Epilog -> {{PointSize[0.04], Red, Map[Point, stxy]}, {Green, Map[Point, exy]}}]WorkingPrecision (1)
FindRoot[Cos[x ^ 2] - x, {x, 1}, WorkingPrecision -> 100]开始时采用机器精度,然后以自适应方式将精度调整到 100 进行求解:
Block[{prec = MachinePrecision}, FindRoot[Cos[x ^ 2] - x, {x, 1.0}, WorkingPrecision -> 100, StepMonitor :> If[Precision[x] ≠ prec, prec = Precision[x];Print["Increased precision to ", prec, " at x = ", x];]]]应用 (3)
计算反函数 (1)
inv[f_, s_] := Function[{t}, s /. FindRoot[f - t, {s, 1}]]einv = inv[Exp[x], x]这非常接近内置的 Log 函数:
Plot[einv[x] - Log[x], {x, 0, 1}, PlotRange -> All]f[a_ ? NumberQ] := Module[{p = 0}, NDSolve[{x''[t] + x[t] + x[t] ^ 3 == 0, x[0] == a, x'[0] == 0}, x, {t, ∞}, Method -> {"EventLocator", "Event" -> x[t], "EventAction" :> Throw[p = t, "StopIntegration"]}];
4p];Plot[f[a], {a, 0, 4}]finv = inv[f[a], a]Plot[finv[s], {s, 3, 6}]求边界值问题 (2)
x1[s_ ? NumberQ] := First[x[1] /. NDSolve[{x''[t] + x'[t] + x[t] ^ 3 == Sin[4 t], x[0] == 0, x'[0] == s}, x, {t, 0, 1}]]Plot[x1[s], {s, -10, 10}]FindRoot[x1[s], {s, -1, 0}]sol = NDSolve[{x''[t] + x'[t] + x[t] ^ 3 == Sin[4t], x[0] == 0, x'[0] == s} /. %, x, {t, 0, 1}]Plot[x[t] /. sol, {t, 0, 1}]n = 100;f[{u_, v_}] := {v, (1 - u - u ^ 3) / ϵ};eqns = Flatten[Join[{Subscript[u, 0] == 0, Subscript[u, n] == 0}, Table[Thread[{Subscript[u, i], Subscript[v, i]} == {Subscript[u, i - 1], Subscript[v, i - 1]} + (1/2 n)(f[{Subscript[u, i - 1], Subscript[v, i - 1]}] + f[{Subscript[u, i], Subscript[v, i]}])], {i, 1, n}]]];sv = Flatten[Table[{{Subscript[u, i], 0}, {Subscript[v, i], 0}}, {i, 0, n}], 1];froot = FindRoot[eqns /. ϵ -> 0.01, sv];
sol = Table[Subscript[u, i], {i, 0, n}] /. froot;
ListLinePlot[sol]属性和关系 (2)
对一个多项式方程组,NSolve 求所有解,FindRoot 求一个解:
polysys = Table[Total[RandomInteger[{-9, 9}, 5]RandomChoice[{x, y, z}, 5] ^ RandomInteger[{0, 3}, 5]] == 0, {3}]FindRoot 用迭代法求单个解:
FindRoot[polysys, Transpose[{{x, y, z}, RandomComplex[1 + I, 3]}]]NSolve 直接求出所有解:
NSolve[polysys, {x, y, z}]对含有参数或有精确解的方程,用 Solve、Reduce 或 FindInstance:
eq = x Exp[2x + a] + 3 == 0;Solve 将返回某些解:
Solve[eq, x]Reduce 列举所有解:
Reduce[eq, x]FindInstance 求特定实例:
FindInstance[eq, {x, a}, 2]可能存在的问题 (3)
FindRoot[Zeta[1 / 2 + I t], {t, 1}]FindRoot[Abs[Zeta[1 / 2 + I t]], {t, 1}]f[x_] := Nest[Sin[# + Sin[2#]]&, x, 20]FindRoot[f[x], {x, 0}]//Timingg[x_ ? NumericQ] := Nest[Sin[# + Sin[2#]]&, x, 20]FindRoot[g[x], {x, 0}]//Timing过度限制搜索范围可能会有问题。从 1/2 开始,在 [0,1] 范围内求非线性方程组(由两个方程组成)的解:
FindRoot[{y == Exp[x], x + y == 2}, {{x, 1 / 2, 0, 1}, {y, 1 / 2, 0, 1}}]相关指南
-
▪
- 方程求解 ▪
- 逆函数 ▪
- 代码编译 ▪
- 多项式方程 ▪
- 符号向量、矩阵和数组
历史
1988年引入 (1.0) | 在以下年份被更新:2003 (5.0)
文本
Wolfram Research (1988),FindRoot,Wolfram 语言函数,https://reference.wolfram.com/language/ref/FindRoot.html (更新于 2003 年).
CMS
Wolfram 语言. 1988. "FindRoot." Wolfram 语言与系统参考资料中心. Wolfram Research. 最新版本 2003. https://reference.wolfram.com/language/ref/FindRoot.html.
APA
Wolfram 语言. (1988). FindRoot. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/FindRoot.html 年
BibTeX
@misc{reference.wolfram_2026_findroot, author="Wolfram Research", title="{FindRoot}", year="2003", howpublished="\url{https://reference.wolfram.com/language/ref/FindRoot.html}", note=[Accessed: 07-November-2026]}
BibLaTeX
@online{reference.wolfram_2026_findroot, organization={Wolfram Research}, title={FindRoot}, year={2003}, url={https://reference.wolfram.com/language/ref/FindRoot.html}, note=[Accessed: 07-November-2026]}