在 Mathematica 中求隐函数的根

发布于 2024-12-03 10:49:11 字数 311 浏览 1 评论 0原文

在 Mathematica 中查找隐式函数的根

我有一个隐式函数,例如:

f(x,y) = x^3 + x*y + y^2 - 36

我想找到根,即方程的解 f(x,y) = 0

绘制解很简单:

ContourPlot[x^3 + x*y + y^2 - 36 == 0, {x , -2 Pi, 2 Pi}, {y, -3 Pi, 3 Pi}]

但是我希望获得图中的数据,而不仅仅是视觉图中的数据。 那么如何找到绘图的数据呢?

Find root of implicit function in Mathematica

I have an implicit function, for example:

f(x,y) = x^3 + x*y + y^2 - 36

I want to find the root, ie solutions to the equation f(x,y) = 0

Drawing the solution is easy:

ContourPlot[x^3 + x*y + y^2 - 36 == 0, {x, -2 Pi, 2 Pi}, {y, -3 Pi, 3 Pi}]

however I would like to have the data that is in the plot and not only the visual plot.
So how do I find the data of the plot?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

饮惑 2024-12-10 10:49:11

我不确定我是否正确解释了您的第二个问题,但假设您需要生成的 ContourPlot 中的 (x,y) 点列表,执行此操作的一种方法可能如下:

plot = ContourPlot[
  x^3 + x*y + y^2 - 36 == 0, {x, -2 Pi, 2 Pi}, {y, -3 Pi, 3 Pi}]

要获取点列表

points = Cases[Normal@plot, x_Line :> First@x, Infinity];

“采取看看“ListPlot

ListPlot[points, PlotRange -> {{-2 Pi, 2 Pi}, {-3 Pi, 3 Pi}}]

给出

在此处输入图像描述

编辑

Nasser 正确指出了这个问题已经之前提到过。 这里是一个指向本质上相同问题的链接,Szabolcs 的这个答案是相关的。

关于上面给出的答案, 这种方法可能更direct:

points2 = Cases[plot, x_GraphicsComplex :> First@x, Infinity]

最后,我应该承认“LunchTime Playground。Mathematica 的乐趣:如何从图中提取点”,请参阅这里,这两个都给出了 上面建议的方法(我现在经常使用)。

编辑 2

此方法是上述方法 1 的改进,因为点是作为 {x,y} 值列表(列表列表)获取的,没有任何无关的 { }。

Cases[Normal@plot, Line[{x__}] :> x, Infinity]

Paul Abbott 发表在 Mathematica Journal 第 7 卷第 2 期,第 108-112 页,1998 年的文章,
在区间中求根,提供了很多有用的信息,并且可以使用 这里

他指出以下内容也有效

Cases[Normal@plot, _Line, -1][[1, 1]]

并且(!)

plot[[1, 1]]

我在评论中引用了FreshApple 提出的问题,可能会发现以下方法的(轻微变体):

InputForm[plot][[1, 1, 1]]

以下计算结果为 True

plot[[1, 1]] == Cases[Normal@plot, Line[{x__}] :> x, Infinity] == 
 InputForm[plot][[1, 1, 1]] == Cases[Normal@plot, _Line, -1][[1, 1]]

编辑 3

只是或有趣...

ListPlot@ContourPlot[x^2 + y^2 == 1, {x, -1, 1}, {y, -1, 1}][[1, 1]]

给出

在此处输入图像描述

I'm not sure if I am interpreting your second question properly, but assuming you require a list of (x,y) points from the generated ContourPlot, one way of doing this might be the following:

plot = ContourPlot[
  x^3 + x*y + y^2 - 36 == 0, {x, -2 Pi, 2 Pi}, {y, -3 Pi, 3 Pi}]

To obtain a list of points

points = Cases[Normal@plot, x_Line :> First@x, Infinity];

'Take a look' with ListPlot

ListPlot[points, PlotRange -> {{-2 Pi, 2 Pi}, {-3 Pi, 3 Pi}}]

giving

enter image description here

Edit

Nasser has correctly pointed out that this question has been addressed before. Here is one link to essentially the same question and this answer by Szabolcs is relevant.

As regards the answer given above, this method is probably more direct:

points2 = Cases[plot, x_GraphicsComplex :> First@x, Infinity]

Finally, I should acknowledge " LunchTime Playground. Fun with Mathematica: How to extract points from a plot", see here, which gives both methods suggested above (and which I now use routinely).

Edit 2

This method is an improvement on method 1 above, as the points are obtained as a list of {x,y} values (list-of-lists) without any extraneous { }.

Cases[Normal@plot, Line[{x__}] :> x, Infinity]

An article by Paul Abbott in the Mathematica Journal Vol 7, No 2, pp 108-112, 1998,
Finding Roots in an Interval, gives a lot of useful information and is available here

He points out the the following also work

Cases[Normal@plot, _Line, -1][[1, 1]]

and(!)

plot[[1, 1]]

I have made reference in the comments to the question by FreshApple where a (slight variant) of the following method may be found:

InputForm[plot][[1, 1, 1]]

The following evaluates to True

plot[[1, 1]] == Cases[Normal@plot, Line[{x__}] :> x, Infinity] == 
 InputForm[plot][[1, 1, 1]] == Cases[Normal@plot, _Line, -1][[1, 1]]

Edit 3

Just or fun ...

ListPlot@ContourPlot[x^2 + y^2 == 1, {x, -1, 1}, {y, -1, 1}][[1, 1]]

gives

enter image description here

情场扛把子 2024-12-10 10:49:11

我鼓励您探索有关方程求解的文档,特别是求解NSolve 函数。

编辑

p = ContourPlot[x^3 + x*y + y^2 - 36 == 0, {x, -2 Pi, 2 Pi}, {y, -3 Pi, 3 Pi}]
p //InputForm

复制并粘贴您需要的部分。

或者,在我看来,这是解决您实际问题的更好方法。

sol = Solve[x^3 + x*y + y^2 - 36 == 0,{x}][[1]] 

您可能需要一些选项才能获得正确的解决方案。

Table[{x/. sol,y},{y, -3 Pi, 3 Pi, 0.02}]

I would encourage you to explore the documentation on equation solving and particularly the Solve and NSolve functions.

EDIT

p = ContourPlot[x^3 + x*y + y^2 - 36 == 0, {x, -2 Pi, 2 Pi}, {y, -3 Pi, 3 Pi}]
p //InputForm

Copy and paste the bit you need.

Alternatively, and in my view a better solution to your actual problem.

sol = Solve[x^3 + x*y + y^2 - 36 == 0,{x}][[1]] 

You might need some options to get the right solution.

Table[{x/. sol,y},{y, -3 Pi, 3 Pi, 0.02}]
过气美图社 2024-12-10 10:49:11

Verbeia 答案的 0.02 美元贡献:

请记住检查 x(y) 和 y(x) 解决方案,因为其中一个可能比另一个更干净。

在您的示例中:

在此处输入图像描述

A $.02 contribution on Verbeia's answer:

Remember to check both x(y) and y(x) solutions as one of them could be cleaner than the other.

In your example:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文