How to use trapz and cumtrapz
Ältere Kommentare anzeigen
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.
Akzeptierte Antwort
Weitere Antworten (2)
Jan
am 1 Mai 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.
Kategorien
Mehr zu Numerical Integration and Differentiation finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!