1
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ @Bill: Are you serious? Thank you anyway. $\endgroup$ – user64494 May 25 '17 at 6:00
  • 1
    $\begingroup$ Isn't this solvable algebraically? You just get $y=\sin x$ such that $-\frac{1}{2}\le y\le\frac{1}{2}$ $\endgroup$ – Ian Miller May 25 '17 at 7:49
  • $\begingroup$ ParametricPlot[If[1/2 - Abs[Sin[x]] > 0, {x, Sin[x]}, {}], {x, -Pi, Pi}] $\endgroup$ – Coolwater May 25 '17 at 7:57
  • $\begingroup$ @Ian Miller: Please, peresent an algebraic solution to base your words. $\endgroup$ – user64494 May 25 '17 at 7:59
  • 2
    $\begingroup$ The underlying problem is that ContourPlot has a hard time plotting the contour of $f(x,y)=g(x,y)$ if the functions never cross each other, i.e. you always have $f(x,y)\le g(x,y)$ and never $f(x,y)>g(x,y)$. $\endgroup$ – user484 May 26 '17 at 4:22
4
$\begingroup$
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

$\endgroup$
4
$\begingroup$

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

$\endgroup$
  • $\begingroup$ I think you meant Plot[ConditionalExpression[Sin[x], Abs[Sin[x]] <= 1/2], {x, 0, 2 π}]. $\endgroup$ – J. M.'s technical difficulties May 26 '17 at 3:38
  • $\begingroup$ The notation I used worked for me in version 10.2. It produced a similar graph to ubpdqn's answer. I'll include it. $\endgroup$ – Ian Miller May 26 '17 at 3:44
  • $\begingroup$ @Ian Miller: I find your answer made by hand with help of Mathematica. $\endgroup$ – user64494 May 26 '17 at 5:59
0
$\begingroup$

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

$\endgroup$
-3
$\begingroup$

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.

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

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.