Integrating a multivariate function
71 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Walter Roberson
2015-5-18
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 个评论
Mike Hosea
2015-5-19
编辑:Mike Hosea
2015-5-19
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Equation Solving 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!