how can i find an itersection point between two function

i need to find an intersction point between two function (all kind of function) but without using a mtllab build in function like fzero and enc so i wrote an exemple of two simple function this is what i wrote:
f1=@(x) -x.^2;
f2=@(x) x.^2-6;
dx=-5:0.000001:5;
y1=f1(dx);
y2=f2(dx);
sum=0;
count=0;
intersectionx=[];
intersectionxx=[];
intersection=y1-y2;
for i=1:length(intersection)-1
if (abs(intersection(i)))<=0.0001
intersectionx(end+1)=dx(i);
end
end
plot(dx,y1)
hold on
plot(dx,y2)
hold on
plot(intersectionx,intersectiony,'*')
I get some problems:
1.I see many of itersection points instead of one when i zoom in
2.I see an intersection point but when i zoom in the point are not exectliy in the dot fo the intersection
3. for some function i miss an intersection points

 채택된 답변

KSSV
KSSV 2018년 6월 19일
편집: KSSV 2018년 6월 19일
YOu may check this code:
f1=@(x) -x.^2;
f2=@(x) x.^2-6;
dx=-5:0.0001:5;
y1=f1(dx);
y2=f2(dx);
sum=0;
intersectionx=[];
intersectionxx=[];
intersection=y1-y2;
tol = 10^-3 ;
idx = abs(intersection)<=tol ;
% get the position
count = 0 ;
pos = zeros([],1) ;
for i = 1:length(idx)
if idx(i)
count = count+1 ;
pos(count) = i ;
end
end
x = dx(pos) ; y = y1(pos) ;
plot(dx,y1)
hold on
plot(dx,y2)
hold on
plot(x,y,'*')

댓글 수: 5

this program uses the function 'find', i cant use matlab build in function to solve it there is another way?
Clearly, if you can't find the function find or fzero or other built-in functions (which if taken literally means you can't even do arithmetics) then that it is because it is homework.
I'm sure the purpose of homework is not to teach you how to ask questions on forums but is to teach you how to solve problems on your own. You only get the benefit of homework by going through the process of trying to solve the problem yourself.
Edited the code.....removed the usage of find
Haim Sfadi If you find the solution useful, give comments and accept, vote the answer. Simply don't close the question.
thanks for your help!

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

추가 답변 (1개)

Sayyed Ahmad
Sayyed Ahmad 2018년 6월 19일
1. you defined the variable intersectionx(end+1) and intersection(i) as array.
So the
plot(intersectionx,intersectiony,'*')
will give you also many points as long as the lenght of your array.
2. That is a numerical itteration what you do. In other words you will never reach your destination, you will only comming closer and closer.
from 1 to 0 as an example:
1; 0.5; 0.25; 0.125; ...
3. check if some answer exit in the range of dx=-5:0.000001:5; you can draw your function to see if you have an intersect. In case of existing intersect you have to check the convergency of your function in the range of searching.

카테고리

도움말 센터File Exchange에서 Surrogate Optimization에 대해 자세히 알아보기

질문:

2018년 6월 19일

댓글:

2018년 6월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by