1

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.

cmp
  • 21
  • 5

1 Answers1

0

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.

cmp
  • 21
  • 5
  • The "spline" option works fine here, and by the way I can see nothing in your link that suggests that "spline" wouldn't work. The difference is that I actually **tested** the command before proposing it... – PierU Apr 14 '23 at 07:08
  • Thank you for your patience PierU, I tested the interpolation with 'spline' and received the following error: interp1: discontinuities not supported for METHOD 'spline'. When I use 'linear' I don't get the error. You'll see at the bottom of the link by searching for 'discontinuous' that only 'linear' and 'nearest' work when there is discontinuity. I'm not sure why this makes a difference, but for me it does. Thanks again for your answer! – cmp Apr 14 '23 at 23:26
  • 1
    This is because your `spline` array contains duplicates. You have to remove them with the `unique` function: `[spline_u index] = unique(spline,"stable")`, then `x0 = interp1(spline_u,xint(index),y0,"linear")`. By the way I don't get the point of your initial interpolation at all: `xint` is equal to `x`, so `spline` is equal to `y`, i.e. you don't interpolate nothing. – PierU Apr 17 '23 at 15:03