Composite Trapezoid rule
Show older comments
I know for a single integral you would use the cumtrapz function. I have a problem where I have to use the composite trapz rule on a double integral and I can find nothing on how to do that. The function is
(x^2 - 2y^2 +xy^3)dxdy where the outer y limits are -1:1 the inner x limits are 0:2 and n = 2
Any help would be much appreciated. The answer is supposed to be 2.
23 Comments
bym
on 9 Apr 2011
are you sure the answer is 2?
Walter Roberson
on 9 Apr 2011
I'm not sure what n is supposed to be here. If it is the number of subdivisions to take, then perhaps the trapezoid comes out as 2 ?
Jason
on 10 Apr 2011
Walter Roberson
on 10 Apr 2011
Are you remembering to multiply by the width of the interval ?
John D'Errico
on 10 Apr 2011
Well, in this case, the width of the interval is exactly 1, so I'm guessing the error is in another place.
John D'Errico
on 10 Apr 2011
To Jason - why not show what you have tried? You are far more likely to get help that way. I'll give you one hint. Did you start with meshgrid?
John D'Errico
on 10 Apr 2011
or ndgrid?
Jason
on 10 Apr 2011
bym
on 10 Apr 2011
I don't see where n factors into your equations. I would suggest you look at meshgrid like John suggested
Jason
on 10 Apr 2011
Jason
on 10 Apr 2011
Jason
on 10 Apr 2011
bym
on 10 Apr 2011
why can't you use meshgrid? Is it specifically forbidden or you don't understand how to use it?
Jason
on 10 Apr 2011
John D'Errico
on 11 Apr 2011
Suppose you knew the value of this function, (x^2-2y^2+xy^3) at a grid of points. Would that help you?
Jason
on 11 Apr 2011
bym
on 11 Apr 2011
how would you plot such a function?
Jason
on 11 Apr 2011
Jason
on 11 Apr 2011
John D'Errico
on 11 Apr 2011
We are not going to do your homework for you. I'm sorry that you have other things to do with your time, but if doing your homework is important to you, then you will find the time. And if it is not important to you, then why should we spend the time to do something that is not important to you anyway? There have been MANY hints offered to you.
Jiro Doke
on 11 Apr 2011
To save you time, I will say that I'm not aware of any built in MATLAB function like cumtrapz for 2D. So your best bet is to implement the algorithm yourself (kind of like the way you did in one of your comments).
The command meshgrid (or ndgrid) helps you easily set up the "grid points" that you need for your algorithm. For example, notice that you went and calculated all the points based on the x and y limits and n. You could do that with meshgrid this way:
[x, y] = meshgrid(linspace(ax, bx, n+1), linspace(ay, by, n+1))
Once you have those, then it's just a matter of using those with your function "(x^2-2y^2+xy^3)" in the composite trapezoidal rule.
Jason
on 11 Apr 2011
Jason
on 11 Apr 2011
Answers (1)
Sulaymon Eshkabilov
on 11 Sep 2020
Here is a simple solution to this exercise:
x = -1:0.02:1;
y = -1:0.025:1;
[xx,yy] = meshgrid(x,y);
Fun = xx.^2 -2*yy.^2+xx.*yy.^2;
ALL = cumtrapz(y,cumtrapz(x,Fun,2));
mesh(xx,yy,Fun)
xlabel('x')
ylabel('y')
str = '$$ \int_{-1}^{1} \int_{-1}^{1} (x^2 -2*y^2+x*y^2 )dxdy $$';
zlabel(str,'Interpreter','latex')
hold on
surf(xx,yy,ALL,'FaceAlpha',0.5,'EdgeColor','none')
plot3(xx(end),yy(end),ALL(end),'md', 'markerfacecolor', 'c')
v = [1 1 1];
view(v);
Categories
Find more on Numerical Integration and Differentiation in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!