How to identify points on a graph of multiple functions
3 次查看(过去 30 天)
显示 更早的评论
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1285585/image.png)
Those coloured dots such that when hovered over it displays the values.
0 个评论
采纳的回答
Tushar Behera
2023-2-6
编辑:Tushar Behera
2023-2-6
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
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!