在 C# 中生成并使用泰勒多项式

发布于 2024-09-28 13:01:40 字数 649 浏览 1 评论 0原文

我用 C# 编写了一个简单的图形实现,我可以通过将每个像素与其所代表的图形上的位置进行比较并将该位置插入函数中来绘制图形,我必须查看它是否在曲线上。这一切都很好。

我遇到的问题是使用生成的泰勒多项式。例如,我可以通过

从 0 到 n 求和来创建以 c 为中心的超越函数 f 的第 n 个泰勒多项式,计数器变量为 k = ((f(c) 的 k 次导数) * (xc )^k)/k!

我不确定如何在 stackoverflow 上进行数学标记,也不太有能力在网络上执行此操作,但我希望这是可以理解的。左边可以写成 sigma _k=0 ^n 或类似的东西,其中 _ 代表 sigma 下面的部分,^ 代表上面的部分......

所以我最终生成了 cos(x 的 6 次泰勒多项式)以 0(麦克劳林,我知道)为中心,看起来像

“1 - x^2/2!+ x^4/4! - x^6/6!”

这可以通过 C# 中的简单字符串操作来完成。我可以循环遍历并将下一项添加到字符串中。

我真的无法理解我实际上如何能够使用字符串作为函数来与图形位置进行比较,以查看该图形位置是否实际上在此图形上,从而将其绘制出来。本质上是:我如何在 C# 中使用字符串作为实际的数学函数,或者是否有更好的方法来做到这一点。

如果令人困惑,真的很抱歉……我真的尽力以人们可以提供帮助的方式解释它。

I've written a simple graphing implementation in C#, and I can graph things by comparing each pixel to the position on the graph it represents and plugging that position into the function I have to see if it is on the curve. That's all well and good.

The problem I'm having is USING a generated taylor polynomial. For example, I am able to create the nth taylor polynomial of a transcendent function f centered at c by doing

summation of this from 0 to to n with the counter variable being k = ((kth derivative of f(c)) * (x-c)^k)/k!

I am not sure how to do math markup on stackoverflow nor am I too competent with doing that on the web, but I hope that is understandable. The left side could be written as sigma _k=0 ^n or something like that with _ representing the section under sigma and the ^ representing the part above...

So I end up generating a Taylor polynomial of the 6th degree for cos(x) centered at 0(maclaurin, I know) that looks something like

"1 - x^2/2! + x^4/4! - x^6/6!"

This can be done through simple string manipulation in C#. I can just loop through and add the next term to the string.

I really can't comprehend how I would actually be able to use the string as a function to compare to graph positions to see if that graph position is actually on this graph to therefore graph it. So essentially: How would I use a string as an actual mathematical function in C#, or is there a better way of doing this.

Really sorry if it's confusing...really trying my best to explain it in a way that people can help.

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

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

发布评论

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

评论(2

他是夢罘是命 2024-10-05 13:01:40

你需要一个字符串解析器 ->功能。有关示例,请参阅 MathParser ,它可能可以完成您提到的所需的所有操作。

You need a parser of string -> function. See MathParser for an example, that probably does everything you mentioned you need.

变身佩奇 2024-10-05 13:01:40

从一般角度来看,每当您想要将字符串转换为有效的东西时,您都必须实现一个解析器,它将解释该字符串并执行它所指示的操作。对于数学公式,表达式树可用于维护运算和分组的顺序。可能有一些可用的数学表达式库可以执行此操作,或者您可以自己创建。这不是一项简单的任务,但它肯定是可能的。

一旦有了表达式树,要确定给定 x 的值 f(x) 是否可绘制,只需对其求值即可。对于 f(x) 图,您可以首先测试 x 以查看它是否落在绘图区域的可见域内。如果是,则计算 f(x),如果点 (x,f(x)) 是可绘制的,则绘制该点。

From a general perspective, anytime you want to convert a string into something that does work, you have to implement a parser, which will interpret the string and perform the actions dictated by it. For mathematical formulas, an expression tree may be of use to maintain order of operations and grouping. There are probably some math expression libraries available that will do this, or you can roll your own. This is not a trivial task, but it's certainly possible.

Once you have the expression tree, to figure out if a value f(x) for a given x is graphable, just evaluate it. For an f(x) graph, you can test x first to see if it falls in the visible domain of the graphing area. If it does, evaluate f(x) and if the point (x,f(x)) is graphable then draw the point.

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