Find intersecting points of two functions

1 view (last 30 days)
Isaac Fife
Isaac Fife on 17 Mar 2020
Commented: Sajeer Modavan on 19 Mar 2020
I need to find the intersecting points ofr two functions but it only works for simple functions
If I try it with trig funtions it tells me my index exceeds my number of arrays, how do I fix this???
dx = 1/10;
x = 0:dx:10;
fun_1 = (power(x,2));
fun_2 = (x);
find (x==1)
difference = abs(fun_1 - fun_2);
sub_script = find (difference == min(difference) );
%%
difference = abs(fun_1 - fun_2);
min_difference = min(difference);
intersect_subs = find(difference == min(difference) );
sub_1 = intersect_subs(1);
sub_2 = intersect_subs(2); %tells me the error is here..
x_intersect_1 = x(sub_1);
x_intersect_2 = x(sub_2);
point_1 = fun_1(sub_1);
point_2 = fun_2(sub_2);
figure(1)
plot(x, fun_1,...
x, fun_2,...
x_intersect_1, point_1, 'ko',...
x_intersect_2, point_2, 'ko', 'Markersize', 7)
  5 Comments
Isaac Fife
Isaac Fife on 18 Mar 2020
They also intersect at (0,0), but the problem is
when I change it to something like sin(x) and (x-2)^2 it just wont run and says my index exceeds my number of arrays.
dx = 1/100;
x = 0:dx:10;
fun_1 = power((x-2),2);
fun_2 = sin(x);
find (x==1)
difference = abs(fun_1 - fun_2);
%sub_script = find (difference == min(difference) );
%%
min_difference = min(difference);
intersect_subs = find(difference == min(difference) );
sub_1 = intersect_subs(1);
sub_2 = intersect_subs(2);
x_intersect_1 = x(sub_1);
x_intersect_2 = x(sub_2);
point_1 = fun_1(sub_1);
point_2 = fun_2(sub_2);
figure(1)
plot(x, fun_1,...
x, fun_2,...
x_intersect_1, point_1, 'ko',...
x_intersect_2, point_2, 'ko', 'Markersize', 7)
darova
darova on 18 Mar 2020
Can't you just use polyxpoly?

Sign in to comment.

Answers (1)

Sajeer Modavan
Sajeer Modavan on 18 Mar 2020
clc
clear
dx = 1/100;
x = 0:dx:2;
fun_1 = (power(x,2));
fun_2 = (x);
intersect_subs = find(round(fun_1,1) == round(fun_2,5));
figure(1)
plot(x, fun_1,...
x, fun_2,...
x(intersect_subs), fun_1(intersect_subs), 'ko', 'Markersize', 7)
%%
dx = 1/100;
x = 0:dx:2;
fun_1 = (cos(x));
fun_2 = (x);
intersect_subs = find(round(fun_1,1) == round(fun_2,1));
figure(2)
plot(x, fun_1,...
x, fun_2,...
x(intersect_subs), fun_1(intersect_subs), 'ko', 'Markersize', 7)

Categories

Find more on Function Creation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!