How do I graph the negative portion of a square root plot?

10 views (last 30 days)
Morgan Barber on 9 Apr 2024
Edited: John D'Errico on 17 Apr 2024
I would appreciate any help possible in understanding how to get this code to graph the portion of each y function shown that comes from -1 to 0. As can be seen the portion shown for each y function graphs the part from 0 to 1 but not -1 to 0.
3 CommentsShow 1 older commentHide 1 older comment
Voss on 9 Apr 2024
Moved: Voss on 9 Apr 2024
When x<0, y is complex (note the warnings when plotting); thus the real part of y is plotted.
%Question 11
x = -1:0.01:1;
y = sqrt(1 - 3.*x.^(2/3) + 3.*x.^(4/3) - x.^2);
plot(x,y)
Warning: Imaginary parts of complex X and/or Y arguments ignored.
xlim([-1,1])
% ylim([-1,1])
hold on
y = -y;
plot(x,y)
Warning: Imaginary parts of complex X and/or Y arguments ignored.
Dina on 17 Apr 2024
пашол наку

John D'Errico on 9 Apr 2024
Edited: John D'Errico on 17 Apr 2024
Since in reality, you have what is called an implicit function, we want to use fimplicit. I might do it like this:
fun = @(x,y) (1 - 3.*nthroot(x,3).^2 + 3.*nthroot(x,3).^4 - x.^2) - y.^2;
That is, you will get the negative branch of the curve by squaring the square root. fimplicit does the heavy lifting for you here.
fimplicit(fun,[-1,1,-1,1])
grid on
xlabel 'X'
ylabel 'Y'
Note my use of nthroot protects me from having problems when x is negative. This avoids creating complex numbers. For example, the real cube root of -2 is
nthroot(-0.5,3)
ans = -0.7937
Therefore, the real 2/3 power of -2 can be found by squaring that result.
nthroot(-0.5,3).^2
ans = 0.6300
The problem of course is if you simply use direct fractional powers, then MATLAB will return ONE of the solutions, but it will choose by default a complex solution for that fractional power. In fact, there are multiple fractional powers we must worry about as solutions. This is only one of them:
(-0.5).^(2/3)
ans = -0.3150 + 0.5456i
I do something similar to deal with the 4/3 power of x. As well, squaring both sides allows fimplicit to plot all 4 branches, for both positive and negative x, as well as positive and negative y.
A bit tricky I'll admit, but, it works, and works correctly, because it deals properly with those nasty fractional powers.
fimplicit is a tremendously useful tool when used carefully.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!