Difference between revisions of "Understanding log plots"

From Course Wiki
Jump to: navigation, search
(Linear vs. log scale)
(Linear vs. log scale)
Line 13: Line 13:
  
 
==Linear vs. log scale==
 
==Linear vs. log scale==
On the linear scale that you are familiar with to, 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.
+
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.
  
[[Image:understandingLogsLinVLogScale.png]]
+
[[Image:understandingLogsLinVLogScale.png|center|thumb|500px]]
  
 
If you’d like to get a more in-depth explanation of the log scale [https://www.khanacademy.org/math/algebra2/exponential-and-logarithmic-functions/logarithmic-scale/v/logarithmic-scale Khan Academy] has a nice ~10 minute video on the subject.
 
If you’d like to get a more in-depth explanation of the log scale [https://www.khanacademy.org/math/algebra2/exponential-and-logarithmic-functions/logarithmic-scale/v/logarithmic-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 <tt> loglog </tt> command to use log-scale axes.
+
There are two ways to make a log-log plot in MATLAB. The first is to use the <tt>plot</tt> command to plot log(y) vs. log(x) on a linear scale.  
 
+
<pre> plot( log10(x), log10(y))
<pre> loglog( x, y)  
+
 
</pre>
 
</pre>
  
The nice thing about using this 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 it's factor of 10. So the tick to the right of <math>10^1</math> represents <math> 2\times 10^1</math>, the next is <math> 3\times 10^1,</math> and so on.  
+
Alternatively, you can use the <tt> loglog </tt> command to use log-scale axes.
  
Alternatively, you can use the <tt>plot</tt> command to plot log(y) vs. log(x) on a linear scale.
+
<pre> loglog( x, y)  
<pre> plot( log10(x), log10(y))
+
 
</pre>
 
</pre>
 +
 +
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 <math>10^1</math> on the x-axis represents <math> 2\times 10^1</math>, the next is <math> 3\times 10^1,</math> and so on.
  
 
Feel free to use whichever method you find most intuitive.
 
Feel free to use whichever method you find most intuitive.
[[Image:understandingLogsEquivalentPlots.png]]
+
[[Image:understandingLogsEquivalentPlots.png|center|thumb|800px|caption|Left: <tt> plot( log10(x), log10(y))</tt> Right: <tt>loglog( x, y)</tt>]]
  
 
==Plotting on log-log axes==
 
==Plotting on log-log axes==

Revision as of 17:42, 31 July 2017

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) $

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 on log-log axes

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 some are. Thankfully, the power law 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.

Clearly, the power law translates to a straight line on the log scale. We typically say that “it is linear on a log scale”. Notice that the ‘slope’ of this line is equal to the exponent n. (When we refer to the ‘slope’ of this line, we are talking about the how many major grid lines 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. 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 squish down into the x-axis, since $ x ^2 >> x $. On the log scale, you can clearly compare all functions from a huge range (in this case x = 0.1 to 100).

The reason that power laws come out as linear relates back to the properties of the logarithm. Remember that $ \log( x^n) = n\times \log(x) $, so plotting log(y) vs. log(x) is a line with a slope of n. Say we ask you to sketch something that has a power law behavior. We’ve showed you 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 which the line goes through. You can choose any point you want, but in this case, conveniently, when x = 1 all functions have y = 1. (I’ll let you think about why x = 0 would be a poor choice.) Now you have all you need. Give it a try on your own with $ x^3 $.

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)) $ is the same shape as $ \log(f(x)) $ but shifted by a constant $ log(a) $.

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


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 $:


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 $ x + x^4 $. When $ x <1 , x^4 << x $, 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.