Integrating a multivariate function
조회 수: 74 (최근 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!