# How do I find, algorithmically, which parts of a given function are interesting to graph?

I'm building a program that does 2D graphing, and was wondering: How can I determine the default zoom level and x/y extents to display on screen, in such a way as to maximise the 'interesting' parts of a function that are shown?

"Interesting parts" would include:

• Minimums/maximums/plateaus,
• Parts of the space where you can actually see the function,
• Roots,
• Discontinuities,
• and anything else that helps understand what the function looks like and what it does.

I am not necessarily looking for a perfect solution, just something that works well for most common cases, hopefully without having to solve the equation the user entered.

Is there a general method I can use? Or a book/reference that might help? Thanks!

-
The best output I've seen of such an algorithm is what Wolfram Alpha does when graphing functions. I assume they have all sorts of special cases for most common expressions. – anktastic Mar 29 '12 at 6:11

## 2 Answers

How will your users enter their curves? In many graphing applications you must be able to deal with raw data, i.e. pairs of numbers. In that case, since your set is finite, you can set up your default window to be (max x - min x) + margin wide and (max y - min y) + margin high.

If a formula is given, you can do the same thing but set a default number of points to plot. After you have generated (x,y) coordinates you proceed as above. If you've ever worked with Octave you can see this for yourself by plotting $y = \tan x$. The default doesn't look like what you would expect.

-

I am afraid this would be rather procedural approach. Calculate the 1st derivative and then the second derivative if possible . This would help determine max and min values of y. You could use newtons' root finding algorithm for roots and to determine discontinuities the obvious one would be finding roots of the denominator, though a more advanced algorithm may be available for this.

-
Thanks for that reply. But what about the initial extent to which the function is zoomed? Say, horizontally, I need an interval of [x1, x2] that will be used to draw the graph - i.e., plot everything from f(x1) to f(x2). – anktastic Mar 27 '12 at 9:50
I think does n't that again depends on which features are being covered. If you want to fit all these function features at the same time u need to maintain a sorted set of all important/interesting (x, y) coordinates and then pick an interval which has the min and max as a subset. – Hardy Mar 27 '12 at 17:15