Integrating a multivariate function

I have a horrendous function that I need to integrate. I think I typed it into MatLab right, but here's the Mathematica version because it's easier on the eyes:
(it will only be a function of x and y for integration, n,m,z are assigned beforehand)
Unfortunately, I don't think any suggestions here can help, at least I tried and they didn't seem to work.
Could someone please illustrate on a simple example, say u = x^2*exp(x*y), how I could go about integrating this beast?
I tried doing something like
>> f = @(x,y)x*y;
>> syms y
>> integral(f,1,2)
thinking that it would just treat y as a constant, but it flipped out :I ... :
Error using @(x,y)x*y
Not enough input arguments.
Error in integralCalc/iterateScalarValued (line 314)
fx = FUN(t);
Error in integralCalc/vadapt (line 132)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 75)
[q,errbnd] = vadapt(@AtoBInvTransform,interval);
Error in integral (line 88)
Q = integralCalc(fun,a,b,opstruct);

 Respuesta aceptada

Walter Roberson
Walter Roberson el 18 de Mayo de 2015
integral() is only for numeric integration of a function in one parameter.
syms x y
u = x^2*exp(x*y);
int(int(u,x),y) %two-dimensional symbolic indefinite integration
If you have a function of two parameters and want numeric integration over fixed boundaries, then use integral2()
u = @(x,y) x.^2 .* exp(x.*y);
integral2(u,xmin,xmax,ymin,ymax)
Make sure that the function is vectorized!

4 comentarios

Solarmew
Solarmew el 18 de Mayo de 2015
Editada: Solarmew el 18 de Mayo de 2015
whoa! that's awesome! :D why does it have to be vectorized tho? (and what does that even mean? >.> ) and is there any fast way to "vectorize" without changing all the operators?
Walter Roberson
Walter Roberson el 18 de Mayo de 2015
Your example had x*y as part of it. The "*" operator is the algebraic matrix multiplication operator. If x and y are scalars, x * y is the same as just multiplying x by y. But if x and/or y are non-scalars then x * y tries to do matrix multiplication, like [a b]' * [c d] being a*c+b*d. If you want each element of x to be multiplied by the corresponding element of y, then instead of x * y you need to use x .* y as the .* operator means to multiply element-by-element.
integral2() requires that the function you pass be able to handle matrices of value, that the output be an array the same as if you had applied the function to individual x and y. So you can't use the matrix multiplication operator because that would give you the algebraic result instead of the element-by-element result. Likewise you need to use .^ and ./ instead of ^ and / as ^ and / have to do with matrix operations when working on non-scalars.
If you have the formula stored in a string, you can use the vectorize() call to convert it to another string that has the corrected operators.
If you have an anonymous function that is not vectorized, and you pass it symbolic variables instead of numeric variables, the result will be a symbolic expression. If you use matlabFunction() on that symbolic expression, as a side effect the result will be vectorized.
Generally though it's better to just edit the source. That makes it clearer what you are doing, and executes more quickly.
hmmm ... i think i kinda get it, maybe ... So unless x and y are matrices, I have to use "." with every operator so MatLab knows not to try to do matrix operations?
In my example x and y are just coordinates, so they are scalars. What if they were matrices? This gives an error for some reason
integral2(@(x,y) exp([1 x]'*[y 0]),0,1,1,2)
Mike Hosea
Mike Hosea el 19 de Mayo de 2015
Editada: Mike Hosea el 19 de Mayo de 2015
The integral functions require that they can evaluate the integrand at batches of points in each call. Consequently, your function will receive arrays as inputs, not the scalar inputs you might have expected. You need to use element-wise operations so that it gives the same answer with, say, 2-by-2 inputs as it would if you evaluated with 4 sets of scalar inputs. If your function cannot accept arrays, you can consider using arrayfun. Search for previous answers of mine to find some examples.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

el 18 de Mayo de 2015

Editada:

el 19 de Mayo de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by