Asked by DM
on 1 Feb 2015

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

Answer by Mike Hosea
on 1 Feb 2015

Edited by Mike Hosea
on 1 Feb 2015

Accepted Answer

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.

DM
on 2 Feb 2015

Mike, thank you. For sake of knowing that my code is correct. Is the following syntax correct? ( I have copied the three commands your wrote and added an example of the f and v function)

syms x y v

f=x^2*y*v

p=v^(2/3-1)

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)

or should it be

syms x y v

f=x^2*y*v

p=v^(2/3-1)

gscalar = @(x,y)integral(@(v)arrayfun(@(v)(1 - f).*p,v),y,inf);

g = @(x,y)arrayfun(gscalar,x,y);

T = integral2(@(x,y)exp(g(x,y)),0,inf,@(x)x,inf)

I get the following error

Error using arrayfun sym output type is not currently implemented.

Thanks

Mike Hosea
on 3 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.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Star Strider (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/174009-symbolic-integration-vs-numerical-integration#comment_263537

## DM (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/174009-symbolic-integration-vs-numerical-integration#comment_263544

Sign in to comment.