Integrating a multivariate function
71 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Solarmew
il 18 Mag 2015
Modificato: Mike Hosea
il 19 Mag 2015
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 Commenti
Risposta accettata
Walter Roberson
il 18 Mag 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 Commenti
Mike Hosea
il 19 Mag 2015
Modificato: Mike Hosea
il 19 Mag 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.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Equation Solving in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!