Search code examples
octaveinterpolationxticks

how to interpolate y to get x from plotted data in Octave


I have a 2D graph of data. Instead of interpolating y from x, I need to determine the x value given y. I can't find any information on this specifically for Octave. Would someone please enlighten me? My code is below:

I also would really appreciate it if you can tell me how to display my xticklabels as decimals (i.e. 0.0014 instead of 1.4e-03). Thanks in advance for any help you can offer!

# here is the data, I need it on a semilogx plot
    x=[75 50 38 25 19 12 10 5 2 0.85 0.425 0.25 0.15 0.075 0.0296 0.0198 0.0121 0.0087 0.0063 0.0032 0.0014]
    y=[100 100 100 100 100 100 100 100 100 99 98 95 92 88.7 79.1 66.2 50.4 43.5 37.6 30.6 23.7]
    
#`````````````````````````   Print data above

xlim=logspace((xmin), 1, (xmax));
graph=semilogx(x,y, 'ro', 'color', 'magenta', 'linewidth', 1.0, 'marker', 'o');
set (gca(), "fontsize", 10, 'ticklength', [0.015, 0.01], 'ylim', [0 100]);
set (gca(), "ylabel", text("string", "Percent Passing, [\\%]", 'fontsize', 14), 'xlabel', text('string', "Particle Size, [mm]", 'fontsize', 14))
set (gca(), 'title', text('string', "{\\bf Particle Size Distribution of Soil}", 'fontsize', 14));
set (gca(), 'xtick', [xmax 10 5 2 0.85 0.425 0.25 0.15 0.075 0.037 0.01 0.005 0.002 0.001 0.0005] , 'fontsize', 10);

#`````````````````````````   Determine Curve Fit
xint=x;
spline = (interp1 (x, y, xint, "spline"));
hold on;
semilogx(xint,spline,'m--');
print(fig, "-depslatex",  figpath);
hold off

# end file

Here is my output

I tried looking up answers for Matlab, but they don't seem to apply to graphs in Octave due to different function names.


Solution

  • Similar to what PierU states above, x can be interpolated from y using the following code:

    x0 = interp1(spline, xint, y0, "linear")
    

    where y0 is the y value you would like to get the corresponding x value for. Note that 'spline' method seems to not be permitted for this operation, as described here.