Understanding log plots

From Course Wiki
Revision as of 18:28, 31 July 2017 by Juliesutton (Talk | contribs)

Jump to: navigation, search

Log plots pop up in 20.309 surprisingly frequently. The goal of this page is to help you get familiar enough with log plots so that you can easily interpret and sketch functions on a log scale.

Basics

Remember the definition of a logarithm:

  • If $ y = 10^a $, then $ \log_{10} y = a $

And some of its useful properties:

  • $ \log(a\times b) = \log(a) + \log(b) $
  • $ \log(a^c) = c \times \log(a) $

Note that unless otherwise stated, we will use $ \log $ and $ \log_{10} $ interchangeably. Be careful that in MATLAB, log() refers to the natural logarithm ($ \ln $), and log10() refers to log base 10 ($ \log_{10} $).

Linear vs. log scale

On the linear scale that you are familiar with, moving a fixed distance along an axis is equivalent to adding a fixed number to your starting point. On a log scale, moving a fixed distance along an axis is equivalent to multiplying the starting point by a fixed number.

UnderstandingLogsLinVLogScale.png

If you’d like to get a more in-depth explanation of the log scale Khan Academy has a nice ~10 minute video on the subject.

There are two ways to make a log-log plot in MATLAB. The first is to use the plot command to plot log(y) vs. log(x) on a linear scale.

 plot( log10(x), log10(y))

Alternatively, you can use the loglog command to use log-scale axes.

 loglog( x, y) 

These two examples are shown in the plot below. The nice thing about using the second method is that you can easily read of the x and y values directly instead of having to do math in your head. And we all know how much BE students love to do math in their heads! Typically, each tick mark represents an integer times its factor of 10. So, for example, the tick to the right of $ 10^1 $ on the x-axis represents $ 2\times 10^1 $, the next is $ 3\times 10^1, $ and so on.

Feel free to use whichever method you find most intuitive.

Left: plot( log10(x), log10(y)) Right: loglog( x, y)

Plotting power laws

So, what happens when you plot things on a log scale? Well, in general, small things get stretched and big things get squished. Not all functions are easy to translate from linear to log, but the power law is. Thankfully this is a function that comes up frequently, so let’s look at it in detail.

A power law is any function of the form $ y = x^n $, where n is some constant. Here are a few familiar examples for n= 1, 2, and 0.5 plotted on a typical (linear) scale and on a log-log plot.

UnderstandingLogsPowerLaw.png

Clearly, the power law looks like a straight line on the log scale. Remember that $ \log( x^n) = n\times \log(x) $, so plotting log(y) vs. log(x) is a line with a slope of n. We typically say that “it is linear on a log scale”. When we refer to the ‘slope’ of this line, we are talking about the apparent slope of the line on the log log plot. I.e. how many major grid lines do you cross in y when you increase in x by a single major grid line? For example, for $ y = x^2 $, when x goes from 1 to 10, y increases by a factor of 100 - or by 2 grid lines, so we’d say the log-log plot of $ x^2 $ has a slope = 2.

What if we ask you to sketch a function that has a power law behavior? You now know that the slope of this line would be equal to the power (n), but to plot a line you need one more piece of information. You must choose a point through which the line passes. You are free to choose any point you want, but it can sometimes be convenient to choose the point $ x= 10^0 = 1 $. (I’ll let you think about why x = 0 would be a poor choice.) In the above case, conveniently, when x = 1 all functions have y = 1, so they all pass the same point. Now you have all you need. Give it a try on your own with $ x^3 $.

An important observation we can make from the above comparison is that you can plot a huge range of values on a log plot. On the linear scale, the range of x values we plotted was only 0 to 3. If we were to plot a larger range of x values, the functions $ y = x $, or $ y = \sqrt{x} $ would get squished down into the x-axis because $ x^2 >> x $ for large x. But on the log scale, you can clearly compare all functions from a huge range (in this case x = 0.1 to 100).

Multiplying by a constant

What happens if you multiply a function by a constant on a log scale? Looking at the math: if $ y = a\times f(x) $, then $ \log(y) = \log(a)+ \log(f(x)) $. In other words, the plot will have the same shape as $ \log(f(x)) $ but will be shifted by a constant offset $ \log(a) $.

Here’s the plot for the case where $ y = x^2+c $:

UnderstandingLogsMultiplyByConstant.png

Adding a constant

If we add a constant to a function, there is no convenient log property that we can rely on to simplify the expression $ \log(a+x) $. Let’s just see what happens to the plot when add different constants to $ x^2 $:

UnderstandingLogsAddConstant.png

Interesting: the function is unchanged when x is large, but then asymptotes to different values for small x.

This brings us to another useful property of log plots. They highlight limiting behavior of a function. In the above case for $ y = a + x^2 $, when $ x^2<<a $, then $ y\approx a $, but when $ x^2>>a, y\approx x^2 $. We can then easily sketch any function by connecting the limiting behaviors.

Let’s try plotting $ y = x + x^4 $. When $ x <1 , x << x^4 $, so $ y \approx x $. But for $ x>1 $, x becomes negligible so that $ y\approx x^4 $. If we plot these two extremes, they approximate the original function very well, except for a small region around the ‘corner’ where the two lines meet. If you're sketching this function by hand, it's ok to approximate what's going on in the corner region, as long as you have the right limiting behavior.

UnderstandingLogsLimits.png