# How do you find the intersection points of two functions?

1,140 views (last 30 days)

Show older comments

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 Comments

Elina Nikolopoulou
on 28 Dec 2021

### Answers (3)

Andrei Bobrov
on 7 Nov 2013

Edited: Andrei Bobrov
on 7 Nov 2013

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

Alexander Efremov
on 7 Nov 2013

Edited: Alexander Efremov
on 7 Nov 2013

##### 1 Comment

Syed Riza
on 31 Jul 2016

Edited: Walter Roberson
on 31 Jul 2016

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 Comments

Adeel Yousuf
on 13 Feb 2019

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!