Search code examples
matlablinear-regressionextrapolation

Extrapolate in log scale on y axis


x = [0,1,2,3,4,5,6,7] y = [0.07, 0.05, 0.03, 0.02, 0.01, 0.005, 0.002, 0.0007]

I want to find what x is when y= 0.000001 and I tried below but it gives me a wrong value.

10^(interp1(log10(y),x,10^-6, 'linear','extrap'))

Also, would linear extrapolation be possible if I only had two points like so,

x = [6,7] y = [0.002, 0.0007]

Solution

  • interp1's linear-extrap function simply extends the last (or first) segment in the piecewise linear fit made from the points. It doesn't actually create a least-squares fit. This is very evident in the image below:

    enter image description here

    How did I get this image? I fixed the following problems with your code: You are interpolating log10(y) vs x.

    • So the third argument for interp1 needs to be log10(new_y). For new_y = 10^-6, you actually need to pass -6.
    • The call to interp1() will give you new_x. You're raising 10 to the result of interp1, which is wrong.
    x = [0,1,2,3,4,5,6,7];
    y = [0.07, 0.05, 0.03, 0.02, 0.01, 0.005, 0.002, 0.0007]
    
    logy = log10(y);
    
    plot(logy, x, '-x');
    
    new_y = 10^-6;
    new_x = interp1(logy, x, log10(new_y), 'linear', 'extrap')
    
    plot(log10([new_y, y(end)]), [new_x, x(end)], '--r');
    
    plot(log10(new_y), new_x, 'or');
    xlabel('log10(y)'); ylabel('x');
    

    The short answer to your second question is yes!.

    Longer answer: replace x and y in my code above and see if it works(spoiler: it does). enter image description here

    Note: I ran this code in Octave Online because I don't have a local MATLAB installation. Shouldn't make a difference to the answer though.