필터 지우기
필터 지우기

How to identify points on a graph of multiple functions

조회 수: 5 (최근 30일)
Samuel Eronmwon
Samuel Eronmwon 2023년 2월 6일
댓글: Samuel Eronmwon 2023년 2월 9일
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.

채택된 답변

Tushar Behera
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
  댓글 수: 1
Samuel Eronmwon
Samuel Eronmwon 2023년 2월 9일
Thank you. This answer's my question very well. I'm grateful.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by