How to use trapz and cumtrapz

149 views (last 30 days)
Jon
Jon on 1 May 2011
I'm having problems understanding how to use trapz. Lets say I have this code:
quad(@(x)myfun,-2,2)
Were myfun is a function in a different .m-file. But I want to solve it using trapz instead of quad. With quad its simple. You just write your function and then the limits of the integral. But with trapz I'm not at all sure how to do even when having read the help about it several times.
Another question on the same topic is about cumtrapz. I wonder what the difference between trapz and cumtrapz are and when to use which?
EDIT:
Let me specify some further questions. First in trapz(X,Y) what doest X and Y signify? Is Y the function that trapz solves? If it is so what does X do? My problem is bigger than one small integral and to better understand how to solve it I will show a more complicated example which is part of what I have to do. I got a function that looks like this:
function Wiz = Wizfun(y,T,TM,TMO,G2,A,b,x,Epsi,ny,Vc,c2,D2,Q,z,sigma)
G = G2.*(1+((T-300)./TM).*TMO);
Ei = -(1/2.*pi).*((1+ny)./(1-ny)).*G.*A.*Epsi;
Wiz = -(2/3).*b.*x.*Ei.*(1./(x.^2+z.^2));
I know I'm not using all the in-data in the function but I have many functions who calls each other and some of them uses all those parameters. In my main window I then write like this:
syms y
T = 1273;
TM = 2163;
TMO = -0.5;
G2 = 126000;
A = 1.2E-29;
b = 0.000000000258;
x = 2/3*b;
Epsi = 0.00487903650119246;
ny = 0.3;
Vc = 1E-9;
c2 = 18.07551;
D2 = 0.0000169;
Q = 263900;
syms z;
sigma = 170;
intg = quad(@(z) Wizfun(y,T,TM,TMO,G2,A,b,x,Epsi,ny,Vc,c2,D2,Q,z,sigma),-250*b,250*b)
And I get this answer:
intg =
1.3118e-034
If I write trapz as Matt suggest:
Edit 2, changing from x = -2:.01:2; to the right numbers:
x = -250*b:0.1*b:250*b;
intg = trapz(x,Wizfun(y,T,TM,TMO,G2,A,b,x,Epsi,ny,Vc,c2,D2,Q,z,sigma))
I get this answer:
intg =
0
Why does this value differ from quad? Is it because the quad value is too small? If I write like Jan suggest:
X = linspace(-2, 2, 1000)
Y = Wizfun(y,T,TM,TMO,G2,A,b,x,Epsi,ny,Vc,c2,D2,Q,z,sigma); % Or a loop, if myfun cannot handle vectors
Int = trapz(X, Y)
I get this error:
??? Undefined function or method 'max' for input arguments of type 'sym'.
Error in ==> trapz at 43
perm = [dim:max(ndims(y),dim) 1:dim-1];
Error in ==> test at 30
Int = trapz(X, Y)
This might be because of that loop thing you wrote about but I'm not sure what you meant by that and how to do a loop.

Accepted Answer

Matt Fig
Matt Fig on 1 May 2011
x = -2:.01:2;
intg = trapz(x,myfun(x));
Jan beat me to it! But CUMTRAPZ is the cumulative integral on your data, not a function. So it is like doing this (explicitly with a loop):
x = 0:.1:2;
cum_int = zeros(size(x));
cnt = 1;
for ii = 0:.1:2
cum_int(cnt) = quadl(@sin,0,ii);
cnt = cnt+1;
end
cum_int2 = cumtrapz(x,sin(x));
plot(x,cum_int,'b',x,cum_int2,'r')
%
%
%
EDIT In response to your further questions.
You need to think a little bit about what you are doing! You were given examples of what TRAPZ does. TRAPS doesn't work on functions, but data... Look again at the examples. You get different numbers because you are doing different things. When using quad you are integrating from:
>> b = 0.000000000258;LH = -250*b
LH =
-6.45e-008
LH to abs(LH). When using TRAPZ you are integrating from -2 to 2 (as you showed in your first question) with a much larger spacing than used from LH to abs(LH). Remember TRAPZ operates on data (look again at the examples given to you), not functions. QUAD operates on functions so it can evaluate a function on arbitrary (well, not quite, but for all intents and purposes...) spacing while determining the integral - TRAPZ is stuck with the spacing in the data. Thus if the spacing in the data misses some important features of the underlying function, TRAPZ won't see it.
Since you have a function, you should stick with QUAD or QUADL.
  2 Comments
Jon
Jon on 1 May 2011
Sorry, copied to fast and got the wrong numbers. Still x = -250*b:0.1*b:250*b also gives the answer 0. I edited it to be correct. If quad is for functions then it is quad I will use. Was trying to see if it was possible to change to trapz to see if it would solve another issue I'm having with quad but I will, maybe, take up that issue in another question instead.
Matt Fig
Matt Fig on 1 May 2011
Since you have a function and not just data, you should use QUAD or one of the newer functions, like QUADL, QUADV or QUADG.

Sign in to comment.

More Answers (2)

Jan
Jan on 1 May 2011
While QUAD operates on a function, TRAPZ and CUMTRAPZ operate on the data. So you have to call your function at first with a manually defined set of X-values to get the Y-values:
X = linspace(-2, 2, 1000);
Y = myfun(X); % Or a loop, if myfun cannot handle vectors
Int = trapz(X, Y);
While TRAPZ replies the scalar value of the integral, CUMTRAPZ accumlates it for each X-value and has the same size as Y. Therefore the reply of CUMTRAPZ starts with 0 ever and ends with the value, which would be replied by TRAPZ.

Jon
Jon on 1 May 2011
I edited my question

Community Treasure Hunt

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

Start Hunting!