# Symbolic integration vs numerical integration

5 views (last 30 days)
DM on 1 Feb 2015
Answered: Susan on 14 Feb 2019
I have an expression with three variables x,y and v. I want to symbolically integrate v, and so I use int function
The command that I use is the following:
v =int((1-fxyz)*pv, v, y,+inf)% Its is taking too long for it to compute symbolically what do I do
Are there tricks to make int compute faster?
PS I havent given you what the function fxyv is but it is very complicated.
I know one option for me is to integrate numerically using for example integrate, however I want to note that the second part of this problem requires me to integrate g(x,y) over x and y from 0 to infinity. So I can't take numerical values of x and y when I want to integrate over v I think or maybe not ?
Thanks
Star Strider on 1 Feb 2015
‘I haven’t given you what the function f(x,y,v) is but it is very complicated.’
It may not have an analytic solution. Consider using the integral function to integrate it numerically.
DM on 1 Feb 2015
yes but then i would have to take numerical values of x and y right?
The problem is my second part of this problem requires me to integrate g for both x y from 0 to infinity.

Mike Hosea on 1 Feb 2015
Edited: Mike Hosea on 1 Feb 2015
It's likely to be quite slow, but it may be possible to take this on numerically. For performance reasons MATLAB insists that integrands be "vectorized", i.e. capable of accepting arrays of inputs and returning arrays of outputs, so we will need to address this. First let me assume that f and p are not coded in a way that would permit me to pass in an array and expect them to return an array of the same size.
gscalar = @(x,y)integral(@(v)arrayfun(@(v)(1 - f(x,y,v)).*p(v),v),y,inf);
g = @(x,y)arrayfun(gscalar,x,y);
T = integral2(@(x,y)exp(g(x,y)),0,inf,@(x)x,inf)
First we defined a gscalar function that accepts scalar x and y and evaluates the innermost integral. Then we used arrayfun to "vectorize" the gscalar function, creating a function that could accept arrays of x and y values and calculates the integrals element-wise. Finally integral2 calculates the outer double integral.
You can tack on 'AbsTol' and 'RelTol' arguments to the integral and integral2 calls.
In case you (or someone reading this) are not familiary with arrayfun, arrayfun is a "loop in a box" sort of thing. Think of
z = arrayfun(f,x,y)
as if it were
z = zeros(size(x));
for k = 1:numel(z)
z(k) = f(x(k),y(k));
end
but arrayfun should be faster than writing the loop in MATLAB.
You might be able to drop the use of arrayfun in the definition of gscalar if f and p are coded in such a way that they can handle an array of v inputs combined with scalar x and y inputs and deliver the correct value.
Mike Hosea on 3 Feb 2015
Edited: Mike Hosea on 4 Feb 2015
You can't use syms here at all, or if you do, you need to use matlabfunction(f) and matlabfunction(v). Just define the functions directly in MATLAB like so:
p = @(v)v.^(2/3 - 1)
f = @(x,y,v)x.^2.*y.*v
Now these functions were easy to vectorize using the .^ and .* operators, so we don't need the arrayfun on the inside of the first integral call. I ran this:
p = @(v)v.^(2/3 - 1);
f = @(x,y,v)x.^2.*y.*v;
gscalar = @(x,y)integral(@(v)(1 - f(x,y,v)).*p(v),y,inf);
g = @(x,y)arrayfun(gscalar,x,y);
warning('off','MATLAB:integral:MinStepSize');
T = integral2(@(x,y)exp(g(x,y)),0,inf,@(x)x,inf)
and it gave me
T =
0
This appears to occur because the g(x,y) values are negative and grow so large in magnitude so quickly that exp(g(x,y)) is numerically zero at every sampled point.
Notice that I turned off a warning. If you don't, you'll see why. I didn't know it was possible to get this warning over and over again, or if I did at one time, I forgot about it. :)
DM on 4 Feb 2015
Mike, thank you very much. This has really been very helpful.

Susan on 14 Feb 2019
Dear Mike,
a = 0 :10 and x = sqrt(2*a) and the goal is plotting Pb versus x(expresses in decibels (dB)).