How do you find the intersection points of two functions?

조회 수: 37 (최근 30일)
Dan Teep
Dan Teep 2013년 11월 7일
댓글: Elina Nikolopoulou 2021년 12월 28일
My problem tells me to plot and then find and print the points of intersection for x=[2:7]. I already sought help and they explained that I should use 'find' and then the '==' to find where the output match. Sounded simple enough but I'm getting "Empty Matrix: 1-by-0" as the answer. Any one know what I'm doing wrong or a better way?
Also, any idea how I'd then go about using a "for - while" loop to find every intersection? I'm taking it one problem at a time though.
Here's my script so far:
% clear all windows and variables
clc
% Declare an array for the values of x
x=[2:7];
% Input the given functions
f1=@(x) 90.*exp(-x);
f2=@(x) 3*sin(2*pi*x);
% Graph the functions in the same window
fplot(f1,[2,7],'b')
hold on
fplot(f2,[2,7],'r')
grid on
title('Finding Intersections of Functions')
xlabel('Input Values (x)')
ylabel('Ouput Values (f)')
% Find the first intersection
f1a=90.*exp(-x);
f2a=3*sin(2*pi*x);
find(f1a==f2a)
  댓글 수: 2
ankit kumar
ankit kumar 2018년 4월 5일
is there anyway i can give a txt file including x and y axis value and can find how many times a curve is crossing through a threshold and also to note down the x values.
Elina Nikolopoulou
Elina Nikolopoulou 2021년 12월 28일
How do i keep the Y and the X coordinates of the intersections in separate arrays (one for the X and one for the Y coordinates) ???

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

답변 (3개)

Andrei Bobrov
Andrei Bobrov 2013년 11월 7일
편집: Andrei Bobrov 2013년 11월 7일
EDIT
f1=@(x) 90.*exp(-x);
f2=@(x) 3*sin(2*pi*x);
f = @(x)f1(x)-f2(x);
xx = 2:.1:7;
t = f(xx) > 0;
i0 = find(diff(t(:))~=0);
i0 = [i0(:)';i0(:)'+1];
n = size(i0,2);
xout = zeros(n,1);
for jj = 1:n
xout(jj) = fzero(f,xx(i0(:,jj)));
end
  댓글 수: 2
Mathijs Frenken
Mathijs Frenken 2018년 11월 27일
Thank you for your answer. However, you realize you are not the only one that reads your code right? It's actually unreadable.

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


Alexander Efremov
Alexander Efremov 2013년 11월 7일
편집: Alexander Efremov 2013년 11월 7일
There is a fancy contribution on "File Exchange" which appeared in the Pick of the week. This should help.

Syed Riza
Syed Riza 2016년 7월 31일
편집: Walter Roberson 2016년 7월 31일
Hello Dan Teep; you can find your answer in this modified code
% Declare an array for the values of x
x=linspace(2,7,1200);
% Input the given functions
f1=@(x) 90.*exp(-x);
f2=@(x) 3*sin(2*pi*x);
% Graph the functions in the same window
fplot(f1,[2,7],'b')
hold on
fplot(f2,[2,7],'r')
grid on
title('Finding Intersections of Functions')
xlabel('Input Values (x)')
ylabel('Ouput Values (f)')
% Find the x-cordinates of intersecting points
f1a=90.*exp(-x);
f2a=3*sin(2*pi*x);
Intersections=find(abs(f1a-f2a)<=(0.05));
X_Values=x(Intersections)
  댓글 수: 2
Maya Priveetra
Maya Priveetra 2017년 3월 9일
Hi can I know why you (abs(fla-f2a)<=(0.05))
Like what does it do especially the 0.05
Adeel Yousuf
Adeel Yousuf 2019년 2월 13일
He has used a tolerance value of 0.05 as the dataset contains floating point values. We can keep tolerance as small as it seems suitable (like: 1e-4). It's meant to "detect" the closeness of 2 floating point values. Moreover in simple words, we need to make MATLAB determine if 60.2745 is the same as 60.274 or not? So for that we can use threshold/tolerance of 0.001.

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

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by