1
$\begingroup$

I need to write a function that returns graphic visualization of the two-dimensional figure described with equation:

$\qquad P = \{(x, y) \in R^2: 4+2x < x^2+y^2<9\}$

I have to use the n x n matrix for this (n is the parameter of the function to write)

If I understand correctly, given the same problem but with circle as $P$ instead for $n = 3$, it would be something like this:

ArrayPlot[{{0, 1, 0}, {1, 1, 1}, {0, 1, 0}}]

I know that, in the problem stated above, I have an intersection of circle with the complementation of another circle, but I am at a lost on how to express the above equation in the form of a matrix and how to scale it with the manipulation of the parameter $n \in N$.

$\endgroup$
2
$\begingroup$

Make a function that captures the condition on {x,y}:

ClearAll[f]
f[x_, y_] := 4  + 2 x <= x^2 + y^2 <= 9

Here is the region defined by f:

rp = RegionPlot[f[x, y], {x, -5, 5}, {y, -5, 5}, PlotPoints -> 100]

enter image description here

Make a matrix using f:

matrix[f_][n_Integer, divs_: Automatic] :=  Module[{m = If[EvenQ[n], n, n + 1]}, 
  Table[Boole[f[i, j]], 
     {j, -m/2, m/2 , m /(divs /. Automatic -> m)},
     {i, -m/2, m/2,  m /(divs /. Automatic -> m)}]]

ap = ArrayPlot[matrix[f][9], Mesh -> All, DataReversed -> True, 
  DataRange -> {{-5, 5}, {-5, 5}}]

enter image description here

Show the region and array plots together:

Show[ap, rp]

enter image description here

Use the second argument of matrix to get finer subdivisions:

divs = 20;
Show[ArrayPlot[matrix[f][9, divs], Mesh -> None, DataReversed -> True, 
  DataRange -> {{-5, 5}, {-5, 5}}], rp]

enter image description here

divs = 100;
Show[ArrayPlot[matrix[f][9, divs], Mesh -> None, DataReversed -> True, 
  DataRange -> {{-5, 5}, {-5, 5}}], rp]

enter image description here

A more complicated region:

ClearAll[f2]
f2[x_, y_] := Xor[(-1/2 + x)^2 + y^2 < 1, 
   x + 4*x^2 + 4*y^2 < 3 + Sqrt[5]*x + Sqrt[2*(5 + Sqrt[5])]*y, 
   x*(1 + Sqrt[5] + 4*x) + 4*y^2 < 3 + Sqrt[10 - 2*Sqrt[5]]*y, 
   x*(1 + Sqrt[5] + 4*x) + y*(Sqrt[10 - 2*Sqrt[5]] + 4*y) < 3, 
   x + 4*x^2 + y*(Sqrt[2*(5 + Sqrt[5])] + 4*y) < 3 + Sqrt[5]*x];

rp = RegionPlot[f2[x, y], {x, -2, 2}, {y, -2, 2}, PlotPoints -> 100]

enter image description here

ap2 = ArrayPlot[matrix[f2][10, 100], Mesh -> None, 
   DataReversed -> True, DataRange -> {{-5, 5}, {-5, 5}}, 
   ColorRules -> {1 -> Red, 0 -> White}];
Show[ap2, rp2, PlotRange -> {{-2, 2}, {-2, 2}}]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.