Message Boards Message Boards

0
|
16595 Views
|
12 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Why does Sqrt[] only return positive values rather than +/-?

Posted 8 years ago

So, I'm trying to graph a parabola reflected across y=x.

y=x^2 --> y^2=x or y=Sqrt[x], yes?

Am I thinking about this correctly? It's been a long time...

So, I write:

Plot[{y = Sqrt[x]}, {x, 0, 50}, 
PlotRange -> {{0, 50}, {-7, 7}}, 
Ticks -> {{0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50}, {-6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6}}]

But, Mathematica only returns the portion of the graph above y=0. I'm not clear on why? Shouldn't it return +/- values and show both halves of the reflected parabola rather than only the top half? Does Sqrt[] only return positive values? Despite the fact that, say, (-2)(-2)=4 just as surely as 22=4?

Am I doing something wrong? Is it a problem with Mathematica? Am I thinking about it wrong? Can I force it to graph the entire thing (both positives and negatives, without having to graph them individually)? Is that even a legit thing to do?

I assume from images like these that it is, or should be?

Reflected Parabola

Attachments:
POSTED BY: Michael Gmirkin
12 Replies

Here is the sqrt function with only one call to Sqrt:

sqrt[x_] := Module[{sq = Sqrt[x]}, {sq, -sq}]

It's about the same speed as two calls to Sqrt.

But my real point was that if you want different behaviour from a function, go ahead and modify it to suit your needs.

POSTED BY: Eric Johnstone

Wish there was a way the Sqrt[] function could use some switch or property to force it to return [both / all] values, rather than just the positive solution. I'd be more fine with using the positive by default if the other values could be generated by changing some parameter. Like:

Sqrt[x], (assumed default)

Sqrt[x, Default], (explicit default)

Sqrt[x, Positive], (explicit positive)

Sqrt[x, Negative], (explicit negative)

Sqrt[x, All] (explicitly returns all solutions)

Ohh well... Maybe in some future version of Mathematica...?

POSTED BY: Michael Gmirkin

This would become hopelessly complicated:

2==Sqrt[4]
True

now returns True. But if it would return {-2,2}, it would stay unevaluated:

2=={-2,2}

Of course you could change the definition of Equal, to check if ONE of them is correct... but then you get into trouble if you want to check 2 things at the same time:

{2,3} == {1,2}+1

So in the end this is not a good idea ;) A similar problem occurs for trigonometric functions. For example the ArcCos (or ArcSin ArcTan..) returns and angle, but it can just as well return the angle + 2Pi. So you have to make a choice, because there are infinite! In the complex plane this becomes even more complex, and one has to choose a so-called branch cut and decide very carefully which answer should be returned.

There is an entire page about it HERE

POSTED BY: Sander Huisman

To add, simply use Contourplot in your endeavours, and use the 'squared' form: y==x^2 rather than 'square-root' form Sqrt[y]==x...

POSTED BY: Sander Huisman

You can make your own sqrt function:

sqrt[x_]:={Sqrt[x],-Sqrt[x]}
Plot[sqrt[x],{x,0,4}]

enter image description here

POSTED BY: Eric Johnstone

Hmm, interesting... :)

Though, of course, it's still using two Sqrt functions. But, still another interesting approach to it. ;)

Thx, Eric!

POSTED BY: Michael Gmirkin

So, long and short of it, plot both halves, 'cause there's no other solution? Got it.

Would still prefer there was some way to plot the reflection of the parabola with a single function, rather than 2 half-functions. Ahh well, guess you can't always get what you want, eh? ;)

POSTED BY: Michael Gmirkin

The square root function is just that: a function. By convention, it returns the positive square root.

When I was young, you could find books referring to multi-valued functions, so you would get ± in the results. This was also a convention, but had to be resolved once computers came to be used, since they only 'like' single valued functions, which are the only ones that are, by definition, functions anymore.

You might still be able to find these books, but they need to be treated as historical artifacts.

So, you need to use a plot of two functions (+sqrt[x] and -sqrt[x]), or either of the solutions provided by other responders.

Once you get your head around that, try plotting the cube root of x. Then you can learn about principal values. ;-)

Or even easier, try ContourPlot!

POSTED BY: Sander Huisman

You could use ParametricPlot.

Clear[x, y, t]
x[t_] := t^2;
y[t_] := t;
ParametricPlot[{x[t], y[t]}, {t, -7, 7}]
POSTED BY: Gerard Kopcsay

Okay, so I thought that maybe wrapping the Sqrt[x] in PlusMinus[] would help.

But, apparently, y=PlusMinus[Sqrt[x]] just makes the top half of the graph ALSO disappear. Grr... This is getting frustrating, now. :o(

So, is it possible to graph the entire function in a single go, or am I going to have to individually graph the upper half and the lower half as separate graphs:

Plot[{y = Sqrt[x], y = -Sqrt[x]}, {x, 0, 50}, PlotRange -> {{0, 50}, {-7, 7}}, 
 Ticks -> {{0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50}, {-6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6}}]

I don't feel like I should have to go to these lengths to graph something so simple...

What am I doing wrong? Or what is Mathematica doing wrong, and how do we fix it??

POSTED BY: Michael Gmirkin
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract