Symbolic integration inside numerical integration

1 view (last 30 days)
I would like to solve the following integral numerically:
As far as I can tell, the built-in function integral2 is not applicable here due to the nature of the expression. Instead, from Evaluating Double Integrals, I arrive at the following solution which takes forever to compute due to the third line:
syms x r
firstint=int(1./(1+x.^2.5),x,r,Inf)
answer=int(r.*exp(-r.^2).*firstint,r,0,Inf),
double(answer)
The last line is just to get a numerical answer out of the symbolic integration. To speed up things, I am inclined to replace the third line with the built-in function integral, but it requires me to make firstint into a handle first, for example by using another built-in function called matlabFunction. Unfortunately, I get an error when doing the following:
syms x r
firstint=int(1./(1+x.^2.5),x,r,Inf),
firstintHandle = matlabFunction(firstint),
answer=integral(r.*exp(-r.^2).*firstintHandle,0,Inf),
Any ideas on what to do? Please be very specific. Your help is greatly appreciated.

Accepted Answer

Star Strider
Star Strider on 17 Mar 2018
See if this does what you want:
firstint = @(r) integral(@(x) 1./(1+x.^2.5), r, Inf, 'ArrayValued',1);
secondint = @(r) r.*exp(-r.^2).*exp(-firstint(r));
answer = integral(secondint, 0, Inf, 'ArrayValued',1)
answer =
0.274214226644371
The code appears to me to be correct. I defer to you to be certain it is.
  6 Comments
Gunnar
Gunnar on 20 Mar 2018
Edited: Gunnar on 20 Mar 2018
Thanks for your replies, Star Strider. I also managed to get a numeric answer based on the first approach, but the error message indicates that it might not be accurate. In comparison, your second solution takes much longer to compute due to the need to do symbolic integration, but on the other hand there is no error message. It is a tradeoff, and it is difficult to know what is the best course of action. Indeed, MATLAB seems to offer multiple ways to numerically evaluate integrals (see for example function 'trapz'), and some integrals may work with one method but not the other. With that said, many thanks for the help so far.

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!