Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am writing a program that accepts 2D functions from users and graphs them to a window (it's part of a small game, so it's not just that, but that's the essence of my task). The functions can contain basic operators ($+$, $-$, $\times$, $\div$, ^), a few functions ($sin$, $cos$, $tan$, $abs$, $√$ as $sqrt$, $log$, $ln$), a few named constants ($e$, $\pi$), arbitrary constant numbers, and the single independent variable $x$. Functions and operators may be composed in arbitrary ways.

Let's have such a function $f(x)$ as an example for the rest of this question.

$$f(x) = \frac{1}{-100(x-0.4999)}+0.4999$$

(a steep quotient function centered around $(0.4999, 0.4999)$)

Normally, the graph should be plotted from $x = 0$ up to the first value of $x$ for which $f(x)$ is undefined (or until the line goes out of view, but I don't need help with that).

The problem I have is that my program uses numerical techniques to draw an approximation of the function. It samples functions at intervals of 0.0125 on the $x$ axis and connects the dots. Because of that, it only catches undefined values of $f(x)$ when $x$ is a multiple of 0.0125, which is not exactly suitable. In our example, $f(x)$ is undefined at $x=0.4999$, so my drawing function will simply connect the two points around it with a nearly vertical line.

I don't plan on making a full-blown symbolic solver as that would be completely overkill for my project. Are there numerical analysis techniques I could use to find undefined points on an arbitrary function, considreing that I only need to find the first one for which $x \geq 0$?

I have considered asking the question on Stack Overflow, but the question feels just too math-related for it.

share|cite|improve this question
What's your notion of a "2D function"? Your example is a function $f:\ x\mapsto f(x)$ of a single real variable. – Christian Blatter Oct 6 '12 at 18:42
You could check if the approximate gradient ${f(x + h)-f(x)\over h}>tol$ where $tol$ is some cutoff that you choose and is somewhat indicitive of what you would expect if a discontinuity were in the interval. If may not be so good of an approach if the discontinuity is near the middle of the interval. The only way to fix that is finer sampling of the function, I suspect. – Daryl Oct 6 '12 at 20:28
@ChristianBlatter, I mean a function that can be displayed on a 2D cartesian plane. – zneak Oct 6 '12 at 23:01
Each function has its valid domain of input values. You are to check that domain first. For example, with $\sqrt{x-1}$ your $x$ values need to stay away from values less than $1$. Next, you could check the denominator for values that would make it undefined (as the first part) or as zero. This is not an exclusive algorithm, but may work for your scope. – NoChance Oct 7 '12 at 2:48
@EmmadKareem, when a whole range of values is invalid, it's pretty easy since my sampling can't miss it. The real problem is when single points are undefined. I thought about checking for points known to be undefined too, but since functions are arbitrary, a denominator could be anything (for instance, $\frac{1}{\sqrt{(x+2)^3}+4x^6+5x^4+cos(x)}$ is an acceptable input for my program), and I would need to write a complete solver to find roots, and I have no idea how to do that. – zneak Oct 7 '12 at 15:07
up vote 2 down vote accepted

I ended up using a more adaptive sampling algorithm. To do this, I implemented an algorithm to derive the functions my program receives.

Before, I had a fixed sample rate of 0.0125 for $x$. Now, instead, I estimate the required increment such that the arc length between the current point and the next is approximately 0.0125. To do so, I evaluate the derivative of the function at the given point to get a slope. By multiplying the cosine of its arctangent by 0.0125, I get that approximate $x$ increment.

$$x_{n+1} = x_n + cos(tan^{-1}(f'(x_n)) \cdot 0.0125$$

The real formula for arc length, $\int_b^a \! {\sqrt{1 + f'(x)^2}} \mathrm{d}x$, would require me to implement integration, and that's an order of magnitude harder than derivation.

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.