7

I am developing a 2D plotting program for functions of 1 variable. It is designed to be very simple so that the user should not have to select the initial plot limits (or "range").

Are there known algorithms that can find the most interesting plot limits, knowing only the function f(x) ?

Notes:

  • The definition of interesting plot limits is not well defined here. This is part of the question: what is the most interesting part of the plot?
  • I already have an algorithm to determine the range of x values where the function f has finite values.
  • I am using Javascript, but any language is ok.
  • I don't want to use existing libraries.
  • The function f is restricted to expressions that the user can write with the basic math operators + - * / ^ and functions exp log abs sqrt sin cos tan acos asin atan ceil floor.
  • Using the Google graphs, you can get some examples of automatic limits. Typing graph sin(x) works pretty well, but graph exp(x) and graph log(x) don't really give the best results. Also, graph sin(x*100)*exp(-x^2) does not choose the limits I would qualify as the most informative. But it would be good enough for me.

UPDATE:

I found that PlotRange in Mathematica does that automatically very well (see here). Is the source code available, or a reference explaining the algorithm? I could not find it anywhere.

UPDATE:

I started using an adaptative refinement algorithm to find informative plot ranges, inspired from this site. It is not working perfectly yet, but the current progress is implemented in my project here. You can try plotting a few functions and see how it works. When I have a fully working version I can post an answer.

4
  • By the way, is it the right place for this question? Or should it be in scicomp.SE? Or somewhere else? Commented May 31, 2013 at 18:01
  • Related question Commented Jun 10, 2013 at 22:19
  • mathematica does not autorange the independent axis range, which is obviously the hardest part of what you ask. Commented Jun 26, 2013 at 15:45
  • @george, True, I did not realize that. I will remove that comment in the question. Commented Jun 26, 2013 at 16:51

1 Answer 1

0

I don't have a complete answer, but I might have some useful ideas to start with.

For me, interesting parts of a graph include:

  • All the roots of the function, except where there is an infinite number of roots (where we might be interested in no more than 8 of each).

  • All the roots of the first and second derivatives of the function, again except where there is an infinite number of roots.

  • The behaviour of the function around x = 0.

  • Locations of asymptotes, though I wouldn't want the graph to plot all the way to infinity.

  • To see the features of the graph, I'd like it to take up a "reasonable" amount of the rectangular graphing window. I think that might be achieved by aiming to have the integral of the absolute value of the function over the plotted range equal be in the range of, say, 20-80% of the graphing window.

Thus, a sketch of an heuristic for setting plot limits might be something like:

  1. Find the range that includes all the roots of the function, its first and second derivatives, or (for functions with infinite numbers of roots) the (say) 8 roots closest to x=0.

  2. If the range does not already include x=0, expand the range to include x=0.

  3. Expand the x range by, say, 10% in each direction to ensure that all "interesting" points are well within the window.

  4. Set the y range so that the integral of the absolute value of the function is (say) 30% of the area of the graphing window.

Sign up to request clarification or add additional context in comments.

2 Comments

There are good points in that answer, such as the roots location. However, finding roots is not an easy task, especially when you may have roots very close to each other, or very far away. What about functions without roots or roots of the derivative such as exp? Also, your point 2 does not make sense as the 0 should not necessarily be included. And your point 4 may not be adequate for functions that are close to 0 for a wide range of x, or for functions that are always much higher than 0.
Perhaps it is just the graphs that I tend to work with that have interesting aspects near x=0. For functions without roots, I wonder if the integral heuristic (i.e. arrange the graph window so that the absolute value of the integral is around 30% of the graph window area) might be the most useful. Since we're only looking for the range between the root with the highest x-value and the lowest x-value, imprecise root-finding (or even missing one of two closely-spaced roots) might not be a problem for this situation. I agree with your edit that Mathematica's PlotRange seems to do a good job.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.