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

조회 수: 8 (최근 30일)
Morgan Barber 2024년 4월 9일
편집: John D'Errico 2024년 4월 17일
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이전 댓글 1개 표시이전 댓글 1개 숨기기
Voss 2024년 4월 9일
이동: Voss 2024년 4월 9일
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 2024년 4월 17일
пашол наку

댓글을 달려면 로그인하십시오.

### 답변 (1개)

John D'Errico 2024년 4월 9일
편집: John D'Errico 2024년 4월 17일
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.

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by