how to display an output on a plot itself?

Hi friends,
for this code:
x = linspace(-5,2);
y = -x-3;
z = 2*x
figure(1)
plot(x, y)
hold on
plot(x, z)
legend('x','y','location','best')
ix = interp1(y-z,x,0)
How can I cahnge this code and what coding line I should add so the solution (intersection point of y and z function, i.e., ix = -1) be displayed on the plot itself in a box?
Thank you

 Respuesta aceptada

Sam Chak
Sam Chak el 17 de En. de 2023
Editada: Sam Chak el 17 de En. de 2023
Do you want to plot something like this?
x = linspace(-5, 2, 701);
y = - x - 3;
z = 2*x;
figure(1)
plot(x, y)
hold on
plot(x, z)
ix = interp1(y-z, x, 0);
iy = 2*ix;
plot(ix, iy, 'o', 'markersize', 15, 'linewidth', 3)
xlabel('x'), grid on
legend('y', 'z', 'location', 'best')
txt = ['\leftarrow intersection: x = ' num2str(ix)];
text(-1+0.25, -2, txt)

4 comentarios

Mohiedin Bagheri
Mohiedin Bagheri el 17 de En. de 2023
Thank you for your reply. @Sam Chak
I meant the answer to this equation (y = z) which is "x = -1" be desplayed on the plot. Is there anyways to modify the code so the output for "ix = interp1(y-z, x, 0)" be displayed on the plot?
Sam Chak
Sam Chak el 17 de En. de 2023
You are welcome, @Mohiedin Bagheri. I have edited my Answer above according to your description. If you find the MATLAB code is helpful, please consider accepting ✔ and voting 👍 the Answer. Thanks a bunch! 🙏
Mohiedin Bagheri
Mohiedin Bagheri el 17 de En. de 2023
Editada: Mohiedin Bagheri el 17 de En. de 2023
@Sam Chak Thank you very much Sam. I appreciate your help.
All done! Voted and Accepted the Answer :)
Thanks buddy
Sam Chak
Sam Chak el 17 de En. de 2023
@Mohiedin Bagheri, Don't mention it. Who knows I'll need your help in future.

Iniciar sesión para comentar.

Más respuestas (1)

Abhinav
Abhinav el 17 de En. de 2023

0 votos

Here is the link to find the intersection point of two line segments/lines. A fast two line intersection point finder based on the line parametric space. Finds the intersection point between two lines if it exists or else submits NaN.

Categorías

Más información sobre Graphics en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 17 de En. de 2023

Comentada:

el 17 de En. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by