6

I have two sets $A = \{1 \leq x \leq 5\}$ and $B = \{5 \leq x \leq 8\}$. Now I want to find the Union and Intersection of $A$ and $B$.

I tried Union[A, B], I got {1 <= x <= 5, 5 <= x <= 8} and for Intersection[A, B], I got {}. The correct answer for $A \cup B$ is [1, 8] and $A \cap B$ is {5}. How do I tell Mathematica to do that?

And if $A = \{1 < x < 5\}$ and $B = \{x > 5\}$. Now I want to find the Union and Intersection of $A$ and $B$. How do I tell Mathematica to do that?

minthao_2011
  • 4,503
  • 3
  • 31
  • 46

2 Answers2

10
a = Interval[{1, 5}];
b = Interval[{5, 8}];
IntervalUnion[a, b]

Interval[{1, 8}]

IntervalIntersection[a, b]

Interval[{5, 5}]

DavidC
  • 16,724
  • 1
  • 42
  • 94
2

Open/closed intervals compatible with regions:

OpenInterval[a_, b_] := ImplicitRegion[a < x && x < b, {x}];
ClosedInterval[a_, b_] := ImplicitRegion[a <= x && x <= b, {x}];

Semi-open intervals and infinity intervals are similar.

Need some typing to transform region to an expression:

  • open intervals:
    RegionMember[RegionUnion[OpenInterval[0, 5], OpenInterval[5, 8]], {x}] // FullSimplify
    

    gives:

    x \[Element] Reals && (0 < x < 5 || 5 < x < 8)
    
  • closed intervals:
    RegionMember[RegionUnion[ClosedInterval[0, 5], ClosedInterval[5, 8]], {x}] // FullSimplify
    

    gives:

    0 <= x <= 8
    
mikea
  • 379
  • 1
  • 5