# How do computers draw function graphs?

Could anyone give me a general overview of the algorithm computers use to draw graphs?

I'm guessing it is either by graphing many, many points and just connecting them or by doing a general study of the function with the derivative. However, just taking a derivative is not enough to know the exact behavior of the function in an interval, since you would have to evaluate each point of the interval in the derivative, leaving us with the first method again...

So how is it done?

-
It varies from program to program, but evaluating a bunch of points and connecting them with straight lines is very common. You can check by evaluating a high frequency periodic function (e.g., $\sin(10000x)$ on the interval $[-1,1]$) and looking at the resulting plot. If the resulting plot doesn't look periodic, then it's pretty likely the underlying algorithm is just evaluating at a bunch of points. An example. – Snowball Jan 11 '14 at 2:26
– lhf Jul 8 '14 at 11:45

There are many different plotting libraries, and they all work differently.

But in general, a graph requires data, usually in the form of x-axis and y-axis data.

Even if you pass a symbolic function, (e.g. plot(x^2,x=0..10)), typically the computer discretizes your domain, computes a finite set of vectors, and plots straight lines between those points. So (x1,y1) connects to (x2,y2); (x2,y2) connects to (x3,y3); etc.

Sometimes, the plotting library is smart enough to know the display window size and the monitor resolution, and it chooses points that are sufficiently close that you cannot tell that it is a series of straight lines.

However, some libraries know that we like smooth things. So they do some sort of spline interpolation of the data to make it happy.

Things get a little more interesting when talking about scalable vector graphics (SVG). In SVG, the function and/or the data can be converted to a parameterized vector-valued function. Then, the plotting library plots the vector as it sweeps through its parameterization, often by painting pixels directly.

Back in the day, computers were only capable of displaying text in a fixed-format display. How did computers plot functions? By using ASCII characters and putting -, |, \, and / symbols in the appropriate location! If you dig up some engineering papers from the 60s and 70s you might even find some of these plots.

In the end, there are hundreds of plotting libraries, and all of them have different ways of representing the data.

-

Whatever the method of plotting a function is used, in the end when the graph is to be drawn, it must draw the graph with finitely many points.

The simplest method is just plotting many points of the function (requires a lot of points to make it look like a continuous curve). Another method is using fewer points but connecting them with straight lines; but in the end you have to plot some finite points which are on the small line segments (you only evaluate the function at fewer points). Other methods can be used to approximate the function such as cubic splines but in the end you have to plot points.

In fact, in many programming languages (such as Matlab, Octave, Python), you only have the option of plotting points. So you have to explicitly create arrays of points of the graph and use the plotting function to plot all those points (and the points are then connected by lines, which becomes apparent if you used a very small sample of point of the graph).

-

With the power of todays computers . . . calculate LOTS of points along the graph. Calculate and plot each point - a thousand or so per inch. One winds up with a very smooth graph that way.

-