NSolve
更多信息和选项
- expr 可以为下述形式的任意逻辑组合:
-
lhs==rhs 方程 lhs!=rhs 不等方程 lhs>rhs 或 lhs>=rhs 不等式 expr∈dom 定义域说明 {x,y,…}∈reg 区域指定 ForAll[x,cond,expr] 全称量词 Exists[x,cond,expr] 存在量词 - NSolve[{expr1,expr2,…},vars] 等价于 NSolve[expr1&&expr2&&…,vars].
- 可以指定一个单变量或一个变量列表.
- NSolve 以如下规则形式给出解:
-
{} 没有解 {{x->solx,y->soly,…},…} 几个解 {{}} 解集是全维度的 - 当指定了一个单变量且一个方程的一个特定根具有大于1的重数时,NSolve 给出对应解的多个复件.
- NSolve[expr,vars] 缺省假设以代数形式出现在不等式中的量为实数,而其它所有量为复数.
- 在 NSolve[expr,vars,Reals] 中,所有变量、参数、常数以及函数值被限定为实数.
- NSolve[expr&&vars∈Reals,vars,Complexes] 求解得到的是变量的实数值,但函数值可以为复数.
- NSolve[…,x∈reg,Reals] 把 x 约束在区域 reg 内. x 的不同坐标可以使用 Indexed[x,i] 指代.
- NSolve 处理的主要是线性和多项式方程.
- 可以给出以下选项:
-
MaxRoots Automatic 返回的最大根数 Method Automatic 应该使用何种方法 RandomSeeding 1234 伪随机发生器的种子 VerifySolutions Automatic 是否验证解 WorkingPrecision Automatic 计算所用精度 - 可能的 Method 设置包括 "EndomorphismMatrix"、"Homotopy"、"Monodromy" 和 "Symbolic". »
范例
打开所有单元 关闭所有单元基本范例 (6)
NSolve[x ^ 5 - 2x + 3 == 0, x]NSolve[x ^ 5 - 2x + 3 == 0, x, Reals]NSolve[x ^ 1000000 - 2x + 3 == 0, x, MaxRoots -> 3]NSolve[{x ^ 2 + y ^ 3 == 1, 2x + 3y == 4}, {x, y}]NSolve[{x ^ 2 + y ^ 3 == 1, 2x + 3y == 4}, {x, y}, Reals]NSolve[{x, y}∈InfiniteLine[{{0, 0}, {2, 1}}] && {x, y}∈Circle[], {x, y}]Graphics[{{Blue, InfiniteLine[{{0, 0}, {2, 1}}], Circle[]}, {Red, Point[{x, y}] /. %}}]范围 (48)
单变量复数方程 (10)
NSolve[x ^ 5 - 2x + 17 == 0, x]NSolve[x ^ 3 - 1.234x + 5.678 == 0, x]NSolve[(x ^ 2 - 1)(x ^ 4 - 1) == 0, x]NSolve[x ^ 1234567 + 9x ^ 2 + 7x - 1 == 0, x, MaxRoots -> 5]NSolve[Sqrt[x] + 3x ^ (1 / 3) == 5, x]NSolve[Sin[x] == 1 / 3, x]NSolve[Sin[x] == 1 / 3, x, MaxRoots -> Infinity]NSolve[5 == x 2 ^ (x ^ 2), x, MaxRoots -> 3]NSolve[Sin[E ^ x] - Cos[2 x] == 1 && -1 ≤ Re[x] ≤ 1 && -1 ≤ Im[x] ≤ 1, x]NSolve[Gamma[x] - Log[x] == I / 2 && Abs[x - 2] < 3 / 2, x]NSolve[Cos[Exp[x]] == 3 Exp[-x] + 1 && 0 ≤ Re[x] ≤ 1, x]NSolve[Cos[Exp[x]] == 3 Exp[-x] + 1 && 0 ≤ Re[x] ≤ 1, x, MaxRoots -> Infinity]NSolve[BesselJ[2, x]x - 2 ^ x == 3, x]多变量复数方程组 (9)
NSolve[{x + 2y + 3z == 4, 3x + 4y + 5z == 6, 7x + 9y + 8z == 10}, {x, y, z}]NSolve[1.23 x + 2.34 y + 3.45 z == 4.56 && 5.67x + 6.78y + 7.89z == 8.9 && 9.01x + 0.12y + 1.23z == 2.34, {x, y, z}]NSolve[x + 2y + 3z == 4 && 3x + 4y + 5z == 6 && 6x + 7y + 8z == 9, {x, y, z}]NSolve[x + 2y + 3z == 4 && 3x + 4y + 5z == 6 && 6x + 7y + 8z == 0, {x, y, z}]NSolve[x ^ 2 + y ^ 2 == 1 && x ^ 3 + 2y ^ 3 == 3, {x, y}]NSolve[x ^ 10000 == y ^ 2 + 2y + 1 && y ^ 10000 == z ^ 2 + 2z + 1 && z ^ 10000 == x ^ 2 + 2 x + 1, {x, y, z}, MaxRoots -> 5]NSolve[x ^ 2 + y ^ 2 + z ^ 2 == 1 && x y z == 2, {x, y, z}]NSolve[Sqrt[x y] == Sqrt[x + y] && Sqrt[x] - y ^ (1 / 3) == 1, {x, y}]NSolve[Sin[x + y] == x y + 1 && Cos[x - y] == AiryAi[x y] + 2, {x, y}]NSolve[3 ^ x ^ 2 == 7 && x ^ 2 - y ^ 3 == 4, {x, y}, MaxRoots -> 5]单变量实数方程 (11)
NSolve[x ^ 5 - 2x + 1 == 0, x, Reals]NSolve[(x ^ 2 - 1)(x ^ 4 - 1) == 0, x, Reals]NSolve[Sqrt[x] + 3x ^ (1 / 3) == 5, x, Reals]NSolve[Abs[x] ^ 2 - x + UnitStep[x] == 9, x, Reals]NSolve[E ^ x - x == 7, x, Reals]NSolve[ 27^2x - 1^(1/(x)) == Sqrt[9^2x - 1], x, Reals]NSolve[AiryBi[1 - x ^ 2] == 0 && 2 < x < 3, x, Reals]NSolve[900 < AiryAiZero[2t + 1] ^ 2 < 1000, t, Reals]NSolve[E ^ (2E ^ x) - Log[x ^ 2 + 1] - 20x == 11, x, Reals]NSolve[x ^ 1000000 - 2x ^ 777777 + 3x ^ 12345 + 9x ^ 67 - 10 == 0, x, Reals]NSolve[2x ^ (123451 / 67890) - x ^ 2 + 4Sqrt[x] - 4x - 9 / 8 == 0, x, Reals]NSolve[x ^ Pi - x ^ x ^ Sqrt[2] - Sqrt[3]x + 2 ^ (1 / 3) == 0, x, Reals]NSolve[10Sin[Tan[E ^ -x ^ 2]] - x == 3, x, Reals]NSolve[2Sin[Exp[x]] - Cos[Pi x] == 3 / 2 && -1 < x < 1, x, Reals]NSolve[Cos[x] - BesselJ[5, x] == 1 / 2 && 0 ≤ x ≤ 10, x]多变量实数方程组和不等式组 (9)
NSolve[2 x + 3y - 5z == 1 && 3x - 4y + 7z == 3 && x + y - z == 8, {x, y, z}, Reals]NSolve[x y == z ^ 2 - x && x y z == 2 && x ^ 2 + y ^ 2 + z ^ 2 == 5, {x, y, z}, Reals]NSolve[Exists[x, x ^ 2 + a x + b == 0 && 2 x + a == 0 && a ^ 2 + x b ^ 2 == 1], {a, b}, Reals]NSolve[ForAll[x, Exists[y, a x ^ 2 + b y ^ 2 - 3y == 1 && y < 0 && a y - y == b + 1]], {a, b}, Reals]NSolve[Sqrt[x + 2y] - 3x + 4y == 5 && x + y ^ (1 / 3) == 2, {x, y}, Reals]NSolve[Max[x ^ 2, 1 - y ^ 3] == Min[y ^ 2, x] + 7y && x + UnitStep[4 - y] == 5, {x, y}, Reals]NSolve[Sin[x + y] == 1 / 2 && E ^ x - y == 1 && x ^ 2 ≤ 10, {x, y}, Reals]NSolve[ 3^x - 2^2y == 77 && Sqrt[3^x] - 2^y == 7, {x, y}, Reals]NSolve[E ^ x y ^ 3 + Log[x]y == 1 && x y + E ^ x / x == 10, {x, y}, Reals]NSolve[Exists[a, a x ^ 2 + Sinh[x ^ 2 + 1]a ^ 2 == 1 && x ^ 2 - a ^ 2 == 1], x, Reals]NSolve[Sin[x - Cos[x]] y ^ 3 - x == 1 && x ^ 2 + y ^ 2 == 1, {x, y}, Reals]NSolve[Exists[y, y ^ 3 - Cos[x] y + 2 x ^ 2 Sin[x ^ 2 - 1] == 0 && x ^ 2 + y ^ 2 == 2], x, Reals]第一个变量为正则有界形式、其它变量为多项式形式的方程及不等式组:
NSolve[y ^ 3 - BesselJ[2, x + 2] y - y - 3 x == -2 && x ^ 2 ≤ 1 && x y - x ^ 2 + y ^ 2 == 3, {x, y}, Reals]NSolve[Exists[y, y ^ 4 - Gamma[x + 2] y - y - 3 ArcSin[x / 3] == 1 && x ^ 2 + y ^ 3 == 1 && 0 < x < 2], x, Reals]变量域混合的方程及不等式组 (1)
几何区域 (8)
Subscript[ℛ, 1] = Circle[];
Subscript[ℛ, 2] = Line[{{-2, 1}, {1, -2}}];NSolve[{x, y}∈Subscript[ℛ, 1] && {x, y}∈Subscript[ℛ, 2], {x, y}]Graphics[{{Blue, Subscript[ℛ, 1], Subscript[ℛ, 2]}, {Red, Point[{x, y}] /. %}}]Subscript[ℛ, 1] = Sphere[];
Subscript[ℛ, 2] = InfinitePlane[{{0, 0, 0}, {0, 1, 0}, {1, 0, 1}}];NSolve[2 x y == z^2 && {x, y, z}∈Subscript[ℛ, 1] && {x, y, z}∈Subscript[ℛ, 2], {x, y, z}, Reals]Show[{ContourPlot3D[2 x y == z^2, {x, -1.2, 1.2}, {y, -1.2, 1.2}, {z, -1.2, 1.2}, Mesh -> None, ContourStyle -> Opacity[0.5]], Graphics3D[{{Opacity[0.5], Green, Subscript[ℛ, 1]}, {Opacity[0.5], Yellow, Subscript[ℛ, 2]}, {PointSize[Large], Red, Point[{x, y, z} /. %]}}]}]ℛ = ImplicitRegion[a + 2 b - 3 c ≥ 1 && a b c == 7, {a, b, c}];NSolve[y^2 + x z == 1 && x + y == z ^ 2 && {x, y, z}∈ℛ, {x, y, z}, Reals]ℛ = ParametricRegion[{s + t, s - t, s t}, {s, t}];NSolve[x y == z && x + 2 y + 3 z == 1 && {x, y, z}∈ℛ, {x, y, z}, Reals]Subscript[ℛ, 1] = Disk[{0, 0}, 2];
Subscript[ℛ, 2] = Circle[{1, 1}, 2];
Subscript[ℛ, 3] = RegionIntersection[Subscript[ℛ, 1], Subscript[ℛ, 2]];NSolve[x^2 == x y + 1 && {x, y}∈Subscript[ℛ, 3], {x, y}, Reals]Show[{ContourPlot[x^2 == x y + 1, {x, -2, 3}, {y, -2, 3}], Graphics[{{Opacity[0.5], Yellow, Subscript[ℛ, 1]}, {Green, Subscript[ℛ, 2]}, {Red, Point[{x, y} /. %]}}]}]Subscript[ℛ, 1] = Circle[];
Subscript[ℛ, 2] = InfiniteLine[{{-2, 0}, {0, t}}];
Subscript[ℛ, 3] = Circle[{1, 1}, {2t, t}];
Subscript[ℛ, 4] = RegionIntersection[Subscript[ℛ, 1], Subscript[ℛ, 2], Subscript[ℛ, 3]];NSolve[{x, y}∈Subscript[ℛ, 4], {t, x, y}, Reals]Graphics[{{Blue, Subscript[ℛ, 1], Subscript[ℛ, 2], Subscript[ℛ, 3]}, {Red, Point[{x, y}]}} /. %]Subscript[ℛ, 1] = Circle[{a, b}, r];
Subscript[ℛ, 2] = Circle[{a + 1, b}, r];
Subscript[ℛ, 3] = RegionIntersection[Subscript[ℛ, 1], Subscript[ℛ, 2]];NSolve[({0, 1} | {0, -1})∈Subscript[ℛ, 3], {a, b, r}, Reals]Show[{Graphics[{{Blue, Subscript[ℛ, 1]}, {Green, Subscript[ℛ, 2]}, {Red, Point[{{0, 1}, {0, -1}}]}} /. %]}]ℛ = RegionIntersection[Circle[], Line[{{-2, -1}, {1, 2}}]];NSolve[x∈ℛ, x]ℛ = Sphere[];NSolve[x.{1, 2, 3} == 0 && x.{-3, -2, -1} == 0 && x∈ℛ, x]推广和延伸 (1)
选项 (10)
MaxRoots (8)
NSolve[x ^ 12345 + x + 1 == 0, x, MaxRoots -> 3]NSolve[x ^ 1000 == y + 1 && y ^ 1000 == z + 1 && z ^ 1000 == x + 1, {x, y, z}, MaxRoots -> 3]NSolve[Sin[x + y] == 2 && Cos[x - y] == 3, {x, y}, MaxRoots -> 5]在默认的 Automatic 设置下,NSolve 可能无法给出所有解:
NSolve[Sin[x] == 1 / 2, x]设置 MaxRootsInfinity,NSolve 会尝试求所有解:
NSolve[Sin[x] == 1 / 2, x, MaxRoots -> Infinity]NSolve[x ^ 2 + 2y ^ 2 == 3 && x ^ 3 - 4x y == 5, {x, y}]NSolve[x ^ 2 + 2y ^ 2 == 3 && x ^ 3 - 4x y == 5, {x, y}, Method -> "EndomorphismMatrix"]NSolve[x ^ 2 + 2y ^ 2 == 3 && x ^ 3 - 4x y == 5, {x, y}, Method -> "Homotopy"]NSolve[x ^ 2 + 2y ^ 2 == 3 && x ^ 3 - 4x y == 5, {x, y}, Method -> "Monodromy"]NSolve[x ^ 2 + 2y ^ 2 == 3 && x ^ 3 - 4x y == 5, {x, y}, Method -> "Symbolic"]该方程组有
个根,严格小于 Bernstein-Khovanskii-Kushnirenko 定理提供的
根的界限:
syst = t1^2 + t2^2 + t3^2 - 12t1 - 68 == 0 && t4^2 + t5^2 + t6^2 - 12t5 - 68 == 0 && t7^2 + t8^2 + t9^2 - 24 t8 - 12t9 + 100 == 0 && t1 t4 + t2 t5 + t3 t6 - 6 t1 - 6t5 - 52 == 0 && t1 t7 + t2 t8 + t3 t9 - 6 t1 - 12t8 - 6t9 + 64 == 0 && t4 t7 + t5 t8 + t6 t9 - 6t5 - 12t8 - 6t9 + 32 == 0 && 2t2 + 2t3 - t4 - t5 - 2 t6 - t7 - t9 + 18 == 0 && t1 + t2 + 2t3 + 2t4 + 2t6 - 2t7 + t8 - t9 - 38 == 0 && t1 + t3 - 2t4 + t5 - t6 + 2t7 - 2t8 + 8 == 0;
vars = {t1, t2, t3, t4, t5, t6, t7, t8, t9};默认使用的 "Homotopy" 方法会返回某些根的多个重复副本:
(sols = NSolve[syst, vars])//Length//AbsoluteTimingUnion[sols, SameTest -> (Norm[(vars /. #1) - (vars /. #2)] < 10 ^ -6&)]//Length"Monodromy" 方法在这里运行得更快,而且不会产生根的重复副本:
NSolve[syst, vars, Method -> "Monodromy"]//Length//AbsoluteTimingNSolve[Sin[x + y] + Cos[x - y] == 1 && Sin[x - y] + Cos[x + y] == 2, {x, y}, Method -> "Monodromy"]NSolve[Sin[x + y] + Cos[x - y] == 1 && Sin[x - y] + Cos[x + y] == 2, {x, y}, Method -> "Symbolic"]Method 选项也可用于本地设置 "NSolveOptions" 组中的系统选项:
"NSolveOptions" /. SystemOptions[]缺省时,NSolve 为欠定复数方程组引入分层超平面:
NSolve[x ^ 2 + y ^ 2 == 1, {x, y}]当 Method->{"UseSlicingHyperplanes"->False} 时,NSolve 给出参数化的解:
NSolve[x ^ 2 + y ^ 2 == 1, {x, y}, Method -> {"UseSlicingHyperplanes" -> False}]VerifySolutions (1)
NSolve 合适用不等价变换获得的解:
funs = {Sqrt[x + Sqrt[x]] - 2, y ^ 9 - y - 2 x ^ (1 / 7) - 3};
eqns = And@@Thread[funs == 0];
sol1 = NSolve[eqns, {x, y}];通过 VerifySolutions->False, NSolve 不核实解:
sol2 = NSolve[eqns, {x, y}, VerifySolutions -> False];部分同 VerifySolutions->False 一起返回的解不正确:
Length /@ {sol1, sol2}sol3 = Select[sol2, Chop[funs /. #, 10 ^ -8] == {0, 0}&];sol3 === sol1WorkingPrecision (1)
缺省时,NSolve 利用机器精度计算得到确切方程的解:
NSolve[x ^ 5 + 2 x + 7 == 0, x]NSolve[x ^ 5 + 2 x + 7 == 0, x, WorkingPrecision -> 50]应用 (17)
几何 (11)
pts = NSolve[x ^ 2 + y ^ 2 == 1 && y - 2x ^ 2 + 3 / 2 == 0, {x, y}]Show[{ContourPlot[{x ^ 2 + y ^ 2 == 1, y - 2x ^ 2 + 3 / 2 == 0}, {x, -1.5, 1.5}, {y, -1.5, 1.5}], Graphics[{Red, PointSize[Medium], Point[{x, y} /. pts]}]}]求 InfiniteLine[{0,0},{1,1}] 和 InfiniteLine[{{0,1},{1,0}}] 的交集:
line1 = InfiniteLine[{0, 0}, {1, 1}];
line2 = InfiniteLine[{{0, 1}, {1, 0}}];sol = NSolve[{x, y}∈line1∧{x, y}∈line2, {x, y}]Graphics[{{Lighter[Blue, 0.5], line1, line2}, {Red, Point[{x, y} /. sol]}}, Axes -> True, ImageSize -> Small]求 InfiniteLine[{0,0},{1,1}] 和 Circle[{0,0},1] 的交集:
line = InfiniteLine[{0, 0}, {1, 1}];
circle = Circle[{0, 0}, 1];sol = NSolve[{x, y}∈line∧{x, y}∈circle, {x, y}]Graphics[{{Lighter[Blue, 0.5], line, circle}, {Red, Point[{x, y} /. sol]}}, ImageSize -> Small]lines = InfiniteLine /@ RandomReal[1, {5, 2, 2}];使用 BooleanCountingFunction 表示恰好两个条件为真:
sol = NSolve[{x, y}∈BooleanRegion[BooleanCountingFunction[{2}, 5], lines], {x, y}]Graphics[{{Lighter[Blue, 0.5], lines}, {Red, PointSize[Medium], Point[{x, y} /. sol]}}]求圆环 Circle[{1/3 Cos[k 2π/5],1/3 Sin[k 2π/5]}] 的每一对交集,其中 k=0,…,4:
circles = Table[Circle[{1 / 3 Cos[k 2π / 5], 1 / 3 Sin[k 2π / 5]}], {k, 0, 4}];sol = NSolve[{x, y}∈BooleanRegion[BooleanCountingFunction[{2}, 5], circles], {x, y}]Graphics[{{Lighter[Blue, 0.5], circles}, {Red, PointSize[Medium], Point[{x, y} /. sol]}}]求 InfiniteLine[{{-1,1,1},{1,1,1}}] 和InfinitePlane[{{2,0,0},{0,2,0},{0,0,2}}] 的交集:
line = InfiniteLine[{{-1, 1, 1}, {1, 1, 1}}];
plane = InfinitePlane[{{2, 0, 0}, {0, 2, 0}, {0, 0, 2}}];sol = NSolve[{{x, y, z}∈line, {x, y, z}∈plane}, {x, y, z}]Graphics3D[{{Opacity[0.5], line, plane}, {Red, PointSize[Large], Point[{x, y, z} /. sol]}}, Axes -> True, PlotRange -> {{-2, 2}, {-2, 2}, {-2, 2}}]求 InfiniteLine[{{-1,1,1},{1,1,1}}] 和 Sphere[{0,0,0},3] 的交集:
line = InfiniteLine[{{-1, 1, 1}, {1, 1, 1}}];
sphere = Sphere[{0, 0, 0}, 3];sol = NSolve[{{x, y, z}∈line, {x, y, z}∈sphere}, {x, y, z}]Graphics3D[{{Opacity[0.5], line, sphere}, {Red, PointSize[Large], Point[{x, y, z} /. sol]}}, PlotRangePadding -> 0.5]求 InfiniteLine[{{-1,1/3,1/2},{1,1/3,1/2}}] 和Tetrahedron[{{0,0,0},{1,0,0},{0,1,0},{0,0,1}}] 边界的交集:
line = InfiniteLine[{{-1, 1 / 3, 1 / 2}, {1, 1 / 3, 1 / 2}}];
tet = RegionBoundary@Tetrahedron[{{0, 0, 0}, {1, 0, 0}, {0, 1, 0}, {0, 0, 1}}];sol = NSolve[{x, y, z}∈RegionIntersection[line, tet], {x, y, z}]Graphics3D[{{Opacity[0.5], line, Tetrahedron[{{0, 0, 0}, {1, 0, 0}, {0, 1, 0}, {0, 0, 1}}]}, {Red, PointSize[Medium], Point[{x, y, z} /. sol]}}, PlotRangePadding -> 0.2]planes = InfinitePlane /@ RandomReal[1, {3, 3, 3}];sol = NSolve[{x, y, z}∈RegionIntersection@@planes, {x, y, z}]Graphics3D[{{Opacity[0.5], planes}, {Red, PointSize[Medium], Point[{x, y, z} /. sol]}}, PlotRangePadding -> 0.1]求球面 Sphere[{1/3 Cos[k 2π/3],1/3 Sin[k 2π/3],0}] 的叠加区域,其中 k=0,1,2:
spheres = Table[Sphere[{1 / 3 Cos[k 2π / 3], 1 / 3 Sin[k 2π / 3], 0}], {k, 0, 2}];sol = NSolve[{x, y, z}∈RegionIntersection@@spheres, {x, y, z}]Graphics3D[{{Opacity[0.5], spheres}, {Red, PointSize[Large], Point[{x, y, z} /. sol]}}]planes = InfinitePlane /@ RandomReal[{-1, 1}, {10, 3, 3}];使用 BooleanCountingFunction 查找恰好三样东西为真的条件:
sol = NSolve[{x, y, z}∈BooleanRegion[BooleanCountingFunction[{3}, 10], planes], {x, y, z}];//QuietGraphics3D[{{Opacity[0.3], planes}, {Red, PointSize[Medium], Point[{x, y, z} /. sol]}}, PlotRange -> {{-1, 1}, {-1, 1}, {-1, 1}}]化学 (1)
reactionpolys = {50 + Subscript[x, 1] - Subscript[x, 4] - (4 Subscript[x, 2] Subscript[x, 4]/125) - (Subscript[x, 2] Subscript[x, 5]/50) - (99 Subscript[x, 6]/100) + Subscript[x, 7], -(9/200) Subscript[x, 3] Subscript[x, 5] + 15 (50 - Subscript[x, 4] - Subscript[x, 6]) + Subscript[x, 8], -(4/125) Subscript[x, 2] Subscript[x, 4] - (1/50) Subscript[x, 4] (350 - Subscript[x, 2] - Subscript[x, 3] + Subscript[x, 4] + Subscript[x, 5]) + 16 (50 - Subscript[x, 4] - Subscript[x, 6]) + (101 Subscript[x, 6]/100), Subscript[x, 1] - (Subscript[x, 2] Subscript[x, 5]/50) - (9 Subscript[x, 3] Subscript[x, 5]/200) - (11 Subscript[x, 5] (350 - Subscript[x, 2] - Subscript[x, 3] + Subscript[x, 4] + Subscript[x, 5])/10000) + Subscript[x, 7] + (43/500) (100 - Subscript[x, 1] - Subscript[x, 5] - Subscript[x, 7] - Subscript[x, 8]) + Subscript[x, 8], (1/50) Subscript[x, 4] (350 - Subscript[x, 2] - Subscript[x, 3] + Subscript[x, 4] + Subscript[x, 5]) - (101 Subscript[x, 6]/100), (9 Subscript[x, 3] Subscript[x, 5]/200) - (273 Subscript[x, 8]/250), (Subscript[x, 2] Subscript[x, 5]/100) - Subscript[x, 7] + (23 Subscript[x, 8]/250), -(3 Subscript[x, 1]/2) + (Subscript[x, 2] Subscript[x, 5]/100)};
vars = Variables[reactionpolys];由于变量代表化学物质的数量,我们对所有分量均为非负的实值解感兴趣:
nonnegSolutions[polys_, vars_, meth_ : Automatic] := Module[
{solns, realsolns},
solns = vars /. NSolve[polys, vars, Method -> meth];
realsolns = Select[solns, FreeQ[#, Complex]&];
Select[realsolns, Apply[And, Map[# >= 0&, #]]&]
]nonnegsolns = nonnegSolutions[reactionpolys, vars]表观多重性是 Automatic 方法的一个伪影,该方法往往很快,但有时会夸大多重性:
nonnegSolnsE = nonnegSolutions[reactionpolys, vars, "EndomorphismMatrix"]机械 (3)
Gough-Stewart 自由度为 6 的平行平台的正运动学:
eqns = {t1^2 + t2^2 + t3^2 - 12t1 - 68, t4^2 + t5^2 + t6^2 - 12t5 - 68, t7^2 + t8^2 + t9^2 - 24 t8 - 12t9 + 100, t1 t4 + t2 t5 + t3 t6 - 6 t1 - 6t5 - 52, t1 t7 + t2 t8 + t3 t9 - 6 t1 - 12t8 - 6t9 + 64, t4 t7 + t5 t8 + t6 t9 - 6t5 - 12t8 - 6t9 + 32, 2t2 + 2t3 - t4 - t5 - 2 t6 - t7 - t9 + 18, t1 + t2 + 2t3 + 2t4 + 2t6 - 2t7 + t8 - t9 - 38, t1 + t3 - 2t4 + t5 - t6 + 2t7 - 2t8 + 8};
vars = {t1, t2, t3, t4, t5, t6, t7, t8, t9};
Length[solns = NSolve[eqns, vars]]Union[solns, SameTest -> (Norm[(vars /. #1) - (vars /. #2)] < 10 ^ -6&)]//LengthLength[NSolve[eqns, vars, Method -> "Monodromy"]]建立一个由四个变量组成的六方程超定系统,该系统源于摄像机姿态估算程序:
coords = {{1, 2, 1.49071, 4}, {1, 3, .400000, 8}, {1, 4, .894427, 4}, {2, 3, 1.49071, 4}, {2, 4, .666667, 8}, {3, 4, .894427, 4}};
vars = Array[x, 4];
polys = MapThread[x[#1] ^ 2 + x[#2] ^ 2 - #3 * x[#1] * x[#2] - #4&, Transpose[coords]]realsols = NSolve[polys[[1 ;; 4]], vars, Reals]对于每个解,插入解的前四个残差都很小,而后两个残差则不可忽略:
residuals = polys /. realsols现在,将每个解作为最小化残差平方和的起点,精细化求解每个解:
newsols = Map[FindMinimum[polys.polys, Transpose[{vars, (vars /. #)}]]&, realsols]polys = {-1 + (3 ct1/4) + (7 ct2/5) - (9 ct3/10), (1/8) + (3 st1/4) + (7 st2/5) - (9 st3/10), (3 ct1/4) + (3 ct2/5) + (st2/5) - x, -(ct2/5) + (3 st1/4) + (3 st2/5) - y, ct1 f1 + ct3 f3, -1 + f1 st1 + f3 st3, ct1 t1 - x, st1 t1 - y0, 1 + ct3 t3 - x, -(1/8) + st3 t3 - y0, -1 + ct1^2 + st1^2, -1 + ct2^2 + st2^2, -1 + ct3^2 + st3^2};
vars = Variables[polys];
Length[solns = NSolve[polys == 0, vars]]每个实值解均对应一个不同的平衡位置,共有六个这样的平衡位置:
realsols = Union[Cases[vars /. solns, {_Real..}], SameTest -> (Norm[#1 - #2] <= 10 ^ -9&)]经济 (1)
经济学中出现的简化 8 维系统;我们使用非默认方法来获得更高的精度:
eqns = {x1 + x1 * x2 + x2 * x3 + x3 * x4 + x4 * x5 + x5 * x6 + x6 * x7 - 1 * u8,
x2 + x1 * x3 + x2 * x4 + x3 * x5 + x4 * x6 + x5 * x7 - 2 * u8,
x3 + x1 * x4 + x2 * x5 + x3 * x6 + x4 * x7 - 3 * u8,
x4 + x1 * x5 + x2 * x6 + x3 * x7 - 4 * u8,
x5 + x1 * x6 + x2 * x7 - 5 * u8,
x6 + x1 * x7 - 6 * u8,
x7 - 7 * u8,
x1 + x2 + x3 + x4 + x5 + x6 + x7 + 1};
vars = {x1, x2, x3, x4, x5, x6, x7, u8};
Length[solns = NSolve[eqns, vars, Method -> "Monodromy"]]Cases[vars /. solns, {_Real..}]差分方程 (1)
des = {a[n + 1] == b[n] ^ 2 * a[n] - 3 * c[n] - 1, b[n + 1] == a[n] * c[n] ^ 2 - 2 * b[n - 1] + 1, c[n + 1] == a[n - 1] - b[n - 2]};RSolve 无法找到解的解析形式:
RSolve[des, {a, b, c}, n]rep = {a[_] :> a, b[_] :> b, c[_] :> c};
asymp = des /. repdesolns = NSolve[asymp, {a, b, c}];
Select[desolns, MatchQ[{a, b, c} /. #, {_Real..}]&]属性和关系 (8)
polys = {x ^ 2 + y ^ 2 - 1, 2x + 3y - 4};
NSolve[Thread[polys == 0], {x, y}]polys /. %NSolve 用 {} 表示空或无解:
NSolve[x == 1 && x == 2, x]NSolve 用 {{}} 表示通解或满足方程的所有点:
NSolve[x == x, x]对于单变量多项式方程,NSolve 会根据解的倍数重复求解:
NSolve[(x - 1) ^ 2(x - 2) ^ 3 == 0, x]NSolve[(x ^ 4 - 1)(x ^ 4 - 4) == 0, x]NSolve[(x ^ 4 - 1)(x ^ 4 - 4) == 0, x, Reals]NSolve 表示替换规则的解:
NSolve[{x ^ 2 + y ^ 2 == 1, x ^ 3 - y ^ 3 == 2}, {x, y}]NSolveValues 给出了解的值:
NSolveValues[{x ^ 2 + y ^ 2 == 1, x ^ 3 - y ^ 3 == 2}, {x, y}]NSolve 是一个全局方程求解器:
NSolve[{x ^ 2 + y ^ 3 == 1, x ^ 4 + y ^ 4 == 2}, {x, y}]FindRoot 是一个局部方程求解器:
FindRoot[{x ^ 2 + y ^ 3 == 1, x ^ 4 + y ^ 4 == 2}, {{x, 1}, {y, -1}}]NSolve 给出近似结果:
NSolve[{x ^ 2 + y ^ 2 == 1, x + y ^ 2 == 2}, {x, y}]使用 Solve 得到确切解:
Solve[{x ^ 2 + y ^ 2 == 1, x + y ^ 2 == 2}, {x, y}]使用 FindInstance 得到确切解的实例:
FindInstance[{x ^ 2 + y ^ 2 == 1, x + y ^ 2 == 2}, {x, y}]使用 NDSolve 对微分方程进行数值求解:
NDSolve[{y''[x] == -y[x], y[0] == 0, y'[0] == 1}, y[x], {x, 0, 7}]Plot[y[x] /. %[[1]], {x, 0, 7}]可能存在的问题 (7)
poly = Expand[Product[x - i, {i, 30}]];NSolve[poly == 0, x]更高的 WorkingPrecision 下,产生了更准确的结果:
NSolve[poly == 0, x, WorkingPrecision -> 20]eqns = {x ^ 2 + y ^ 2 == 1, x ^ 3 - 2x y == 2};
sols = NSolve[eqns, {x, y}]eqns /. solsNorm /@ ({x ^ 2 + y ^ 2 - 1, x ^ 3 - 2x y - 2} /. sols)使用较高的 WorkingPrecision 将给出公差较小的解:
sols = NSolve[eqns, {x, y}, WorkingPrecision -> 25]({x ^ 2 + y ^ 2 - 1, x ^ 3 - 2x y - 2} /. sols)如果多项式系统的解集是无限的,NSolve 会给出其与随机超平面的交集:
NSolve[x ^ 2 + y ^ 2 == 1, {x, y}]NSolve[x ^ 2 + y ^ 2 + z ^ 2 == 1, {x, y, z}]用 ContourPlot 和 ContourPlot3D 观察解的实部:
ContourPlot[x ^ 2 + y ^ 2 == 1, {x, -1, 1}, {y, -1, 1}]ContourPlot3D[x ^ 2 + y ^ 2 + z ^ 2 == 1, {x, -1, 1}, {y, -1, 1}, {z, -1, 1}]默认情况下,NSolve 可能会显示有多个解,但实际计数其实应该更小:
polys = {9 * y2 ^ 2 - 5.656854249492381 * y2 + z2, x3 ^ 2 + y3 ^ 2 + z3 ^ 2 - 1, x4 ^ 2 + y4 ^ 2 + z4 ^ 2 - 1, y5 ^ 2 + z5 ^ 2 - 0.888888888888889, x3 - 2.828427124746190 * y2 * x3 + y2 * y3 + z2 * z3 - 1 / 3, x3 * x4 + y3 * y4 + z3 * z4 - 1 / 3, 1 / 3 * x4 + y4 * y5 + z4 * z5 - 1 / 3, 8 / 3 - 2.828427124746190 * y2 + x3 + x4, y2 + y3 + y4 + y5 + 0.8888888888888889, z2 + z3 + z4 + z5};
vars = {x3, x4, y2, y3, y4, y5, z2, z3, z4, z5};
Length[solns = NSolve[polys, vars]]Length[Union[vars /. solns, SameTest -> (Norm[#1 - #2] < 10 ^ -9&)]]Length[NSolve[polys, vars, Method -> "Monodromy"]]Length[NSolve[polys, vars, Method -> "EndomorphismMatrix"]]polys = {-1 + a + b + c + d, -2772 a^2 - 4620 a b - 1980 b^2 - 3960 a c - 3465 b c - 1540 c^2 - 3465 a d - 3080 b d - 2772 c d - 1260 d^2 + 13860 r, 926640 a^3 + 2432430 a^2 b + 2116400 a b^2 + 617760 b^3 + 2230800 a^2 c + 3848130 a b c + 1684800 b^2 c + 1731600 a c^2 + 1526525 b c^2 + 462000 c^3 + 2108106 a^2 d + 3603600 a b d + 1573000 b^2 d + 3211065 a c d + 2836680 b c d + 1287000 c^2 d + 1474200 a d^2 + 1310400 b d^2 + 1192464 c d^2 + 368550 d^3 + 16216200 a r + 14414400 b r + 13513500 c r + 12972960 d r, 213840 a^2 + 374220 a b + 162800 b^2 + 343200 a c + 296010 b c + 133200 c^2 + 324324 a d + 277200 b d + 247005 c d + 113400 d^2 + 332640 a lambda + 277200 b lambda + 237600 c lambda + 207900 d lambda + 831600 mu + 1247400 r, 486486 a^2 + 846560 a b + 370656 b^2 + 769626 a c + 673920 b c + 305305 c^2 + 720720 a d + 629200 b d + 567336 c d + 262080 d^2 + 720720 a lambda + 617760 b lambda + 540540 c lambda + 480480 d lambda + 2162160 mu + 2882880 r, 2230800 a^2 + 3848130 a b + 1684800 b^2 + 3463200 a c + 3053050 b c + 1386000 c^2 + 3211065 a d + 2836680 b d + 2574000 c d + 1192464 d^2 + 3088800 a lambda + 2702700 b lambda + 2402400 c lambda + 2162160 d lambda + 10810800 mu + 13513500 r, 2108106 a^2 + 3603600 a b + 1573000 b^2 + 3211065 a c + 2836680 b c + 1287000 c^2 + 2948400 a d + 2620800 b d + 2384928 c d + 1105650 d^2 + 2702700 a lambda + 2402400 b lambda + 2162160 c lambda + 1965600 d lambda + 10810800 mu + 12972960 r};
vars = {r, a, b, c, d, lambda, mu};
Length[solmach = NSolve[polys, {r, a, b, c, d, lambda, mu}, Method -> "EndomorphismMatrix"]]Length[solbig = NSolve[polys, {r, a, b, c, d, lambda, mu}, Method -> "EndomorphismMatrix", WorkingPrecision -> 100]]Map[Max, Abs[polys /. solmach[[1 ;; 4]]]]Map[Max, Abs[polys /. solbig[[1 ;; 4]]]]尽管残差存在差异,但两个解在 MachinePrecision 的所有位数上都是一致的:
(vars /. solmach[[1 ;; 4]]) - (vars /. solbig[[1 ;; 4]])在指定域为 Reals 的情况下,NSolve 可能无法找到在解的任何邻域中函数不是实值的解:
f = (x ^ 2 - 7 x + 11) ^ (x ^ 2 - 13 x + 42) - 1;NSolve[f == 0, x, Reals]Plot[{Abs[f], Im[f]}, {x, 1.5, 7.5}, PlotLegends -> "Expressions"]NSolve[f == 0 && Element[x, Reals], x]NSolve 可能不会给出所有解:
NSolve[Cos[x] == x, x]NSolve[Cos[x] == x, x, MaxRoots -> 100]//Length巧妙范例 (1)
roots = NSolve[Sin[z + Sin[z + Sin[z]]] == Cos[z + Cos[z + Cos[z]]] && -3 < Re[z] < 3 && -3 < Im[z] < 3, z, MaxRoots -> Infinity];ListPlot[{Re[z], Im[z]} /. roots, PlotLabel -> TraditionalForm[Sin[z + Sin[z + Sin[z]]] == Cos[z + Cos[z + Cos[z]]]]]技术笔记
-
▪
- 数值运算 ▪
- 方程的数值解 ▪
- 一元方程 ▪
- Wolfram 语言数值数学 ▪
- 多项式方程的数值解 ▪
- 关于内部实现的一些注释: 数值及相关函数
历史
1991年引入 (2.0) | 在以下年份被更新:2014 (10.0) ▪ 2024 (14.0)
文本
Wolfram Research (1991),NSolve,Wolfram 语言函数,https://reference.wolfram.com/language/ref/NSolve.html (更新于 2024 年).
CMS
Wolfram 语言. 1991. "NSolve." Wolfram 语言与系统参考资料中心. Wolfram Research. 最新版本 2024. https://reference.wolfram.com/language/ref/NSolve.html.
APA
Wolfram 语言. (1991). NSolve. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/NSolve.html 年
BibTeX
@misc{reference.wolfram_2026_nsolve, author="Wolfram Research", title="{NSolve}", year="2024", howpublished="\url{https://reference.wolfram.com/language/ref/NSolve.html}", note=[Accessed: 17-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_nsolve, organization={Wolfram Research}, title={NSolve}, year={2024}, url={https://reference.wolfram.com/language/ref/NSolve.html}, note=[Accessed: 17-June-2026]}