שתף באמצעות


Interpolate X at Known Y Using Semi-Log of LOG-X and Linear-Y

Question

Sunday, December 13, 2015 6:17 PM

Hi there,

I was hoping somebody smarter than me could help figure this out. I have a simple chart in which the X axis is logarithmic and the Y axis is linear. On the chart, I have one straight line with the following coordinates:

POINT1
X1 = 0.08
Y1 = 19.99

POINT2
X2 = 0.01
Y2 = 5.0

The value of these points will vary but it will always be a straight line with two coordinates as above.

I need to interpolate what the log-X value will be given a linear-Y value of 10.

Here's what I have so far, but it's not bringing forth the correct interpolation:

Dim X1, X2, Y1, Y2 as Double            X1 = 0.08
            Y1 = 19.99
            X2 = 0.01
            Y2 = 5

            Dim A, B, C As Double

            A = (Math.Log(X2) - Math.Log(X1)) / (Y1 - (Y2 - Y1))
            B = (Math.Log(X2) - Math.Log(X1)) / (Math.Log(X1) - (Y2 - Y1))
            C = A * B * 10

It returns 0.0991...

The answer should be about 0.02 and I can't figure out where the problem lies in my equation. Any help would be appreciated, thanks...

Shawn

All replies (7)

Monday, December 14, 2015 2:43 PM ✅Answered | 1 vote

Since your logarithmic scale is special, try this calculation:

    Dim X1a = MyLog(X1)

    Dim X2a = MyLog(X2)

    Dim Xa = X2a - (X2a - X1a) * (Y - Y2) / (Y1 - Y2)

    X = MyExp(Xa)

where:

    Function MyLog(v As Double) As Double

        Return -Math.Log10(v / 100)

    End Function

    Function MyExp(v As Double) As Double

        Return Math.Pow(10, 2 - v)

    End Function

It gives 0.02.


Monday, December 14, 2015 7:49 AM

Try the next formula:

    Dim X, Y As Double

    Y = 10

    X = Math.Log((Y - Y2) * (Math.Exp(X1) - Math.Exp(X2)) / (Y1 - Y2) + Math.Exp(X2))

It gives 0.034. Are you sure that 0.02 is the answer?


Monday, December 14, 2015 1:43 PM

Thank you kindly for your response. I'm positive the answer is 0.02. If you look at the image below of my chart, you'll see that the point at which the line crosses over the 10 (Y) is just shy of 0.02. Your number is a lot closer than mine was...


Monday, December 14, 2015 1:46 PM

Thank you kindly for your response. I'm positive the answer is 0.02. If you look at the image below of my chart, you'll see that the point at which the line crosses over the 10 (Y) is just shy of 0.02. Your number is a lot closer than mine was...

I'll try to come back to this tonight when I get back.

If I had eight hours to chop down a tree, I'd spend six sharpening my axe. -- Abraham Lincoln


Tuesday, December 15, 2015 1:56 AM

That works! Thank you so much! I have marked it as answered. Some of these calculations go way over my head, thanks a lot!

Shawn


Tuesday, December 15, 2015 6:33 PM

That works! Thank you so much! I have marked it as answered. Some of these calculations go way over my head, thanks a lot!

Shawn

Hi,

it works, but maybe you are mixing a decimal (normal) value-representation with a logarithmic one (think of Benford's Law)?

https://en.wikipedia.org/wiki/Benford's_law

(see relative size of P)

Shouldnt the labelling of the x-axis be somewhat like -2, -1, 0 ... instead of 0.01, 0.1, 1 ... ?

Some code to experiment with:

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim X1, X2, Y1, Y2 As Double

        X1 = 0.08
        Y1 = 19.99
        X2 = 0.01
        Y2 = 5

        'graph is linear ("a line") in a logarithmic x-axis chart, so
        'for our equation, we need the correct logarithmic representation
        'of the x-values (which are in normal, decimal representation)

        'the equation is (linear algebra line representation with point + mu*direction vector)
        '(X1, Y1)T + mu*(dx / dy)T = (x, 10)T
        'and with the above correction:
        '(Math.Log(X1, 10), Y1)T + mu*(1, dy/dx)T = (XL, 10)T where dy/dx is  (Y2 - Y1 / Math.Log(X2, 10) - Math.Log(X1, 10)) and
        'XL is the logarithmic representation's result -> so normal would be Math.Pow(10, xl)

        Dim dy As Double = Y2 - Y1
        Dim dx As Double = Math.Log(X2, 10) - Math.Log(X1, 10)

        Dim mu As Double = (10.0 - Y1) * dx / dy
        Dim xl As Double = Math.Log(X1, 10) + mu

        'result XL
        MessageBox.Show(xl.ToString) 'this is the linear (decimal distance) value in the logarithmic scale 

        '"percentage" of shift of result from 0.01 (= -2 as log), 
        'which indicates, it is close to the value where the logarithmic representation changes a/(the first) fractional digit (see Benford's Law)
        MessageBox.Show(((0.01 - (-2 - xl)) * 100).ToString)

        'your normal, decimal representation result of 0.2, but verify that this is really the value you need...
        MessageBox.Show(Math.Pow(10, xl).ToString)
    End Sub

Regards,

  Thorsten


Sunday, December 20, 2015 2:53 PM

Thanks Thorsten,

The x axis has to go from 100 to 0.001. It's a technical application for gradation of soil. Thanks a bunch!

Shawn