How do I find the instersection on my plot?

I have a plot with the errorbar. The data coming from a matrix 21x6 and the errorbar from another matrix. I need to find where (for a given y-axis value) the functions intersect the curves and at what coordinate. Since the values of the graph are given by a matrix, only the y axis correspond to the physical values, for the x axis I changed the values via
xticks([1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21])
xticklabels({'-20','-18','-16', '-14', '-12', '-10', '-8', '-6','-4','-2','0','2', '4', '6', '8', '10', '12', '14', '16', '18', '20'})
The graph is this one:

 Réponse acceptée

Matt J
Matt J le 11 Oct 2021
Modifié(e) : Matt J le 11 Oct 2021
You can use fzero, e.g.,
fun=@(x)x^2;
y=4;
x_intersect=fzero(@(x) fun(x)-y,[0,1e6])
x_intersect = 2.0000

12 commentaires

Matt J
Matt J le 11 Oct 2021
Modifié(e) : Matt J le 11 Oct 2021
If you only have discrete samples of the curves, you will have to select an interpolation model, e.g.
xdata=(0:0.3:10); ydata=xdata.^2; %plotted samples
fun=griddedInterpolant(xdata,ydata,'linear'); %interpolator
y=4; %given y value
x_intersect=fzero(@(x) fun(x)-y,[0,1e6])
x_intersect = 1.9949
I don't understand
xdata=(0:0.3:10);
ydata=xdata.^2;
Can you explain?
Matt J
Matt J le 11 Oct 2021
It's just example plot data.
HelpAStudent
HelpAStudent le 11 Oct 2021
Ok, I think you does not understand my problem. My plot is a matrix. I have 6 curves on the plot because the matrix it's 21x6. A curve was drawn from the 21 values ​​identified. For a total of 6 curves with 21 values ​​each (the dots in the graph). If you see the graph I know a value of the yaxis but it does not correspond on something because the plot is made of the matrix
Matt J
Matt J le 11 Oct 2021
You Accept-clicked the answer so I assume you figured it out? If not, I don't see how my answer is invalid. If you have 6 curves, just apply my technique 6 times, or use a loop.
HelpAStudent
HelpAStudent le 11 Oct 2021
Your answer it's not ok, because you don't use the right function: you used fun=@(x)x^2; but I have a matrix, so I don't know the equation for my function. I just wanna know HOW from the graph that I share with you I can find where for y=80 the courves interpolate. The courves are the columns of the matrix and the rows are the point (the dots on the plot)
Matt J
Matt J le 11 Oct 2021
Modifié(e) : Matt J le 11 Oct 2021
I already showed you how to modify the technique when you only have discrete plot data xdata and ydata. Here it is again.
xdata=(0:0.3:10); ydata=xdata.^2; %plotted samples
fun=griddedInterpolant(xdata,ydata,'linear'); %interpolator
y=4; %given y value
x_intersect=fzero(@(x) fun(x)-y,[0,1e6])
HelpAStudent
HelpAStudent le 11 Oct 2021
Yes, but the y data are not the power of the x data as you write. And I can't give an y value because I have just the matrix of the value. I did not create a plot from x and y values, I create a plot from the value and matlab create somehow the value of the x axis and y axis, but I have them only on the graph, not as a variable in my script
Matt J
Matt J le 11 Oct 2021
Modifié(e) : Matt J le 11 Oct 2021
The ydata would be a particular column in your matrix, which you do have. The xdata, based on what you posted, would just be,
xdata=-20:2:20
xdata = 1×21
-20 -18 -16 -14 -12 -10 -8 -6 -4 -2 0 2 4 6 8 10 12 14 16 18 20
In other words, you could have (and probably should have) plotted the data as follows, rather than manipulating the xticklabels.
plot(xdata,Matrix)
I do not have ydata as the columns of my matrix. As you can see:
meanbasketT = transpose(meanbasket);
y = meanbasketT;
e = transpose(devstdbasket);
figure; errorbar(y,e,'-o')
legend({'Function 1', 'Function 2', 'Function 3', 'Function 4', 'Function 5', 'Function 6'}, 'Location', 'northwest', 'FontSize',14 );
xticks([1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21])
xticklabels({'-20','-18','-16', '-14', '-12', '-10', '-8', '-6','-4','-2','0','2', '4', '6', '8', '10', '12', '14', '16', '18', '20'})
HelpAStudent
HelpAStudent le 11 Oct 2021
The meanbasketT is 21x6 that's why I have 6 function and 21 dots for function
Matt J
Matt J le 11 Oct 2021
Modifié(e) : Matt J le 11 Oct 2021
The ydata is not a particular column. The column as I told you before are the different line
Yes, the ydata in my original example also represented one particular line, but as I told you you can loop over your matrix columns and apply my technique to each.
xdata=-20:2:20
for i=1:6
ydata=Matrix(:,i);
y=...
fun=griddedInterpolant(xdata,ydata,'linear'); %interpolator
x_intersect(i) = fzero(@(x) fun(x)-y,[0,1e6])
end

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by