- 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 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
VBBV
el 6 de Dic. de 2022
Comentada: Jan
el 10 de Dic. de 2022
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 comentario
Stephen23
el 6 de Dic. de 2022
Editada: Stephen23
el 6 de Dic. de 2022
"Can anyone clarify this ?"
The two arrays do not share any data points.
"How can i use intersect function correctly ?"
To do what exactly?:
Respuesta aceptada
Star Strider
el 6 de Dic. de 2022
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 comentarios
Star Strider
el 6 de Dic. de 2022
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
el 10 de Dic. de 2022
@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().
Más respuestas (1)
Jan
el 6 de Dic. de 2022
Editada: Jan
el 6 de Dic. de 2022
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)
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

