Mathematica 中的奇怪 Sin[x] 图 [英] Strange Sin[x] graph in Mathematica

查看:20
本文介绍了Mathematica 中的奇怪 Sin[x] 图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Mathematica 7 中随机绘制了一个 Sin[x] 函数,结果如下:

注意大约 x = -100 处的可见缺陷.

这是缺陷部分的放大图,清楚地表明 Mathematica 出于某种原因在那里的点之间使用了低得多的分辨率:

有人知道为什么会发生这种情况吗?为什么只在 x = -100 处?

注意:同样发生在

您可以看到,对于您的函数,评估函数的点错过了峰值"并引入了大的近似误差.用于选取点位置的算法非常简单,当两个峰的间距比 PlotRange/PlotPoints 更近时,可能会发生这种情况.

Plot 从 50 个等距点开始,然后在最多 MaxRecursion 阶段插入额外的点.如果您为 MaxRecursion 的各种设置绘制区域,您可以看到这个洞"是如何出现的.

plot1 = Plot[Sin[x], {x, -42 Pi, 42 Pi}, PlotPoints ->100,绘图样式 ->浅灰];表[plot2 =绘图[Sin[x], {x, -42 Pi, 42 Pi}, Mesh ->全部,MeshStyle ->厚的,绘图样式 ->红色,MaxRecursion ->k];显示[plot1, plot2, PlotRange ->{{-110, -90}, {-1, 1}},绘图标签 ->("MaxRecursion " <> ToString[k])], {k, 0,5}]//图形列

<块引用>

根据 Stan Wagon 的 Mathematica 一书,Plot 决定如果两条新线段之间的角度大于 5 度,是否在两个连续点之间的中间添加一个额外的点.在这种情况下,情节不走运,初始点定位和细分不符合该标准.您可以看到,在孔的中心插入一个评估点将生成外观几乎相同的图.

使用Refinement选项增加用于决定何时细分的角度的方法(我从书中得到的,但产品中似乎没有记录)

plot1 = Plot[Sin[x], {x, -42 Pi, 42 Pi}, PlotPoints ->100,绘图样式 ->浅灰];显示[情节1,绘图[Sin[x], {x, -42 Pi, 42 Pi}, Mesh ->全部,MeshStyle ->厚的,绘图样式 ->红色,MaxRecursion ->3、方法 ->{细化 ->{控制值 ->4 \[度数]}}],绘图范围 ->{{-110, -90}, {-1, 1}}]

在这里你可以看到,从默认值 5 增加 1 度可以修复这个洞.

I randomly plotted a Sin[x] function in Mathematica 7 and this is what it shows:

Note the visible defect at approximately x = -100.

Here is a zoom of the defect part, clearly showing that Mathematica for some reason uses a much lower resolution between the points there:

Anybody know why this happens and why only at x = -100?

Note: same happens in Wolfram Alpha, by the way.

解决方案

Short answer: default plotting accuracy is not sufficient for that function, so increase it as follows

Plot[Sin[x], {x, -42 Pi, 42 Pi}, PlotPoints -> 100]

Long answer: Plot works by evaluating the function at a finite set of points, and connecting those points by straight lines. You can see the points used by Plot using the following command

Plot[Sin[x], {x, -42 Pi, 42 Pi}, Mesh -> All, PlotStyle -> None, 
 MeshStyle -> Black]

You can see that for your function, the points where the function was evaluated "missed the peak" and introduced a large approximation error. The algorithm used to pick locations of points is very simple and this situation might happen when two peaks are spaced more closely together than PlotRange/PlotPoints.

Plot starts with 50 equally spaced points and then inserts extra points in up to MaxRecursion stages. You can see how this "hole" appears if you plot the region for various settings of MaxRecursion.

plot1 = Plot[Sin[x], {x, -42 Pi, 42 Pi}, PlotPoints -> 100, 
   PlotStyle -> LightGray];
Table[plot2 = 
   Plot[Sin[x], {x, -42 Pi, 42 Pi}, Mesh -> All, MeshStyle -> Thick, 
    PlotStyle -> Red, MaxRecursion -> k]; 
  Show[plot1, plot2, PlotRange -> {{-110, -90}, {-1, 1}}, 
   PlotLabel -> ("MaxRecursion " <> ToString[k])], {k, 0, 
   5}] // GraphicsColumn

According to Stan Wagon's Mathematica book, Plot decides whether to add an extra point halfway between two consecutive points if the angle between two new line segments would be more than 5 degrees. In this case, plot got unlucky with initial point positioning and subdivision does not meet that criterion. You can see that inserting a single evaluation point in the center of the hole will produce almost identically looking plot.

The way to increase the angle used to decide when to subdivide by using Refinement option (I got it from the book, but it doesn't seem to be documented in product)

plot1 = Plot[Sin[x], {x, -42 Pi, 42 Pi}, PlotPoints -> 100, 
   PlotStyle -> LightGray];
Show[plot1, 
 Plot[Sin[x], {x, -42 Pi, 42 Pi}, Mesh -> All, MeshStyle -> Thick, 
  PlotStyle -> Red, MaxRecursion -> 3, 
   Method -> {Refinement -> {ControlValue -> 4 \[Degree]}}], 
 PlotRange -> {{-110, -90}, {-1, 1}}]

Here you can see that increasing it by 1 degree from default 5 fixes the hole.

这篇关于Mathematica 中的奇怪 Sin[x] 图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆