NSolveValues[expr,vars]
尝试找到由系统 expr 的解确定的 vars 值的数值近似.
NSolveValues[expr,vars,Reals]
在实数范围内求解.
NSolveValues
NSolveValues[expr,vars]
尝试找到由系统 expr 的解确定的 vars 值的数值近似.
NSolveValues[expr,vars,Reals]
在实数范围内求解.
更多信息和选项
- 系统 expr 可以是以下内容的任何逻辑组合:
-
lhs==rhs 等式 lhs!=rhs 不等式 lhs>rhs 或 lhs>=rhs 不等式 expr∈dom 定义域规范 {x,y,…}∈reg 区域规范 ForAll[x,cond,expr] 通用量词 Exists[x,cond,expr] 存在量词 - NSolveValues[{expr1,expr2,…},vars] 等价于 NSolveValues[expr1&&expr2&&…,vars].
- 可以指定一个变量或变量列表.
- 如果指定了单个变量,则结果是使 expr 为 True 的变量值列表.
- 如果指定了变量列表,则结果是使 expr 为 True的变量值列表的列表.
- 当指定单个变量且方程特根的多重性大于 1 时,NSolveValues 会提供相应解的多个副本.
- NSolveValues[expr,vars] 默认假设不等式中以代数形式出现的量是实数,而所有其他量都是复数.
- 在 NSolveValues[expr,vars,Reals] 中,所有变量、参数、常数和函数值均被限定为实数.
- NSolveValues[expr&&vars∈Reals,vars,Complexes] 求解变量的实数值,但函数值可以是复数.
- NSolveValues[…,x∈reg,Reals] 将 x 约束在区域 reg 中. 可以使用 Indexed[x,i] 引用 x 的不同坐标.
- NSolveValues 主要处理线性和多项式方程.
- 可以给出以下选项:
-
MaxRoots Automatic 返回的最大根数 Method Automatic 应该使用什么方法 RandomSeeding 1234 伪随机发生器的种子 VerifySolutions Automatic 是否验证解 WorkingPrecision Automatic 计算中使用的精度 - 可能得 Method 设置包括 "EndomorphismMatrix"、"Homotopy"、"Monodromy",和 "Symbolic". »
范例
打开所有单元 关闭所有单元基本范例 (6)
NSolveValues[x ^ 5 - 2x + 3 == 0, x]NSolveValues[x ^ 5 - 2x + 3 == 0, x, Reals]NSolveValues[x ^ 1000000 - 2x + 3 == 0, x, MaxRoots -> 3]NSolveValues[{x ^ 2 + y ^ 3 == 1, 2x + 3y == 4}, {x, y}]NSolveValues[{x ^ 2 + y ^ 3 == 1, 2x + 3y == 4}, {x, y}, Reals]NSolveValues[{x, y}∈InfiniteLine[{{0, 0}, {2, 1}}] && {x, y}∈Circle[], {x, y}]Graphics[{{Blue, InfiniteLine[{{0, 0}, {2, 1}}], Circle[]}, {Red, Point[%]}}]范围 (48)
一元复数方程 (10)
NSolveValues[x ^ 5 - 2x + 17 == 0, x]NSolveValues[x ^ 3 - 1.234x + 5.678 == 0, x]NSolveValues[(x ^ 2 - 1)(x ^ 4 - 1) == 0, x]NSolveValues[x ^ 1234567 + 9x ^ 2 + 7x - 1 == 0, x, MaxRoots -> 5]NSolveValues[Sqrt[x] + 3x ^ (1 / 3) == 5, x]NSolveValues[Sin[x] == 1 / 3, x]NSolveValues[Sin[x] == 1 / 3, x, MaxRoots -> Infinity]NSolveValues[5 == x 2 ^ (x ^ 2), x, MaxRoots -> 3]NSolveValues[Sin[E ^ x] - Cos[2 x] == 1 && -1 ≤ Re[x] ≤ 1 && -1 ≤ Im[x] ≤ 1, x]NSolveValues[Gamma[x] - Log[x] == I / 2 && Abs[x - 2] < 3 / 2, x]NSolveValues[Cos[Exp[x]] == 3 Exp[-x] + 1 && 0 ≤ Re[x] ≤ 1, x]NSolveValues[Cos[Exp[x]] == 3 Exp[-x] + 1 && 0 ≤ Re[x] ≤ 1, x, MaxRoots -> Infinity]NSolveValues[BesselJ[2, x]x - 2 ^ x == 3, x]多变量复方程组 (9)
NSolveValues[{x + 2y + 3z == 4, 3x + 4y + 5z == 6, 7x + 9y + 8z == 10}, {x, y, z}]NSolveValues[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}]NSolveValues[x + 2y + 3z == 4 && 3x + 4y + 5z == 6 && 6x + 7y + 8z == 9, {x, y, z}]NSolveValues[x + 2y + 3z == 4 && 3x + 4y + 5z == 6 && 6x + 7y + 8z == 0, {x, y, z}]NSolveValues[x ^ 2 + y ^ 2 == 1 && x + 2y == 3, {x, y}]NSolveValues[x ^ 10000 == y ^ 2 + 2y + 1 && y ^ 10000 == z ^ 2 + 2z + 1 && z ^ 10000 == x ^ 2 + 2 x + 1, {x, y, z}, MaxRoots -> 5]NSolveValues[x ^ 2 + y ^ 2 + z ^ 2 == 1 && x y z == 2, {x, y, z}]NSolveValues[Sqrt[x y] == Sqrt[x + y] && Sqrt[x] - y ^ (1 / 3) == 1, {x, y}]NSolveValues[Sin[x + y] == x y + 1 && Cos[x - y] == AiryAi[x y] + 2, {x, y}]NSolveValues[3 ^ x ^ 2 == 7 && x ^ 2 - y ^ 3 == 4, {x, y}, MaxRoots -> 5]一元实数方程 (11)
NSolveValues[x ^ 5 - 2x + 1 == 0, x, Reals]NSolveValues[(x ^ 2 - 1)(x ^ 4 - 1) == 0, x, Reals]NSolveValues[Sqrt[x] + 3x ^ (1 / 3) == 5, x, Reals]NSolveValues[RealAbs[x] ^ 2 - x + UnitStep[x] == 9, x, Reals]NSolveValues[E ^ x - x == 7, x, Reals]NSolveValues[ 27^2x - 1^(1/(x)) == Sqrt[9^2x - 1], x, Reals]NSolveValues[AiryBi[1 - x ^ 2] == 0 && 2 < x < 3, x, Reals]NSolveValues[900 < AiryAiZero[2t + 1] ^ 2 < 1000, t, Reals]NSolveValues[E ^ (2E ^ x) - Log[x ^ 2 + 1] - 20x == 11, x, Reals]NSolveValues[x ^ 1000000 - 2x ^ 777777 + 3x ^ 12345 + 9x ^ 67 - 10 == 0, x, Reals]NSolveValues[2x ^ (123451 / 67890) - x ^ 2 + 4Sqrt[x] - 4x - 9 / 8 == 0, x, Reals]NSolveValues[x ^ Pi - x ^ x ^ Sqrt[2] - Sqrt[3]x + 2 ^ (1 / 3) == 0, x, Reals]NSolveValues[10Sin[Tan[E ^ -x ^ 2]] - x == 3, x, Reals]NSolveValues[2Sin[Exp[x]] - Cos[Pi x] == 3 / 2 && -1 < x < 1, x, Reals]NSolveValues[Cos[x] - BesselJ[5, x] == 1 / 2 && 0 ≤ x ≤ 10, x]多变量的实数方程组和不等式组 (9)
NSolveValues[2 x + 3y - 5z == 1 && 3x - 4y + 7z == 3 && x + y - z == 8, {x, y, z}, Reals]NSolveValues[x y == z ^ 2 - x && x y z == 2 && x ^ 2 + y ^ 2 + z ^ 2 == 5, {x, y, z}, Reals]NSolveValues[Exists[x, x ^ 2 + a x + b == 0 && 2 x + a == 0 && a ^ 2 + x b ^ 2 == 1], {a, b}, Reals]NSolveValues[ForAll[x, Exists[y, a x ^ 2 + b y ^ 2 - 3y == 1 && y < 0 && a y - y == b + 1]], {a, b}, Reals]NSolveValues[Sqrt[x + 2y] - 3x + 4y == 5 && x + y ^ (1 / 3) == 2, {x, y}, Reals]NSolveValues[Max[x ^ 2, 1 - y ^ 3] == Min[y ^ 2, x] + 7y && x + UnitStep[4 - y] == 5, {x, y}, Reals]NSolveValues[Sin[x + y] == 1 / 2 && E ^ x - y == 1 && x ^ 2 ≤ 10, {x, y}, Reals]NSolveValues[ 3^x - 2^2y == 77 && Sqrt[3^x] - 2^y == 7, {x, y}, Reals]NSolveValues[E ^ x y ^ 3 + Log[x]y == 1 && x y + E ^ x / x == 10, {x, y}, Reals]NSolveValues[Exists[a, a x ^ 2 + Sinh[x ^ 2 + 1]a ^ 2 == 1 && x ^ 2 - a ^ 2 == 1], x, Reals]NSolveValues[Sin[x - Cos[x]] y ^ 3 - x == 1 && x ^ 2 + y ^ 2 == 1, {x, y}, Reals]NSolveValues[Exists[y, y ^ 3 - Cos[x] y + 2 x ^ 2 Sin[x ^ 2 - 1] == 0 && x ^ 2 + y ^ 2 == 2], x, Reals]NSolveValues[y ^ 3 - BesselJ[2, x + 2] y - y - 3 x == -2 && x ^ 2 ≤ 1 && x y - x ^ 2 + y ^ 2 == 3, {x, y}, Reals]NSolveValues[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}}];NSolveValues[{x, y}∈Subscript[ℛ, 1] && {x, y}∈Subscript[ℛ, 2], {x, y}]Graphics[{{Blue, Subscript[ℛ, 1], Subscript[ℛ, 2]}, {Red, Point[%]}}]Subscript[ℛ, 1] = Sphere[];
Subscript[ℛ, 2] = InfinitePlane[{{0, 0, 0}, {0, 1, 0}, {1, 0, 1}}];NSolveValues[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[%]}}]}]ℛ = ImplicitRegion[a + 2 b - 3 c ≥ 1 && a b c == 7, {a, b, c}];NSolveValues[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}];NSolveValues[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]];NSolveValues[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[ %]}}]}]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]];NSolveValues[{x, y}∈Subscript[ℛ, 4], {t, x, y}, Reals]Graphics[{{Blue, Subscript[ℛ, 1], Subscript[ℛ, 2], Subscript[ℛ, 3]}, {Red, Point[{x, y}]}} /. (Thread[{t, x, y} -> #]& /@ %)]Subscript[ℛ, 1] = Circle[{a, b}, r];
Subscript[ℛ, 2] = Circle[{a + 1, b}, r];
Subscript[ℛ, 3] = RegionIntersection[Subscript[ℛ, 1], Subscript[ℛ, 2]];NSolveValues[({0, 1} | {0, -1})∈Subscript[ℛ, 3], {a, b, r}, Reals]Show[{Graphics[{{Blue, Subscript[ℛ, 1]}, {Green, Subscript[ℛ, 2]}, {Red, Point[{{0, 1}, {0, -1}}]}} /. Thread[{a, b, r} -> %[[1]]]]}]ℛ = RegionIntersection[Circle[], Line[{{-2, -1}, {1, 2}}]];NSolveValues[x∈ℛ, x]ℛ = Sphere[];NSolveValues[x.{1, 2, 3} == 0 && x.{-3, -2, -1} == 0 && x∈ℛ, x]选项 (10)
MaxRoots (4)
NSolveValues[x ^ 12345 + x + 1 == 0, x, MaxRoots -> 3]NSolveValues[x ^ 1000 == y + 1 && y ^ 1000 == z + 1 && z ^ 1000 == x + 1, {x, y, z}, MaxRoots -> 3]NSolveValues[Sin[x + y] == 2 && Cos[x - y] == 3, {x, y}, MaxRoots -> 5]在默认的 Automatic 设置下,NSolveValues 可能无法给出所有解法:
NSolveValues[Sin[x] == 1 / 2, x]设置 MaxRootsInfinity,NSolveValues 试图求出所有解:
NSolveValues[Sin[x] == 1 / 2, x, MaxRoots -> Infinity]Method (4)
NSolveValues[x ^ 2 + 2y ^ 2 == 3 && x ^ 3 - 4x y == 5, {x, y}]NSolveValues[x ^ 2 + 2y ^ 2 == 3 && x ^ 3 - 4x y == 5, {x, y}, Method -> "EndomorphismMatrix"]NSolveValues[x ^ 2 + 2y ^ 2 == 3 && x ^ 3 - 4x y == 5, {x, y}, Method -> "Homotopy"]NSolveValues[x ^ 2 + 2y ^ 2 == 3 && x ^ 3 - 4x y == 5, {x, y}, Method -> "Monodromy"]NSolveValues[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 = NSolveValues[syst, vars])//Length//AbsoluteTimingUnion[sols, SameTest -> (Norm[#1 - #2] < 10 ^ -6&)]//Length"Monodromy" 方法的运行速度更快,而且不会产生多个根的重复副本:
NSolveValues[syst, vars, Method -> "Monodromy"]//Length//AbsoluteTimingNSolveValues[Sin[x + y] + Cos[x - y] == 1 && Sin[x - y] + Cos[x + y] == 2, {x, y}, Method -> "Monodromy"]NSolveValues[Sin[x + y] + Cos[x - y] == 1 && Sin[x - y] + Cos[x + y] == 2, {x, y}, Method -> "Symbolic"]Method 选项也可用于本地设置 "NSolveOptions" 组中的系统选项:
"NSolveOptions" /. SystemOptions[]默认情况下 NSolveValues 为欠定复杂系统引入了切分超平面(slicing hyperplanes):
NSolveValues[x ^ 2 + y ^ 2 == 1, {x, y}]设置 Method->{"UseSlicingHyperplanes"->False},NSolveValues 给出了参数解:
NSolveValues[x ^ 2 + y ^ 2 == 1, {x, y}, Method -> {"UseSlicingHyperplanes" -> False}]VerifySolutions (1)
NSolveValues 验证使用非等效转换获得的解:
funs = {Sqrt[x + Sqrt[x]] - 2, y ^ 9 - y - 2 x ^ (1 / 7) - 3};
eqns = And@@Thread[funs == 0];
sol1 = NSolveValues[eqns, {x, y}];通过设置 VerifySolutions->False,NSolve 不验证解:
sol2 = NSolveValues[eqns, {x, y}, VerifySolutions -> False];使用 VerifySolutions->False 返回的某些解是不正确的:
Length /@ {sol1, sol2}sol3 = Select[sol2, Chop[funs /. Thread[{x, y} -> #], 10 ^ -8] == {0, 0}&];sol3 === sol1WorkingPrecision (1)
默认情况下,NSolveValues 使用机器精度计算来求精确方程的解:
NSolveValues[x ^ 5 + 2 x + 7 == 0, x]NSolveValues[x ^ 5 + 2 x + 7 == 0, x, WorkingPrecision -> 50]应用 (17)
几何 (11)
pts = NSolveValues[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[pts]}]}]求 InfiniteLine[{0,0},{1,1}] 和 InfiniteLine[{{0,1},{1,0}}] 的交点:
line1 = InfiniteLine[{0, 0}, {1, 1}];
line2 = InfiniteLine[{{0, 1}, {1, 0}}];sol = NSolveValues[{x, y}∈line1∧{x, y}∈line2, {x, y}]Graphics[{{Lighter[Blue, 0.5], line1, line2}, {Red, Point[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 = NSolveValues[{x, y}∈line∧{x, y}∈circle, {x, y}]Graphics[{{Lighter[Blue, 0.5], line, circle}, {Red, Point[sol]}}, ImageSize -> Small]lines = InfiniteLine /@ RandomReal[1, {5, 2, 2}];使用 BooleanCountingFunction 表示恰好有两个条件成立:
sol = NSolveValues[{x, y}∈BooleanRegion[BooleanCountingFunction[{2}, 5], lines], {x, y}]Graphics[{{Lighter[Blue, 0.5], lines}, {Red, PointSize[Medium], Point[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 = NSolveValues[{x, y}∈BooleanRegion[BooleanCountingFunction[{2}, 5], circles], {x, y}]Graphics[{{Lighter[Blue, 0.5], circles}, {Red, PointSize[Medium], Point[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 = NSolveValues[{{x, y, z}∈line, {x, y, z}∈plane}, {x, y, z}]Graphics3D[{{Opacity[0.5], line, plane}, {Red, PointSize[Large], Point[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 = NSolveValues[{{x, y, z}∈line, {x, y, z}∈sphere}, {x, y, z}]Graphics3D[{{Opacity[0.5], line, sphere}, {Red, PointSize[Large], Point[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 = NSolveValues[{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[sol]}}, PlotRangePadding -> 0.2]planes = InfinitePlane /@ RandomReal[1, {3, 3, 3}];sol = NSolveValues[{x, y, z}∈RegionIntersection@@planes, {x, y, z}]Graphics3D[{{Opacity[0.5], planes}, {Red, PointSize[Medium], Point[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 = NSolveValues[{x, y, z}∈RegionIntersection@@spheres, {x, y, z}]Graphics3D[{{Opacity[0.5], spheres}, {Red, PointSize[Large], Point[sol]}}]planes = InfinitePlane /@ RandomReal[{-1, 1}, {10, 3, 3}];使用 BooleanCountingFunction 求三件事正好成立的条件:
sol = NSolveValues[{x, y, z}∈BooleanRegion[BooleanCountingFunction[{3}, 10], planes], {x, y, z}];//QuietGraphics3D[{{Opacity[0.3], planes}, {Red, PointSize[Medium], Point[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 = NSolveValues[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 = NSolveValues[eqns, vars]]Union[solns, SameTest -> (Norm[#1 - #2] < 10 ^ -6&)]//LengthLength[NSolveValues[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 = NSolveValues[polys[[1 ;; 4]], vars, Reals]对于每个解,插入解的前四个残差都很小,而后两个残差则不可忽略:
residuals = (polys /. Thread[vars -> #])& /@ realsols现在,将每个解作为最小化残差平方和的起点,精细化求解每个解:
newsols = Map[FindMinimum[polys.polys, Transpose[{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 = NSolveValues[polys == 0, vars]]每个实值解均对应一个不同的平衡位置,共有六个这样的平衡位置:
realsols = Union[Cases[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 = NSolveValues[eqns, vars, Method -> "Monodromy"]]Cases[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 = NSolveValues[asymp, {a, b, c}];
Select[desolns, MatchQ[#, {_Real..}]&]属性和关系 (7)
polys = {x ^ 2 + y ^ 2 - 1, 2x + 3y - 4};
NSolveValues[Thread[polys == 0], {x, y}]polys /. (Thread[{x, y} -> #]& /@ %)对于单变量方程,NSolveValues 根据解的多重性重复求解:
NSolveValues[(x - 1) ^ 2(x - 2) ^ 3 == 0, x]NSolveValues[(x ^ 4 - 1)(x ^ 4 - 4) == 0, x]NSolveValues[(x ^ 4 - 1)(x ^ 4 - 4) == 0, x, Reals]NSolveValues 给出解的值:
NSolveValues[x ^ 4 == 4, x]NSolve 根据替换规则表示解:
NSolve[x ^ 4 == 4, x]NSolveValues 是一个全局方程求解器:
NSolveValues[{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}}]NSolveValues 给出近似结果:
NSolveValues[{x ^ 2 + y ^ 2 == 1, x + y ^ 2 == 2}, {x, y}]使用 SolveValues 获得确切解:
SolveValues[{x ^ 2 + y ^ 2 == 1, x + y ^ 2 == 2}, {x, y}]使用 FindInstance 获取确切解的实例:
FindInstance[{x ^ 2 + y ^ 2 == 1, x + y ^ 2 == 2}, {x, y}]使用 NDSolveValue 数值式求解微分方程:
NDSolveValue[{y''[x] == -y[x], y[0] == 0, y'[0] == 1}, y[x], {x, 0, 7}]Plot[%, {x, 0, 7}]可能存在的问题 (7)
poly = Expand[Product[x - i, {i, 30}]];NSolveValues[poly == 0, x]更高的 WorkingPrecision 可以产生更准确的结果:
NSolveValues[poly == 0, x, WorkingPrecision -> 20]eqns = {x ^ 2 + y ^ 2 == 1, x ^ 3 - 2x y == 2};
sols = NSolveValues[eqns, {x, y}]eqns /. (Thread[{x, y} -> #]& /@ sols)Function[{x, y}, Norm /@ {x ^ 2 + y ^ 2 - 1, x ^ 3 - 2x y - 2}]@@@sols使用较高的 WorkingPrecision 将给出具有较小公差的解:
sols = NSolveValues[eqns, {x, y}, WorkingPrecision -> 25]Function[{x, y}, Norm /@ {x ^ 2 + y ^ 2 - 1, x ^ 3 - 2x y - 2}]@@@sols如果解集是无限的,则 NSolveValues 给出其与随机超平面的交集:
NSolveValues[x ^ 2 + y ^ 2 == 1, {x, y}]NSolveValues[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}]默认情况下,NSolveValues 可能会显示有多个解,但实际计数其实应该更小:
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 = NSolveValues[polys, vars]]Length[Union[solns, SameTest -> (Norm[#1 - #2] < 10 ^ -9&)]]Length[NSolveValues[polys, vars, Method -> "Monodromy"]]Length[NSolveValues[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 = NSolveValues[polys, {r, a, b, c, d, lambda, mu}, Method -> "EndomorphismMatrix"]]Length[solbig = NSolveValues[polys, {r, a, b, c, d, lambda, mu}, Method -> "EndomorphismMatrix", WorkingPrecision -> 100]]Map[Max, Abs[(polys /. Thread[vars -> #]&) /@ solmach[[1 ;; 4]]]]Map[Max, Abs[(polys /. Thread[vars -> #]&) /@ solbig[[1 ;; 4]]]]尽管残差存在差异,但两个解在 MachinePrecision 的所有位数上都是一致的:
solmach[[1 ;; 4]] - solbig[[1 ;; 4]]在指定了 Reals 域的情况下,如果函数的解的所有邻域都不是实数,则 NSolveValues 可能不能对这种函数求解:
f = (x ^ 2 - 7 x + 11) ^ (x ^ 2 - 13 x + 42) - 1;NSolveValues[f == 0, x, Reals]Plot[{Abs[f], Im[f]}, {x, 1.5, 7.5}, PlotLegends -> "Expressions"]NSolveValues[f == 0 && Element[x, Reals], x]NSolveValues 可能不会给出所有解:
NSolveValues[Cos[x] == x, x]NSolveValues[Cos[x] == x, x, MaxRoots -> 100]//Length巧妙范例 (1)
技术笔记
-
▪
- 数值运算 ▪
- 方程的数值解 ▪
- 一元方程 ▪
- Wolfram 语言中的数值数学 ▪
- 多项式方程的数值解 ▪
- 关于内部实现的一些注释:数字和相关函数
文本
Wolfram Research (2021),NSolveValues,Wolfram 语言函数,https://reference.wolfram.com/language/ref/NSolveValues.html (更新于 2024 年).
CMS
Wolfram 语言. 2021. "NSolveValues." Wolfram 语言与系统参考资料中心. Wolfram Research. 最新版本 2024. https://reference.wolfram.com/language/ref/NSolveValues.html.
APA
Wolfram 语言. (2021). NSolveValues. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/NSolveValues.html 年
BibTeX
@misc{reference.wolfram_2026_nsolvevalues, author="Wolfram Research", title="{NSolveValues}", year="2024", howpublished="\url{https://reference.wolfram.com/language/ref/NSolveValues.html}", note=[Accessed: 05-December-2026]}
BibLaTeX
@online{reference.wolfram_2026_nsolvevalues, organization={Wolfram Research}, title={NSolveValues}, year={2024}, url={https://reference.wolfram.com/language/ref/NSolveValues.html}, note=[Accessed: 05-December-2026]}