1

I try to produce a plot of $$\frac{1}{2}-\left| y\right| =\sqrt{\left(\frac{1}{2}-\left| y\right| \right)^2+(y-\sin (x))^2} $$ through

ContourPlot[1/2 - Abs[y] ==Sqrt[(1/2 - Abs[y])^2+(y-Sin[x])^2],
{x,-Pi,  Pi}, {y, -1, 1}, PlotPoints -> 20]

, but I obtain an empty plot.

user64494
  • 26,149
  • 4
  • 27
  • 56

4 Answers4

4
ir = ImplicitRegion[
  FullSimplify[
   Reduce[{1/2 - Abs[y] == 
      Sqrt[(1/2 - Abs[y])^2 + (y - Sin[x])^2], -Pi < x < Pi, -1 < y < 
      1}, {x, y}, Reals]], {x, y}]
RegionPlot[ir, 
 GridLines -> {{-Pi, -5 Pi/6, -Pi/6, Pi/6, 5 Pi/6, Pi}, None}]

enter image description here

ubpdqn
  • 60,617
  • 3
  • 59
  • 148
4

This can be solved algebraically as follows:

$$\frac{1}{2}-\left| y\right| =\sqrt{\left(\frac{1}{2}-\left| y\right| \right)^2+(y-\sin (x))^2} $$

Firstly note that $\frac{1}{2}-|y|\ge0$ so $-\frac{1}{2}\le y\le\frac{1}{2}$.

Next square the equation:

$$\left(\frac{1}{2}-\left| y\right|\right)^2 =\left(\frac{1}{2}-\left| y\right| \right)^2+(y-\sin (x))^2 $$

$$0=(y-\sin(x))^2$$

$$0=y-\sin(x)$$

$$y=\sin(x)$$

So the solution is $y=\sin(x)$ such that $-\frac{1}{2}\le y\le\frac{1}{2}$.

Using Mathematica's Solve or Reduce gives the same result (it just gives restrictions on $x$ rather than $y$).

Solve[{1/2 - Abs[y] == Sqrt[(1/2 - Abs[y])^2 + (y - Sin[x])^2], Element[y, Reals], Element[x, Reals]}, y]

{{y -> ConditionalExpression[Sin[x], (C[1] \[Element] Integers && 2 \[Pi] C[1] <= x <= 1/6 (\[Pi] + 12 \[Pi] C[1])) || (C[1] \[Element] Integers && \[Pi] + 2 \[Pi] C[1] < x <= 1/6 (7 \[Pi] + 12 \[Pi] C[1])) || (C[1] \[Element] Integers && 1/6 (-\[Pi] + 12 \[Pi] C[1]) <= x < 2 \[Pi] C[1]) || (C[1] \[Element] Integers && 1/6 (5 \[Pi] + 12 \[Pi] C[1]) <= x <= \[Pi] + 2 \[Pi] C[1])]}}


Reduce[{1/2 - Abs[y] == Sqrt[(1/2 - Abs[y])^2 + (y - Sin[x])^2], Element[y, Reals], Element[x, Reals]}, y]

C[1] \[Element] Integers && (((2 \[Pi] C[1] <= x <= 1/6 (\[Pi] + 12 \[Pi] C[1]) || 1/6 (5 \[Pi] + 12 \[Pi] C[1]) <= x <= \[Pi] + 2 \[Pi] C[1]) && y == Sin[ x]) || ((1/6 (-\[Pi] + 12 \[Pi] C[1]) <= x < 2 \[Pi] C[1] || \[Pi] + 2 \[Pi] C[1] < x <= 1/6 (7 \[Pi] + 12 \[Pi] C[1])) && y == Sin[x]))

Plotting the solution from the algebraic solution is then straightforward:

Plot[Sin[x] && Abs[Sin[x]] <= 1/2, {x, -Pi, Pi}]

enter image description here

Ian Miller
  • 393
  • 1
  • 11
0

If you wonder why ContourPlot fails, notice that your two functions touch each other on discrete lines but never cross.

Plot3D[{1/2 - Abs[y], 
  Sqrt[(1/2 - Abs[y])^2 + (y - Sin[x])^2]}, {x, -Pi, Pi}, {y, -1, 1}]

enter image description here

ContourPlot looks for the difference in the functions to change signs, which it never does.

Knowing this you can get an approximation by plotting a countour line at some small epsilon near zero:

 ContourPlot[
 1/2 - Abs[y] - Sqrt[(1/2 - Abs[y])^2 + (y - Sin[x])^2], {x, -Pi, 
  Pi}, {y, -1, 1}, PlotPoints -> 200, Contours -> {-0.0001}, 
 ContourShading -> None]

enter image description here

george2079
  • 38,913
  • 1
  • 43
  • 110
-3

In general an answer to your question is as simple as described in the documentation page:

ContourPlot[Cos[x] + Cos[y], {x, 0, 4 Pi}, {y, 0, 4 Pi}]

As for question, are you sure your equation even has a solution? If it has, then you should be able to find the roots using NSolve[] or Solve[].

Now, none of the above works for your equation, so I assume, you have to first figure out if it's solvable and only then try to plot the values.

SuTron
  • 1,708
  • 1
  • 11
  • 21
  • The plot under consideration is not empty. For example, $(0,0)$ and $(-\frac {\pi} 6, -\frac {1} 2)$ belong to the one. @SuTron – user64494 May 25 '17 at 07:30