سؤال

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.

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top