How to find second intersection point?
Show older comments
Okay, so
My task is to find the intersections points of functions f1 = 1/x and f2 = sqrt(5./2 - (x^2))
I've found one of the intersections points using:
Intersections=find(abs(f1-f2)<=(0.05));
xvalues=x1(Intersections);
But looking at the graphs I see there are two intersections points, so how do I find the other?
Answers (2)
It is easiest to do this symbolically —
syms x
f1 = 1/x;
f2 = sqrt(5./2 - (x^2));
Intx = solve(f1 == f2)
Intxd = double(Intx)
.
6 Comments
Amanda
on 17 Sep 2022
Star Strider
on 17 Sep 2022
Edited: Star Strider
on 17 Sep 2022
Yes, although this is slightly complicated by the fact that ‘f2’ is complex —
x = linspace(0, 5);
f1 = 1./x;
f2 = sqrt(5/2 - (x.^2));
figure
plot(x, real(f1), x, real(f2))
grid
idx = find(diff(sign(real(f1)-real(f2))));
for k = 1:numel(idx)
idxrng = max(1,idx(k)-1 : min(numel(x),idx(k)+1));
Intx(k) = interp1(real(f1(idxrng))-real(f2(idxrng)),x(idxrng),0);
end
Intx
EDIT — (17 Sep 2022 at 14:18)
f1fcn = @(x) 1./x;
f2fcn = @(x) sqrt(5/2 - (x.^2));
for k = 1:2
Intx(k) = fzero(@(x)real(f1fcn(x))-real(f2fcn(x)), k);
end
Intx
.
Amanda
on 17 Sep 2022
Star Strider
on 17 Sep 2022
My pleasure!
Sure! The fzero function is a root-finding algorithm, and can only return one root at a time, so to find more roots, it is necessary to iterate calls to it with different initial estimates. It will return the closest root to each estimate. Here, one root is closer to 1 and the second root is closer to 2, so I just used the loop counters for the initial estimates here. Usually, more elaborate initial estimates are required. I generally prefer the interp1 approach if it is applicable and if I know the region-of-interest, since it is easier to discover multiple roots or intersections with it, using the ‘idx’ approach to finding them.
Amanda
on 17 Sep 2022
Star Strider
on 17 Sep 2022
The symbolic approach solves for
and
, agreeing with the numeric approach, so I do not see how any other values could be correct.
and
, agreeing with the numeric approach, so I do not see how any other values could be correct. x1 = 0:0.0001:1.5;
f1 = 1./x1;
f2 = sqrt(5./2 - (x1.^2));
Intersections = find(abs(f1-f2)<0.00005);
xvalues=x1(Intersections)
Categories
Find more on Linear Algebra 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!
