Find intersecting points of two functions

1 vue (au cours des 30 derniers jours)
Isaac Fife
Isaac Fife le 17 Mar 2020
Commenté : Sajeer Modavan le 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 commentaires
Isaac Fife
Isaac Fife le 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 le 18 Mar 2020
Can't you just use polyxpoly?

Connectez-vous pour commenter.

Réponses (1)

Sajeer Modavan
Sajeer Modavan le 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)
  1 commentaire
Sajeer Modavan
Sajeer Modavan le 19 Mar 2020
is it solved your problem?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Function Creation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by