Because the plot command does not work? Matlab calculate improper integrals?

1) Because the plot command does not work?
2) Matlab calculate improper integrals?
3) How it sets the actual field in matlab?
The code that does not work as I would like is this:
clc
close all
clear all
format compact
syms x
y = (x-2).^(1/3);
I = int(y,x,[0,4])
(3*2^(1/3)*((-1)^(1/3) + 1))/2
figure
ezplot(y,[0,4])
I =
(3*2^(1/3)*((-1)^(1/3) + 1))/2
ans =
2.8348 + 1.6367i
>>

Answers (1)

The problem here is that (-1)^(1/3) has three different possible roots, namely: 1/2+1/2*sqrt(3)*1i, 1/2-1/2*sqrt(3)*1i, and -1. Take the cube of each of these and you will get -1 in all three cases. A similar thing is true for any negative quantity, as for example, (-27)^(1/3). Its three roots are: 3/2+3/2*sqrt(3)*1i, 3/2-3/2*sqrt(3)*1i), and -3. If matlab is confronted with an expression such as (-27)^(1/3), it will choose the first of these, 3/2+3/2*sqrt(3)*1i, and not -3. One way to force matlab to give you a negative real as an answer for x^(1/3) with negative real x, is sign(x)*(abs(x))^(1/3).
The output of your 'int' is correct if you interpret (-1)^(1/3) as being -1. That would make the answer zero, which is correct, assuming (x-2)^(1/3) is interpreted correctly for negative x. Therefore in your plot you should write:
y = sign(x-2).*(abs(x-2)).^(1/3);
and it will plot correctly, and make it clear from its symmetry that its integral should be zero.
Here is one possible explanation for matlab's choice for fractional powers of negative numbers. Suppose x is negative and real. Matlab can (and may actually) express x^(1/3) in terms of the logarithm function as follows:
x^(1/3) = exp(log(x^(1/3))) = exp(1/3*log(x))
Letting x = -27 and we would get
(-27)^(1/3) = exp(1/3*log(-27)) = exp(1/3*(log(27)+pi*1i))
= exp(log(27^(1/3))+1/3*pi*1i) = exp(log(3)+1/3*pi*1i)
= exp(log(3))*exp(1/3*pi*1i)
= 3*(cos(1/3*pi)+sin(1/3*pi)*1i)
= 3*(1/2+1/2*sqrt(3)*1i) = 3/2+3/2*sqrt(3)*1i
which is what matlab actually gets, not -3. A similar reasoning applies to other possible fractional powers of negative numbers.

3 Comments

(A)
If I write: (-27) ^ (1/3) and I get: 1.5000 + 2.5981i I infer two things: 1) Matlab does not give me all 3 complex solutions; 2) Matlab is no longer suitable to work inside the numerical set R; Having said that it seems strange that Matlab does not have a function to choose the set where do the math !!! I can not use sign () and abs () every time I study a function. In the past, when I used Matlab very often, I do not remember these difficulties. I remember that I could also choose the number of elements of the vector 'x' and 'I' using the expression: es = x1: Step: x2;
(B)
I don't understand like to write the function into figure(2). See my second graph, there is a grammatical mistake.
(C)
Why I do not use 'all clear' into my script and Matlab highlights it? What command should I use?
clc
close all
clear all
format compact
syms x
% Questa funzione non può essere disegnata da matlab:
% y = @(x) (x-2).^(-1/3);
% per poterla rappresentare è necessario scrivere:
y = @(x) sign(x-2).*(abs(x-2)).^(-1/3);
x = linspace(-4, 4);
figure(1)
% Grafico della funzione
plot(x, y(x));
x = linspace(0, 4);
I = cumtrapz(x, y(x));
figure(2)
plot(x, real(I), x, imag(I))
grid
xlabel('x')
ylabel('\int (x-2)^{^1/_3}')
legend('Re', 'Im')
% Lunghezza del vettore (rappresenta la cura con cui si valuta l'integrale)
dim=length(I);
% Valore dell'integrale:
Re_I_calc = I(real(dim));
% Valore approssimato dell'integrale:
Re_I = round(Re_I_calc,3)
Thanks for all
@Ubaldo: In reference to your remark in (A): "2) Matlab is no longer suitable to work inside the numerical set R", what would you have matlab do with (-25)^(1/2)? Its two possible roots are both in the complex world. Once you introduce fractional powers, you inevitably encounter complex numbers. There is no way to remain always in the "set R". You will run into the same kinds of problems with Mathematica, since they also choose as a "principal" cube root of a negative number, a complex number with positive imaginary part.
You can use nthroot(-27, 3) to force using the real root (if one exists). There is also realpow(), reallog(), realsqrt()
If you work symbolically then you get all of the roots of polynomials -- though of course there are only exact solutions up to degree 4.

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Asked:

on 7 Mar 2016

Commented:

on 21 Apr 2016

Community Treasure Hunt

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

Start Hunting!