How to find x-values when y-function equals a set value?

110 views (last 30 days)
Hi, there!
New to matlab.. I have a dilema trying to find the x-values for a function =0.7(see figure). And I should be able to find both values of x when the bell-curve changes.
Big help, please! I've tried fzero, and other functions and mostly does't work or it will just give me one single value of x. there should always be 2.
Thank you in advance!
x=[0.46 0.7]
f=(a1*exp(-((x-b1)/c1).^2))=0.7
The a1, b1 and c1 values are known.

Accepted Answer

Star Strider
Star Strider on 16 Oct 2020
Edited: Star Strider on 18 Oct 2020
Try this:
x = linspace(0.54, 0.61); % Create Data
y = exp(-(x-0.58).^2*1E+4); % Create Data
idx = find(diff(sign(y-0.7)));
for k = 1:numel(idx)
idxrng = [-1 1]+idx(k);
x7(k) = interp1(y(idxrng), x(idxrng), 0.7); % Interpolate To Find Intersection ‘x’-Value
end
figure
plot(x, y)
hold on
plot(xlim, [1 1]*0.7, ':k', 'LineWidth',1) % Reference Line
plot(x7, [1 1]*0.7, 'pg','MarkerFaceColor','g','MarkerSize',10) % Intersections
plot([1 1]*x7(1), [min(ylim) 0.7], ':k')
plot([1 1]*x7(2), [min(ylim) 0.7], ':k')
hold off
legend('Data','Reference','Intersections', 'Location','NW')
Use your own function for ‘y’. I would have used it, however the parameters are nowhere to be found.
EDIT — (18 Oct 2020 at 19:37)
Added plot image —
.
  7 Comments
Coryn Melissa LLamoza Carabali
Sir/Madam, May God bless you, your family and everything you do! you just saved my bachelor project, It works perfectly. Thank you very much
The reason for the fitting was to connect the data points which I thought necessary to find the x_values at y=0.7. would you care to explain a little bit on how you made this possible without a fitting? again, thanks.
Star Strider
Star Strider on 20 Oct 2020
As always, my pleasure!
After about a half hour of experimentation with the fitting (all unsuccessful), I simply used the raw data, and use the if block to trap segments that either did not reach the 0.7 level or that did not have 2 intercepts with it.. The fitting is likely not necessary (or at least a different model would be necessary, since the Gaussian gives a very poor approximation). I do not know what your data are, however if you want to fit them (although I do not recommend that), a sinc function of some sort (with more parameters, for example amplitude, position, and width) could be more appropriate than the Gaussian.
(For the record, I am male.)

Sign in to comment.

More Answers (2)

KSSV
KSSV on 16 Oct 2020
You can take your curve as L1 and define your straight line at y = 0.7 as curve L1 and use InterX. Get the function InterX from:

Alan Stevens
Alan Stevens on 16 Oct 2020
With your particular function you can get both values of x immediately from
x = b1 + c1*sqrt(log(a1/0.7))*[1, -1];
  1 Comment
Coryn Melissa LLamoza Carabali
Hi, thanks for your answer! It really seems to work for the formula I gave above which corresponce to a Gauss1 fit. But as it happens it is not fitting my data anymore and I'm using Gauss2 now:
y=a1*exp(-((x-b1)/c1).^2)+a1*exp(-((x-b1)/c1).^2)
and with this one the method you gave is not working :(

Sign in to comment.

Categories

Find more on Interpolation in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!