How to use "trapz" on a double integral?

I have a double integral where f = integral (x.^4 - 3*x*y +6*y.^2)dxdy with the outer limits -2 to 2 and the inner limits are 0 to 3. I am supposed to evaluate this integral using Matlab's built in function "trapz" and set the segment width in the x and y- directions at h = 0.1.
Here's what I have so far:
% integration limits
a = -2; b = 3; n = 2;
% given function
f = @(w,y) ((w.^4) - (3*w*y) + (6*(y^2)))
% implement composite trapezoidal rule
trapz(f,a,b)
I know that I am pretty far off from getting the answer, so I apologize and hopefully don't look too dumb.

3 comentarios

Lia
Lia el 24 de Abr. de 2013
In case anyone later comes along to read this post, I think I figured out a way to do the double integral using trapz.
% integration limits
x = linspace(-2,2);
y = linspace (0,3);
% given function
f = (x.^4) - (3*x.*y) + (6.*(y.^2));
q = x.^4 - x;
% implement trapezoidal rule
I = trapz(x,q.*trapz(y,f))
I don't know if it is right, but hopefully it will get you on the right track.
Jess
Jess el 16 de En. de 2016
Was it right?
Torsten
Torsten el 18 de En. de 2016
Take a look at the example "Multiple Numerical Integrations" under
Best wishes
Torsten.

Iniciar sesión para comentar.

Respuestas (2)

John BG
John BG el 17 de En. de 2016
try
x1=-2;dx=.1;x2=2;
y1=0;dy=.1;y2=3
[X,Y]=meshgrid(x1:dx:x2,y1:dy:y2)
Z=X.^4-3*X.*Y+6*Y.^2
x=x1:dx:x2;y=y1:dy:y2
I=trapz(y,trapz(x,Z,2))
you can visualize the curve with
surf(Z)
mind the negative values
find(Z<0)
meaning the curve is not positive for all values in the patch to run the double integral through. If attempting to measure power, square any function. Hope it helps
John
Sean de Wolski
Sean de Wolski el 24 de Abr. de 2013
Instead of using trapz, try integral2().
doc integral2
If you are on an olde release that does not have integral2, try dblquad.
doc dblquad

1 comentario

Lia
Lia el 24 de Abr. de 2013
I appreciate it, but my prof calls for the use of trapz

Iniciar sesión para comentar.

Categorías

Productos

Preguntada:

Lia
el 23 de Abr. de 2013

Comentada:

el 18 de En. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by