54

I have a linear scale that ranges form 0.1 to 10 with increments of change at 0.1:
  |----------[]----------|
0.1         5.0         10

However, the output really needs to be:
  |----------[]----------|
0.1         1.0         10 (logarithmic scale)

I'm trying to figure out the formula needed to convert the 5 (for example) to 1.0. Consequently, if the dial was shifted halfway between 1.0 and 10 (real value on linear scale being 7.5), what would the resulting logarithmic value be? Been thinking about this for hours, but I have not worked with this type of math in quite a few years, so I am really lost. I understand the basic concept of log10X = 10y, but that's pretty much it.

The psuedo-value of 5.0 would become 10 (or 101) while the psuedo-value of 10 would be 1010. So how to figure the pseudo-value and resulting logarithmic value of, let's say, the 7.5?

Let me know if addition information is needed.

Thanks for any help provided; this has beaten me.

TurdPile
  • 976
  • 1
  • 7
  • 21

2 Answers2

64

Notation

As is the convention both in mathematics and programming, the "log" function is taken to be base-e. The "exp" function is the exponential function. Remember that these functions are inverses we take the functions as:

exp : ℝ → ℝ+, and

log : ℝ+ → ℝ.

Solution

You're just solving a simple equation here:

y = a exp bx

Solve for a and b passing through the points x=0.1, y=0.1 and x=10, y=10.

Observe that the ratio y1/y2 is given by:

y1/y2 = (a exp bx1) / (a exp bx2) = exp b(x1-x2)

Which allows you to solve for b

b = log (y1/y2) / (x1-x2)

The rest is easy.

b = log (10 / 0.1) / (10 - 0.1) = 20/99 log 10 ≈ 0.46516870565536284

a = y1 / exp bx1 ≈ 0.09545484566618341

More About Notation

In your career you will find people who use the convention that the log function uses base e, base 10, and even base 2. This does not mean that anybody is right or wrong. It is simply a notational convention and everybody is free to use the notational convention that they prefer.

The convention in both mathematics and computer programming is to use base e logarithm, and using base e simplifies notation in this case, which is why I chose it. It is not the same as the convention used by calculators such as the one provided by Google and your TI-84, but then again, calculators are for engineers, and engineers use different notation than mathematicians and programmers.

The following programming languages include a base-e log function in the standard library.

In fact, I cannot think of a single programming language where log() is anything other than the base-e logarithm. I'm sure such a programming language exists.

Marc Dirven
  • 309
  • 2
  • 18
Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
  • However, there is only 1 number being used to derive. That being the value on the linear scale. .1 to 10 is the physical value that the script sees. It need to take that corresponding value, and convert it to its logarithmic counterpart. IE: 5 would be converted to 1. If you need to see the example, I can link it to you. – TurdPile Oct 20 '13 at 04:42
  • @TurdPile: The *x* values are the linear scale values, and the *y* values are the logarithmic scale values. You asked to convert linear to logarithmic, and you just plug the numbers into the formula. In this case, the linear input of 5.00 would become 1.02 on the logarithmic scale. – Dietrich Epp Oct 20 '13 at 05:51
  • @TurdPile: The linear 5.00 would correspond to 1.00 exactly if the linear scale started at 0.0, but it starts at 0.1, hence the difference. – Dietrich Epp Oct 20 '13 at 05:52
  • Oh right. That was user error saying 5.0 should be exactly 1, since 5 is not the exact middle of 10 and .1. Technically 5.05 is the exact middle (10.1/2). But anyways, I would be foolish thinking there *wouldnt* be a margin of error. – TurdPile Oct 20 '13 at 15:53
  • So let me get this right: in my particular case, b is a static value. X and Y are the min/max values of the linear scale, and I solve for a to give the proper logarithmic equivalent? – TurdPile Oct 20 '13 at 15:56
  • x and y are variables. a and b are constants. – Dietrich Epp Oct 20 '13 at 17:53
  • Did you make a typo on equation for b? It equals .202-repeating – TurdPile Oct 20 '13 at 23:11
  • Can you solve for 5.05 using your example? I will understand faster if i see an example done. – TurdPile Oct 20 '13 at 23:27
  • Sure. x=5.05, so y = a exp bx = 1.0. There's no typo for the equation for b. log 10 ≈ 2.3, so 20/99 * log 10 ≈ 0.47. As a reminder, the "log" function is base e unless otherwise specified. – Dietrich Epp Oct 20 '13 at 23:41
  • Really? I've always been told log is base 10. huh. Google calc uses base 10 as a default as well. that's why log 10 becomes 1 when i calculated it. – TurdPile Oct 21 '13 at 03:45
  • @TurdPile: Base e is the convention in both mathematics and programming. Fire up your favorite programming language and run `Math.log()` or `#import ` or whatever. It's all base e. It's also common in scientific contexts. – Dietrich Epp Oct 21 '13 at 04:22
  • Clearly not since Google + TI-84 graphing calc + computer's calc all convert Log(10) to 1. Which is log with base 10. I do believe you are thinking natural log. Because that does indeed have base e. log(10) = 1, while ln(10) = 2.3. So does this mean where you say log, you really mean ln() ? – TurdPile Oct 21 '13 at 11:02
  • I just did your calculation for b and with ln it works. One more question and then i think I got this... what is exp? I thought EXP at first, but that doesn't allow decimals, so then i thought exponent, but that doesn't calculate out correctly. So am I correct to assume that `(exp bx) = e^bx` ? – TurdPile Oct 21 '13 at 11:19
  • If I am indeed on the right track, y1 / e^bx1 where y1,x1 = 10 (according to your calc for b), which would then mean a = 1.326, not the .095 you have. What did i mess up? – TurdPile Oct 21 '13 at 11:33
  • @TurdPile: I have expanded the section on notation. I can't follow your math. You say "would mean that a = 1.326", but the equations you supply do not contain the variable *a*, which means you left part of the equation out. You also write "y1,x1=10" but I'm not sure what that means because the left-hand side has two variables, but the right-hand side has one number. – Dietrich Epp Oct 21 '13 at 16:23
  • Part of what I was saying is that you were using log-base-e, which is ln() on calculators. The default of log() on most is indeed base 10. When I said y1,x1 I was plugging the values of 10 into both of those in the equation. – TurdPile Oct 21 '13 at 22:08
  • Anyways: Does the base of the log not matter as long as you solve for the ratio between the two? What i mean is would the logarithmic values converted from linear be the same between log_e vs log_10 as long as log_10 was solved properly (ie `y = a 10 bx` instead of `y = a exp bx`)? – TurdPile Oct 21 '13 at 22:10
  • IE: `y = a 10^bx`. `y1/y2 = 10^b(x1-x2)` and so on, solve for b, then a. Same steps, right? – TurdPile Oct 21 '13 at 22:45
  • @TurdPile: Proving or disproving that the base doesn't matter is a straightforward exercise, which I recommend if you want to review exponentials and logarithms. (I feel at this point in the conversation I am done doing your math, not to be rude.) – Dietrich Epp Oct 21 '13 at 23:42
  • Yes you are, which is why I gave you the accepted answer. Thanks for your time and (steadily decreasing) patience. Cheers. – TurdPile Oct 22 '13 at 03:41
  • @DietrichEpp I just found your excellent answers regarding the horizontal logarithmic scale. I was trying to modify your calculations to draw a vertical (Y axis) scale in Swift. I had no luck so far. I mentioned Swift because the coordinate system in this language starts from the top-left corner. Any help will be welcome. Please let me know if you want me to create a new question for it. Thanks! Janusz – Janusz Chudzynski Dec 23 '15 at 01:05
  • @JanuszChudzynski: The coordinate system actually has nothing to do with the language you use. Typically, with Quartz, you will find the default origin to be the lower left, not the upper left. This question is about a simple 1D scale, so 2D graphics are something else entirely. I also don't know what your question is. "No luck so far? Well, then, better luck in the future!" If you have a concise question that you can actually ask, then go ahead and make it into its own question. – Dietrich Epp Dec 23 '15 at 01:10
  • @DietrichEpp Thanks for getting back to me. Could you have a look at: http://stackoverflow.com/questions/34427225/graph-with-logarythimic-y-axis-in-swift – Janusz Chudzynski Dec 23 '15 at 01:32
  • @DietrichEpp sorry to bother you again: could you tell me if I implemented your function correctly: let y = a * pow(CGFloat(M_E),(x * b)) ? a and b are the same as in your example but I am getting different results. For example for x = 5 I am getting ~0.97 – Janusz Chudzynski Dec 23 '15 at 04:45
  • @JanuszChudzynski: Here's a chat link: http://chat.stackoverflow.com/rooms/98763/graph-with-logarythimic-y-axis-in-swift – Dietrich Epp Dec 23 '15 at 04:52
13

I realize this answer is six years too late, but it might help someone else.

Given a linear scale whose values range from x0 to x1, and a logarithmic scale whose values range from y0 to y1, the mapping between x and y (in either direction) is given by the relationship shown in equation 1:

 x - x0    log(y) - log(y0)
------- = -----------------      (1)
x1 - x0   log(y1) - log(y0)

where,

x0 < x1
{ x | x0 <= x <= x1 }

y0 < y1
{ y | y0 <= y <= y1 }
y1/y0 != 1   ; i.e., log(y1) - log(y0) != 0
y0, y1, y != 0

EXAMPLE 1

The values on the linear x-axis range from 10 to 12, and the values on the logarithmic y-axis range from 300 to 3000. Given y=1000, what is x?

Rearranging equation 1 to solve for 'x' yields,

                 log(y) - log(y0)
x = (x1 - x0) * ----------------- + x0
                log(y1) - log(y0)

                log(1000) - log(300)
  = (12 - 10) * -------------------- + 10
                log(3000) - log(300)

  ≈ 11

EXAMPLE 2

Given the values in your question, the values on the linear x-axis range from 0.1 to 10, and the values on the logarithmic y-axis range from 0.1 to 10, and the log base is 10. Given x=7.5, what is y?

Rearranging equation 1 to solve for 'y' yields,

          x - x0
log(y) = ------- * (log(y1) - log(y0)) + log(y0)
         x1 - x0

        /  x - x0                                \
y = 10^|  ------- * (log(y1) - log(y0)) + log(y0) |
        \ x1 - x0                                /

        / 7.5 - 0.1                                  \
  = 10^|  --------- * (log(10) - log(0.1)) + log(0.1) |
        \  10 - 0.1                                  /

        / 7.5 - 0.1                    \
  = 10^|  --------- * (1 - (-1)) + (-1) |
        \  10 - 0.1                    /

  ≈ 3.13

:: EDIT (11 Oct 2020) ::

For what it's worth, the number base 'n' can be any real-valued positive number. The examples above use logarithm base 10, but the logarithm base could be 2, 13, e, pi, etc. Here's a spreadsheet I created that performs the calculations for any real-valued positive number base. The "solution" cells are colored yellow and have thick borders. In these figures, I picked at random the logarithm base n=13—i.e., z = log13(y).

Spreadsheet values
Figure 1. Spreadsheet values.

Spreadsheet formulas
Figure 2. Spreadsheet formulas.

Mapping of X and Y values
Figure 3. Mapping of X and Y values.

Jim Fischer
  • 405
  • 4
  • 13
  • In example 2 you used wrong basis of power. Should be `e` I think. – Lynx Oct 11 '20 at 08:29
  • @Lynx, try the calculations exactly as shown in Example 2. This method works for any real-valued positive number base, n = 10, 2, e, etc. See also the information I added to my answer on October 11, 2020. – Jim Fischer Oct 11 '20 at 16:28
  • You are right, I just assumed in your calculation you used loge(x) function, so the basis of power should also be `e`. Am I correct? – Lynx Oct 11 '20 at 17:20
  • @Lynx: The important thing to remember is this: choose any convenient logarithm base number 'n', and use that base number from beginning to end in your calculations. For some positive real number 'n': log_n(y)=z and n^z=y. (NB: log_n(y) does not imply ln(y).) When Euler's constant 'e' is selected as the logarithm base number (n:=e), then one is working with the so-called "natural" logarithm: log_e(y)=ln(y)=x and exp(x)=e^x=y. In my examples, had I used ln(y)=z, you are correct that I must then use exp(z)=e^z=y to recover the y value from the z value. – Jim Fischer Oct 11 '20 at 21:40
  • This is a great answer. One question: how do you find what base should you select? – jash101 Nov 11 '20 at 16:42
  • @jash101, the logarithm base number can be any positive real value 'n'. The calculated result values in cells B6 and D13 are the same values (11.0 and 3.13) for all logarithm base numbers n. For example, had I chosen log base n=10, the calculated values in cells B6 and D13 would still be 11.0 and 3.13. Likewise for n=π, or n=e. The only constraint is that you use the same logarithm base number n throughout your calculations. – Jim Fischer Nov 12 '20 at 21:29