- return common data points from two sets of data (just as the function is documented)
- find intersections of two functions (perhaps what you really want to achieve)
How can i use intersect function correctly ?
21 ビュー (過去 30 日間)
古いコメントを表示
I tried the following code to use intersect function available in Matlab f=@(x)1+cos(x); g=@(x)sin(x); x=-8:.01:8; A = intersect(f(x),g(x)) % this returns empty plot(x,f(x),x,g(x)); but the intersect function returns empty, even though it appears that there are common values for both functions defined over same x-interval. Can anyone clarify this ?
1 件のコメント
Stephen23
2022 年 12 月 6 日
編集済み: Stephen23
2022 年 12 月 6 日
"Can anyone clarify this ?"
The two arrays do not share any data points.
"How can i use intersect function correctly ?"
To do what exactly?:
採用された回答
Star Strider
2022 年 12 月 6 日
f=@(x)1+cos(x);
g=@(x)sin(x);
fg = @(x) f(x)-g(x);
x=-8:.01:8;
A = intersect(f(x),g(x)) % this returns empty
ixx = find(diff(sign(fg(x)))) % Approximate Indices Of Intersection
for k = 1:numel(ixx)
idxrng = max(1,ixx(k)-1) : min (numel(x),ixx(k)+1); % Index Range
xv(k) = interp1(fg(x(idxrng)),x(idxrng),0); % X-Coordinate
yv(k) = f(xv(k)); % Y-Coordinate
end
Intersections = [xv; yv]
figure
plot(x,f(x),x,g(x), 'DisplayName','Function');
hold on
plot(xv, yv, 'sr', 'DisplayName','Intersections')
hold off
legend('Location','best')
Using the loop and the ‘idxrng’ vector avoids problems with non-monotonic regions of the functions.
.
3 件のコメント
Star Strider
2022 年 12 月 6 日
As always, my pleasure!
Not that I’m aware of.
The best it is possible to do would be to put something like this into its own function. This version works with vectors, so the vectors would have to be passed to it.
A different version could also work with funcitons, however that would have to be a separate function.
It might be possible to use one function for both vectors and functions, however it would require internal logic to evaluate them and use them correctly.
Using this version as a function —
f=@(x)1+cos(x);
g=@(x)sin(x);
x=-8:.01:8;
fx = f(x);
gx = g(x);
[xv,yv] = functionIntersections(fx,gx,x)
function [xv,yv] = functionIntersections(f,g,x)
ixx = find(diff(sign(f-g)));
if isempty(ixx)
error('No intersections exist between these two functions.')
return
end
for k = 1:numel(ixx)
idxrng = max(1,ixx(k)-1) : min (numel(x),ixx(k)+1); % Index Range
xv(k) = interp1(f(idxrng)-g(idxrng),x(idxrng),0); % X-Coordinate
yv(k) = interp1(x(idxrng),f(idxrng),xv(k)); % Y-Coordinate % Y-Coordinate
end
end
There are File Exchange contributions that can handle much more extensive sets of line intersections. This one works for relatively straightforward problems of two functions. I have incorporated logic in it for failed intersections (the two function arguments having no intersections).
.
Jan
2022 年 12 月 10 日
@Star Strider: The accuracy of the interpolation method is limited:
f = @(x)1+cos(x);
g = @(x)sin(x);
x = -8:.01:8;
[xv,yv] = functionIntersections(f(x), g(x), x)
format long g
xv(3) - pi/2
This is rough, but maybe sufficient for a graphical display. But if the points of the polygon is all you have and the functions f() and g() are not available, your linear interpolation method is trustworthy. Including further assumptions like cubic or spline interpolations can increase and decrease the accuracy, so this is a fragile option.
If f() and g() are available, this interpolation method ist a good method to find start points for e.g. fzero().
その他の回答 (1 件)
Jan
2022 年 12 月 6 日
編集済み: Jan
2022 年 12 月 6 日
There are no overlapping points:
f = @(x) 1 + cos(x);
g = @(x) sin(x);
x = 1.54:.01:1.6;
plot(x, f(x), 'r.', x, g(x), 'b.');
The same matters the other points, where the graphs intersect, but not at any of the fixed raster points x=-8:.01:8. A real intersection:
format long g
xi = fzero(@(x) f(x) - g(x), [1.54, 1.6])
A symbolic solution of the intersections:
syms x
eq = 1 + cos(x) - sin(x) == 0
solve(eq)
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

