像mathematica这样的程序如何绘制图表以及如何制作这样的程序?

发布于 2024-10-14 03:20:27 字数 111 浏览 6 评论 0原文

我一直想知道mathematica 和mathlab 等程序如何如此优雅且快速地绘制函数图。谁能向我解释他们是如何做到这一点的,此外,我该如何做到这一点?它与计算机编程或数学的某个方面或课程有关吗?那么哪个?

I've been wondering how programs like mathematica and mathlab, and so on, plot graphs of functions so gracefully and fast. Can anyone explain to me how they do this and, furthermore, how I can do this? Is it related to an aspect or course in Computer Programming or Math? Which then?

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

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

发布评论

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

评论(4

夜未央樱花落 2024-10-21 03:20:27

好吧,在贝利萨留的一些鼓励下,这是我的评论作为答案:尝试查看 matplotlib。从主页:

matplotlib 是一个 Python 2D 绘图库,它以各种硬拷贝格式和跨平台的交互环境生成出版质量的图形。 matplotlib 可用于 python 脚本、python 和 ipython shell(ala MATLAB®* 或 Mathematica®†)、Web 应用程序服务器和六个图形用户界面工具包。

它最初受到 MATLAB 绘图功能的启发,尽管从那时起它已经发展了很多。它是可靠的软件 - 而且它是开源的,在 BSD 许可证下,因此您不仅可以阅读源代码,还可以破解它并以任何您喜欢的方式使用它。

您可以查看的另一个地方是 gnuplot。它不是常见的开源许可证之一,但它肯定是开源的,具有一些修改权限等。

Gnuplot 是一个可移植的命令行驱动的图形实用程序,适用于 Linux、OS/2、MS Windows、OSX、VMS 和许多其他平台。源代码受版权保护,但可以自由分发(即,您无需付费)。它最初的创建是为了让科学家和学生能够交互式地可视化数学函数和数据,但现在已经发展到支持许多非交互式用途,例如网络脚本。它还被 Octave 等第三方应用程序用作绘图引擎。自 1986 年以来,Gnuplot 一直受到支持并积极开发。

它也可以进行 3D 绘图,这是 matplotlib 不具备的功能,而且它已经存在了很长时间。我首先想到 matplotlib 的原因是它旨在作为高级语言的库,而不是独立的应用程序,所以我猜它可能更容易阅读。

另一项建议是,为了了解 Mathematica 在幕后所做的事情,请查看 Plot 文档。特别是,如果您查看可用的选项,您可以推断出一些事情。

MaxRecursion Automatic允许的最大递归细分数
方法 自动 用于细化曲线的方法
PerformanceGoal $PerformanceGoal 尝试优化的性能方面
PlotPoints 自动样本点的初始数量

MaxRecursionPlotPoints,你可以看到它正在做初始采样然后以某种方式决定哪些区域需要细分(重新采样)以获得准确的绘图视图。从那时起,它就变得神奇了:有一些Method可以实现这一点,还有一个PerformanceGoal来指导它......

Well, with some encouragement from belisarius, here's a my comment as an answer: try looking at matplotlib. From the home page:

matplotlib is a python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms. matplotlib can be used in python scripts, the python and ipython shell (ala MATLAB®* or Mathematica®†), web application servers, and six graphical user interface toolkits.

It was originally inspired by MATLAB's plotting capabilities, though it's grown a lot since then. It's solid software - and it's open source, under a BSD license, so not only can you read the source, you can hack on it and use it in whatever you like.

Another place you could look is gnuplot. It's not one of the common open source licenses, but it's certainly open source, with some permissions to modify and such.

Gnuplot is a portable command-line driven graphing utility for linux, OS/2, MS Windows, OSX, VMS, and many other platforms. The source code is copyrighted but freely distributed (i.e., you don't have to pay for it). It was originally created to allow scientists and students to visualize mathematical functions and data interactively, but has grown to support many non-interactive uses such as web scripting. It is also used as a plotting engine by third-party applications like Octave. Gnuplot has been supported and under active development since 1986.

It does 3D plotting as well, which matplotlib doesn't do, and it's been around a lot longer. The reason I thought of matplotlib first is that it's intended as a library for a higher-level language, not a stand-alone application, so I'm guessing it might a bit easier for you to read.

One other suggestion, just to get an idea of the sorts of things Mathematica is doing under the hood, is to look at the documentation for Plot. In particular, if you look at the available options, you can deduce things.

MaxRecursion Automatic the maximum number of recursive subdivisions allowed
Method Automatic the method to use for refining curves
PerformanceGoal $PerformanceGoal aspects of performance to try to optimize
PlotPoints Automatic initial number of sample points

From the MaxRecursion and PlotPoints, you can see that it's doing an initial sampling then somehow deciding which regions need to be subdivided (resampled) to get an accurate view of the plot. And from there on, it's magic: there is some Method for this, and a PerformanceGoal to guide it...

小…红帽 2024-10-21 03:20:27

对于 MATLAB,由于其跨平台要求,没有使用 OpenGL 的替代方案。 MATLAB 运行时是用 C++ 编写的,非轴 GUI 使用 Java Swing。因此 MATLAB Plot 可能是 C++/OpenGL/Swing 的混合体。

实际上,MATLAB 图形比视频游戏图形复杂得多。我认为更容易找到有关视频游戏图形的教程,然后将其“缩小”为 MATLAB 功能,例如用相同颜色绘制一条线。

最重要的概念可能是变换矩阵

For MATLAB, because of its cross-platform requirement there is no alternatives as using OpenGL. MATLAB runtime is written in C++ and non-axis GUI uses Java Swing. Therefore MATLAB Plot is probably a C++/OpenGL/Swing mixture.

In reality MATLAB graphics is much less complex then a video game graphics. I think it is easier to find tutorials on video game graphics and then "downsize" it to MATLAB functionality, like drawing a single line with the same color.

The most important concept is probably Transformation Matrix.

心意如水 2024-10-21 03:20:27

基本上,大多数绘制任何类型的图形(特别是任何具有合理复杂性的图形)的程序都会使用某种类型的第三方库。

使用的特定库取决于正在使用的编程语言。
例如:

对于 .Net 应用程序,您可以使用 Crystal 报表。 http://en.wikipedia.org/wiki/Crystal_Reports

对于 Java,您可以使用 JFreeChart。 http://www.jfree.org/jfreechart/
等等...

您可能会找到许多适用于您决定使用哪种语言进行编码的库。

如果您想在特定项目中完成此功能,我建议使用库,特别是如果您是初学者。由于许多问题,例如跨平台兼容性、图形渲染优化(即:确保图形渲染快速且“漂亮”)、与元素定位相关的数学等许多问题,这些图形库的实现方式的内部复杂性将非常显着。图表等。

最后,我怀疑您是否会找到该主题的特定课程(或需要它们),因为再次排除非常特定的情况,程序员将始终使用已经存在的库。

既然有人已经有了,为什么还要自己编码呢?
已经帮您解决问题了吗?

Basically most programs that plot any type of graph (particularly any graphs of reasonable complexity) will use some type of third party libraries.

The specific library used would depend on the programming language that is being used.
For example:

For a .Net application you might use Crystal reports. http://en.wikipedia.org/wiki/Crystal_Reports

For Java you might use JFreeChart. http://www.jfree.org/jfreechart/
And so on...

You will likely find numerious libraries for whatever language you decide to code in.

If you want to accomplish this functionality in your specific project I suggest using a library especially if you are a beginner. The internal complexities of how these graph libraries are implemented would be significant because of many issues such as cross platform compatibility, graphic rendering optimizations (ie: making sure the graphics render quickly and ‘prettily’), the maths associated with the positioning of elements on the graph and so forth.

Lastly I doubt you will find specific courses in this subject (or require them) as again excluding VERY specific cases programmers will always use libraries that already exist.

Why code it yourself when someone has
already solved the problem for you?

七婞 2024-10-21 03:20:27

一个好的起点是了解图形有语法,并且您在收到绘图命令后想要构建的是图形的符号表示。对于 Mathematica,您可以执行类似

FullForm[Plot[Sin[x], {x, 0, 2 Pi}]]

查看 Mathematica 使用的内部表示形式的操作。基本上,您需要根据颜色和坐标来描述要绘制的线段 (2D) 或网格 (3D)。此外,还需要有关图形比例以及如何绘制刻度线、标签轴等的信息。

这将我们引向问题的核心,如何确定要从函数和函数中绘制的线段范围?如果你仔细研究绘图的帮助文件,你会看到一些东西。首先有一个绘图点选项和一个 MaxRecursion 选项。这让我相信(这只是一个有根据的猜测,但这就是我的做法)Mathematica 在范围内的偶数间隔上绘制初始点数以获得起始值。下一部分是识别变化超过某个阈值的区域,然后对更多点进行采样,直到线段中任意两点之间的“变化”低于阈值。 Mathematica 递归地执行此操作,因此有 MaxRecursion 选项。

到目前为止,我对变化率的定义还相当模糊。描述变化的更有用的方法是在线段上取 3 分。假设第 1 点和第 3 点之间存在线性关系,并假设这种线性关系,对第二点进行预测。如果此预测的误差足够低,则考虑下一组三个点。如果误差高于阈值,那么您应该在此区域中采样更多点,直到满足阈值。通过这种方式,您将需要相对较少的曲线相对笔直的点,而更多的曲线在“有趣”的部分,其中曲线向新的方向弯曲。您绘制的曲线的平滑度将与您在点的线性预测中愿意容忍的误差成正比。

A good place to start is to understand that there is a grammar to graphics and what you want to construct upon receiving a plot command is a symbolic representation of the graph. For Mathematica, you can do something like

FullForm[Plot[Sin[x], {x, 0, 2 Pi}]]

to see the internal representation Mathematica uses. Basically you need to describe the line segments (2D) or meshes (3D) you want to draw in terms of their color and coordinates. Also, there needs to information about the scale of the graph and how to draw tick marks, label axes, etc.

This leads us to the heart of the question, how do you determine the line segment you want to draw from a function and a range? If you dig around in the help file for plot, you see a few things. First there is a plot points option and a MaxRecursion option. This leads me to believe (and this is just an educated guess, but it is how I would do it) that Mathematica plots the initial number of points on an even interval over the range to get a starting value. The next part is to identify regions where change exceeds some threshold and then to sample more points until the "change" between any two points in your line segment is below a threshold. Mathematica does this recursively, hence the MaxRecursion option.

So far I have been pretty vague about defining rate of change. A more useful way to describe change is to take 3 pts on your line segment. Assume a linear relationship between the 1st and 3rd point and, assuming this linear relationship, make a prediction about what the 2nd point would be. If the error of this prediction is sufficiently low, then consider the next group of three points. If the error is above a threshold, then you should sample some more points in this region until the threshold is met. In this way you will require relatively few points where the curve is relatively straight and more at the "interesting" parts where it bends in new directions. The smoothness of the curve you draw will be proportional to the error you are willing to tolerate in the linear prediction of points.

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