MATLAB Answers

why my plot is not correct?

14 views (last 30 days)
wenchong chen
wenchong chen on 11 Apr 2021 at 4:46
Commented: DGM on 11 Apr 2021 at 22:29
here is my code
x = -5:0.1:5
y = x.^(1/3)
wenchong chen
wenchong chen on 11 Apr 2021 at 4:47
also there is a Warning: Imaginary parts of complex X and/or Y arguments ignored.

Sign in to comment.

Answers (2)

Paul on 11 Apr 2021 at 17:54
If you're looking for a plot with odd symmetry, try:
plot(x,nthroot(x,3)) % check doc nthroot for details

DGM on 11 Apr 2021 at 5:01
I'm not sure what you intend the correct number to be. You're doing the cube root of negative numbers. They're going to be complex.
By default, plot() will only plot the real component of complex inputs. If you're expecting the plot to be symmetrical, consider plotting abs(y) to get the magnitude of the complex-valued region.
x = -5:0.1:5;
y = x.^(1/3);
h1=plot(x,real(y)); hold on
DGM on 11 Apr 2021 at 22:29
You probably want Paul's answer below. nthroot() calculates the real root, whereas power() or .^ calculates the complex root. The web docs include such an example.

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!