从 Mathematica 中的 ContourPlot 中提取轮廓
我有一个包含两个变量的函数 f(x,y),我需要知道它过零的曲线位置。 ContourPlot 非常有效地做到了这一点(也就是说:它使用巧妙的多网格方法,而不仅仅是强力细粒度扫描),但只是给我一个绘图。我想要一组值 {x,y} (具有某些指定的分辨率)或者一些插值函数,它允许我访问这些轮廓的位置。
曾想过从 ContourPlot 的 FullForm 中提取它,但这似乎有点麻烦。有更好的方法来做到这一点吗?
I have a function f(x,y) of two variables, of which I need to know the location of the curves at which it crosses zero. ContourPlot does that very efficiently (that is: it uses clever multi-grid methods, not just a brute force fine-grained scan) but just gives me a plot. I would like to have a set of values {x,y} (with some specified resolution) or perhaps some interpolating function which allows me to get access to the location of these contours.
Have thought of extracting this from the FullForm of ContourPlot but this seems to be a bit of a hack. Any better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您最终从
ContourPlot
中提取点,这是一种简单的方法:编辑
看来
ContourPlot
不会产生非常精确的轮廓。它们当然是用于绘图的,并且足够好,但是这些点并不精确地位于轮廓上:我们可以尝试提出自己的方法来跟踪轮廓,但这很麻烦以一般方式。以下是适用于具有平滑轮廓的平滑变化函数的概念:
从某个点 (
pt0
) 开始,沿着f 的梯度找到与轮廓的交点
.现在我们在轮廓上有了一个点。沿着轮廓的切线移动固定的步长(
分辨率
),然后从步骤 1 开始重复。这是一个基本实现,仅适用于可以符号微分的函数:
红点是“起点”,黑点是轮廓的轨迹。
编辑2
也许使用类似的技术使我们从
ContourPlot
获得的点更加精确是一个更简单、更好的解决方案。从初始点开始,然后沿着渐变移动,直到与轮廓相交。请注意,此实现也适用于无法进行符号区分的函数。如果是这种情况,只需将函数定义为
f[x_?NumericQ, y_?NumericQ] := ...
即可。If you end up extracting points from
ContourPlot
, this is one easy way to do it:EDIT
It appears that
ContourPlot
does not produce very precise contours. They're of course meant for plotting and are good enough for that, but the points don't lie precisely on the contours:We can try to come up with our own method to trace the contour, but it's a lot of trouble to do it in a general way. Here's a concept that works for smoothly varying functions that have smooth contours:
Start from some point (
pt0
), and find the intersection with the contour along the gradient off
.Now we have a point on the contour. Move along the tangent of the contour by a fixed step (
resolution
), then repeat from step 1.Here's a basic implementation that only works with functions that can be differentiated symbolically:
The red points are the "starting points", while the black points are the trace of the contour.
EDIT 2
Perhaps it's an easier and better solution to use a similar technique to make the points that we get from
ContourPlot
more precise. Start from the initial point, then move along the gradient until we intersect the contour.Note that this implementation will also work with functions that can't be differentiated symbolically. Just define the function as
f[x_?NumericQ, y_?NumericQ] := ...
if this is the case.从
ContourPlot
中提取点的方式略有不同(可能是由于 David Park):或(作为 {x,y} 点的列表)
编辑
正如所讨论的此处,Paul Abbott 在 Mathematica Journal 上发表的文章 (< em>Finding Roots in an Interval) 给出了以下两种从 ContourPlot 获取 {x,y} 值列表的替代方法,包括 (!)
对于上面的示例
,
其中
A slight variation for extracting points from
ContourPlot
(possibly due to David Park):or (as a list of {x,y} points)
Edit
As discussed here, an article by Paul Abbott in the Mathematica Journal (Finding Roots in an Interval) gives the following two alternative methods for obtaining a list of {x,y} values from ContourPlot, including (!)
For the above example
and
where