Find intersecting points of two functions

조회 수: 2 (최근 30일)
Isaac Fife
Isaac Fife 2020년 3월 17일
댓글: Sajeer Modavan 2020년 3월 19일
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
Isaac Fife
Isaac Fife 2020년 3월 18일
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 2020년 3월 18일
Can't you just use polyxpoly?

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

답변 (1개)

Sajeer Modavan
Sajeer Modavan 2020년 3월 18일
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)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by