How to identify points on a graph of multiple functions

2 visualizzazioni (ultimi 30 giorni)
I wish to identify the point where y=0 for four functions on the same figure with a dot. I understand that interp1 can be used do this but I plotted my functions using "fplot" and it seems that Interp1 doesn't work with fplot. So how do I do this?
Here is my code;
n1= exp(1);
n2= 2.95;
n3= 3.05;
n4= pi;
figure;
f1 = @(x) n1-tan((x-(pi/4))/5);
fplot(f1,[0,8])
hold on
f2 = @(x) n2-tan((x-(pi/4))/5);
fplot(f2,[0,8])
hold on
f3 = @(x) n3-tan((x-(pi/4))/5);
fplot(f3,[0,8])
hold on
f4 = @(x) n4-tan((x-(pi/4))/5);
fplot(f4,[0,8])
grid on
title('Plot of Functions')
xlabel('Y values')
ylabel('X values')
legend('n=e','n=2.95','n=3.05','n=pi')
Using Snipping tool, this is what I want to produce
Those coloured dots such that when hovered over it displays the values.

Risposta accettata

Tushar Behera
Tushar Behera il 6 Feb 2023
Modificato: Tushar Behera il 6 Feb 2023
Hi Samuel,
I believe you want to identify spots where y=0 on your plot.
To find the x values where y=0 for each function, you can use the fsolve function. fsolve finds the roots of a function, so you can use it to find the x values where each function crosses the y=0 line. Here's an example of how you can find the roots and plot them on the same figure:
n1= exp(1);
n2= 2.95;
n3= 3.05;
n4= pi;
figure;
f1 = @(x) n1-tan((x-(pi/4))/5);
fplot(f1,[0,8])
hold on
f2 = @(x) n2-tan((x-(pi/4))/5);
fplot(f2,[0,8])
hold on
f3 = @(x) n3-tan((x-(pi/4))/5);
fplot(f3,[0,8])
hold on
f4 = @(x) n4-tan((x-(pi/4))/5);
fplot(f4,[0,8])
grid on
% Find the roots using fsolve
x1 = fsolve(f1,pi/4);
x2 = fsolve(f2,pi/4);
x3 = fsolve(f3,pi/4);
x4 = fsolve(f4,pi/4);
% Plot the roots on the same figure
scatter(x1, 0, 'ro');
scatter(x2, 0, 'ro');
scatter(x3, 0, 'ro');
scatter(x4, 0, 'ro');
title('Plot of Functions with Roots')
xlabel('Y values')
ylabel('X values')
legend('n=e','n=2.95','n=3.05','n=pi')
I hope this is what you were looking for.
Regards,
Tushar

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by