简体   繁体   中英

integration of complicated function with heaviside in denominator in Matlab

I want to evaluate the following expression:

表达

I have used the Matlab int command, but for unknown reason, complex number and pi are parts of Matlab's answer.

syms x;
q = int(1/((9*(1 - x^2)^(1/2)*(heaviside(x - 1) - heaviside(x + 1)))/5 + 2*(4 - x^2)^(1/2)),-2,2)

It is giving me this pretty complex answer:

(68*pi)/19 - (450*pi*957^(1/2))/6061 - (957^(1/2)*log(5700)*225*i)/6061 + (957^(1/2)log(3^(1/2)((19*319^(1/2))/2700 - (3*19^(1/2))/100 + (6061^(1/2)*i)/300 + (19*i)/300))*225*i)/6061 - (957^(1/2)log(-3^(1/2)((3*19^(1/2))/100 + (19*319^(1/2))/2700 + (6061^(1/2)*i)/300 - (19*i)/300))*225*i)/6061 + (957^(1/2)*log(10*57^(1/2))*450*i)/6061 - (19^(1/2)*243^(1/2)*6061^(1/2)*log((3^(1/2)*i + (19^(1/2)243^(1/2)(6061^(1/2)/19 + 4))/243)/(6061^(1/2)/19 + 1))*25*i)/115159 + (19^(1/2)*243^(1/2)*6061^(1/2)*log(-(3^(1/2)*i - (19^(1/2)243^(1/2)(6061^(1/2)/19 - 4))/243)/(6061^(1/2)/19 - 1))*25*i)/115159

where as the answer should be between 1 and 10 real only (not complex) if my research model is right.

Can you give me any suggestions, any command, or point anything I am doing wrong in matlab that is giving this complex answer.

You are worrying about the wrong thing. The result you are getting is in the range [1 10]. The complex parts will cancel each others out.

If I evaluate your code:

syms x;
q = int(1/((9*(1 - x^2)^(1/2)*(heaviside(x - 1) - heaviside(x + 1)))/5 + 2*(4 - x^2)^(1/2)),-2,2)

I obtain a different expression for q than you:

q =
(68*pi)/19 - (45*pi*300^(1/2)*319^(1/2))/6061 - (45*300^(1/2)*319^(1/2)*i*log(243*19^(1/2)*319^(1/2) - 4617))/6061 + (45*300^(1/2)*319^(1/2)*i*log(243*19^(1/2)*319^(1/2) + 4617))/6061 - (45*300^(1/2)*319^(1/2)*i*log(300*19^(1/2)*319^(1/2) - 5700))/6061 + (45*300^(1/2)*319^(1/2)*i*log(300*19^(1/2)*319^(1/2) + 5700))/6061 + (45*300^(1/2)*319^(1/2)*i*log(19*300^(1/2)*319^(1/2) - 19*19^(1/2)*300^(1/2)))/6061 - (45*300^(1/2)*319^(1/2)*i*log(19*19^(1/2)*300^(1/2) + 19*300^(1/2)*319^(1/2)))/6061 - (45*300^(1/2)*319^(1/2)*i*log(- 76*19^(1/2)*243^(1/2) - 19*243^(1/2)*319^(1/2) - 4617*3^(1/2)*i))/12122 + (45*300^(1/2)*319^(1/2)*i*log(19*243^(1/2)*319^(1/2) - 76*19^(1/2)*243^(1/2) - 4617*3^(1/2)*i))/12122 + (45*300^(1/2)*319^(1/2)*i*log(76*19^(1/2)*243^(1/2) - 19*243^(1/2)*319^(1/2) + 4617*3^(1/2)*i))/12122 - (45*300^(1/2)*319^(1/2)*i*log(76*19^(1/2)*243^(1/2) + 19*243^(1/2)*319^(1/2) + 4617*3^(1/2)*i))/12122

However, if I evaluate it in an old fashion (this works on Matlab R2009a):

>> q.eval
ans =
  1.883829527329203 - 0.000000000000004i

You should notice that the imaginary part is next to nothing. This is just a residue of calculation error. You cannot expect more than 15 digits precision when using 64 bits floating point number representation (matlab double format). Expect even less if it is the result of long calculations (the error may grow with the number of computations).

In this case, you can safely discard a value of 0.000000000000004 and assimilate it to 0 . Which means that your integral evaluated a real number.

Now I don't know what your research conclude, but if you are positively certain that the result has to be real , then you can take only the real part of the expression:

>> q.real.eval
ans =
   1.883829527329203

Thanks to Horchler's comment, a better way to evaluate the value of the integral is to directly cast it into a double precision number :

>> double(q)
ans =
  1.883829527329202 - 0.000000000000000i

Apparently, Matlab have improved the conversion to double (and let the old q.eval method get deprecated) because the residual error on the imaginary part is smaller yet.

If as above you only want the real part of the result, use the function real in conjunction with double , which still give the same result:

>> double(real(q))
ans =
   1.883829527329202

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM