Integrating a multivariate function
74 views (last 30 days)
Show older comments
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);
0 Comments
Accepted Answer
Walter Roberson
on 18 May 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 Comments
Mike Hosea
on 19 May 2015
Edited: Mike Hosea
on 19 May 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.
More Answers (0)
See Also
Categories
Find more on Equation Solving 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!