Symbolic integration inside numerical integration

3 次查看(过去 30 天)
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.

采纳的回答

Star Strider
Star Strider 2018-3-17
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 个评论
Gunnar
Gunnar 2018-3-20
编辑:Gunnar 2018-3-20
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Calculus 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by