Can't solve an indefinite integral in MATLAB

228 views Asked by At

In order to solve an indefinite integral, I came with a solution which was not in accordance with the the book's solution. In order to check my solution, I tried to solve the problem using this code:

syms x
question=int(1/(x*(1+x^5)^0.5))`   
mysolution=(1/5)*(log(((sqrt(1+x^5))-1)/(sqrt(1+x^5)+1)))
x=3  
eval(question)  
eval(mysolution)

Which resulted in:

ans =-0.0256 - 0.6283i
ans =-0.0256

I have to find the solution of an indefinite integral but when I gave MATLAB this problem (solving the indefinite integral) it gave another solution. In order to check whether my solution was right, I gave x the value 3 using syms and eval, so MATLAB was supposed to show my answer and his own answer with the assumption of x=3.

The real part of MATLAB's answer was the same as my answer but it it contained an imaginary part which was not in accordance with my solution. In fact, my answer with x=3 did not contain any imaginary part. what is wrong and Why is that so?

1

There are 1 answers

0
flawr On

This is more a math question than a programming question.

Your indefinite integral can be written as:

-2/5 * atanh(sqrt(1+x^5))

which is equal to your solution, since

atanh(z) = 1/2 ln( (1+z) / (1-z) ) 

Now if we look at what Matlab is calculating, it says

(2*atan((x^5 + 1)^(1/2)*i)*i)/5

This makes sense because

atan(x*i) = i*atanh(x)

Now let's consider the imaginary part of matlab's solution: Let us recall that ln is not uniquely defined on the complex numbers, but has different branches. This is because the complex exponential function is 2*pi*i periodic:

exp(x) = exp(x + 2*pi*i)

This is the imaginary part of a "multivalued" representation of a complex logarithm that visualizes this circumstance, you could say

" log(z) = log(z) + 2*pi*i "

So if we go back to your concrete example: The imaginary part was - 0.6283 and you had

-2/5 * atanh(sqrt(1+x^5))=-2/5 * 1/2 * ln((1+sqrt(1+x^5))/(1-sqrt(1+x^5)))
                            let z = sqrt(1+x^5)
                         = -2/5 * 1/2 * ln((1+z)/(1-z))
                            let w = (1+z)/(1-z)
                         = -1/5 * ln(w)
                         = -1/5 * [ ln(-w) + ln(-1) ]
                         = -1/5 * [ ln(-w) - pi*i]     since exp(-pi*i)=-1
                now apply the "equation" in quotation marks
                        "=" -1/5 * [ ln(-w) + 2*pi - pi*i]
                         = -1/5 * ln(-w) - 1/5*pi*i

And guess what: -1/5*pi*i = - 0.6283i

So this comes fromt he fact that Matlab is basically trying to find the logarithm of a negative value, hence the imaginary part -pi*i.

So the lesson here: Be careful with complex numbers :D (or be careful with matlab's symbolic math routines...)