How can i use intersect function correctly ? (2024)

36 views (last 30 days)

Show older comments

VBBV on 6 Dec 2022

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/1872297-how-can-i-use-intersect-function-correctly

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/1872297-how-can-i-use-intersect-function-correctly

Commented: Jan on 10 Dec 2022

Accepted Answer: Star Strider

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 Comment

Show -1 older commentsHide -1 older comments

Stephen23 on 6 Dec 2022

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/1872297-how-can-i-use-intersect-function-correctly#comment_2503617

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/1872297-how-can-i-use-intersect-function-correctly#comment_2503617

Edited: Stephen23 on 6 Dec 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?:

  • 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)

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Star Strider on 6 Dec 2022

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/1872297-how-can-i-use-intersect-function-correctly#answer_1121452

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/1872297-how-can-i-use-intersect-function-correctly#answer_1121452

Use calls to interp1 in a loop to get each intersection —

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

A = 1×0 empty double row vector

ixx = find(diff(sign(fg(x)))) % Approximate Indices Of Intersection

ixx = 1×5

329 486 958 1115 1586

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]

Intersections = 2×5

-4.7124 -3.1416 1.5708 3.1416 7.8540 1.0000 0.0000 1.0000 0.0000 1.0000

figure

plot(x,f(x),x,g(x), 'DisplayName','Function');

hold on

plot(xv, yv, 'sr', 'DisplayName','Intersections')

hold off

legend('Location','best')

How can i use intersect function correctly ? (4)

Using the loop and the ‘idxrng’ vector avoids problems with non-monotonic regions of the functions.

.

3 Comments

Show 1 older commentHide 1 older comment

VBBV on 6 Dec 2022

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/1872297-how-can-i-use-intersect-function-correctly#comment_2504042

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/1872297-how-can-i-use-intersect-function-correctly#comment_2504042

Thank you for the Nice explanation. are there any builtin function/s available in Matlab which can detect the region of crossovers for curves that overlap?

Star Strider on 6 Dec 2022

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/1872297-how-can-i-use-intersect-function-correctly#comment_2504102

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/1872297-how-can-i-use-intersect-function-correctly#comment_2504102

Open in MATLAB Online

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)

xv = 1×5

-4.7124 -3.1416 1.5708 3.1416 7.8540

yv = 1×5

1.0000 0.0000 1.0000 0.0000 1.0000

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 on 10 Dec 2022

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/1872297-how-can-i-use-intersect-function-correctly#comment_2510842

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/1872297-how-can-i-use-intersect-function-correctly#comment_2510842

Open in MATLAB Online

@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)

xv = 1×5

-4.7124 -3.1416 1.5708 3.1416 7.8540

yv = 1×5

1.0000 0.0000 1.0000 0.0000 1.0000

format long g

xv(3) - pi/2

ans =

3.69037267589079e-06

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().

Sign in to comment.

More Answers (1)

Jan on 6 Dec 2022

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/1872297-how-can-i-use-intersect-function-correctly#answer_1121432

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/1872297-how-can-i-use-intersect-function-correctly#answer_1121432

Edited: Jan on 6 Dec 2022

Open in MATLAB Online

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.');

How can i use intersect function correctly ? (9)

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])

xi =

1.5707963267949

A symbolic solution of the intersections:

syms x

eq = 1 + cos(x) - sin(x) == 0

eq=How can i use intersect function correctly ? (10)

solve(eq)

ans=

How can i use intersect function correctly ? (11)

1 Comment

Show -1 older commentsHide -1 older comments

VBBV on 6 Dec 2022

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/1872297-how-can-i-use-intersect-function-correctly#comment_2504047

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/1872297-how-can-i-use-intersect-function-correctly#comment_2504047

Thank you for the Nice explanation. are there any builtin function/s available in Matlab which can detect the region of crossovers for curves that overlap?

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABMathematics

Find more on Mathematics in Help Center and File Exchange

Tags

  • intersect
  • matlab
  • empty
  • array

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


How can i use intersect function correctly ? (13)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

Contact your local office

How can i use intersect function correctly ? (2024)

References

Top Articles
Latest Posts
Article information

Author: Duane Harber

Last Updated:

Views: 5575

Rating: 4 / 5 (71 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Duane Harber

Birthday: 1999-10-17

Address: Apt. 404 9899 Magnolia Roads, Port Royceville, ID 78186

Phone: +186911129794335

Job: Human Hospitality Planner

Hobby: Listening to music, Orienteering, Knapping, Dance, Mountain biking, Fishing, Pottery

Introduction: My name is Duane Harber, I am a modern, clever, handsome, fair, agreeable, inexpensive, beautiful person who loves writing and wants to share my knowledge and understanding with you.