MATLAB: Find value of x from curve

find value of x from curve

hi, i had values of y from 0 to 2pi, related to x values from 0 to 10 with step 0.5; i.e., i calculated the value of y using program for the values of x=0:0.5:10. i want to find the value of x from the plot(x,y), for cer
x=0:0.5:10;
y=0 0.383135237593798 0.762305633253970 1.12500381866696 1.47841592230188 1.82360426305799 2.16799972102249 2.50162151353647 2.83129834831420 3.15388959500014 3.47113990999891 3.77183336056194 4.06431023145887 4.35173946610087 4.63997932479654 4.92354827732318 5.20849294256939 5.49434987763603 5.77926587166893 5.77926587166893 6.28318530717959
find x for values of y=0 1.57079632679490 3.14159265358979 4.71238898038469;
thanks a lot for any helps

Best Answer

Use interp1, interpolating on the y values, reversing the x and y arguments. Your data contain two identical y-values, so it is necessary to cheat a bit and add a very small value (I chose 1E-15) to make them monotonically increasing in y. I sorted them first to be certain I wasn’t missing any of the repeated values:
x = 0:0.5:10;
y = [0 0.383135237593798 0.762305633253970 1.12500381866696 1.47841592230188 1.82360426305799 2.16799972102249 2.50162151353647 2.83129834831420 3.15388959500014 3.47113990999891 3.77183336056194 4.06431023145887 4.35173946610087 4.63997932479654 4.92354827732318 5.20849294256939 5.49434987763603 5.77926587166893 5.77926587166893 6.28318530717959];
yq = [0 1.57079632679490 3.14159265358979 4.71238898038469]; % Query Points
xy = [x' y']; % Create MAtrix For Sorting
[xys, idx] = sortrows(xy,2); % Sort
dup = find(diff([xys(:,2)]) == 0)+1; % Find Non-Increasing Values
xys(dup,2) = xys(dup,2) + [1:length(dup)]*1E-15; % Add ‘1E-15’ To Duplicates
xq = interp1(xys(:,2), xys(:,1), yq); % Interpolate To Find ‘x’ Values
figure(1)
plot(x, y, 'g')
hold on
plot(xq, yq, 'mp')
hold off
The ‘xq’ variable holds the x values you want that correspond to the y values in ‘yq’. The plot shows them.