Difference between trapz vs integral

조회 수: 129 (최근 30일)
atsprink
atsprink 2018년 3월 6일
이동: John D'Errico 2023년 6월 23일
Hello all,
I am trying to understand the difference between trapz() and integral(). The following is my code for integral():
gcn = sqrt(E).*((2^(1/2)*me_star^(3/2))/(pi^2*h_bar^3));
fen = 1./(1+(exp((E-Efn)./(kB*Temp))));
fun = @(E)gcn.*fen;
Nn = integral(fun,0,Eg,'ArrayValued',true);
where E is a vector of 1000 points and other variables are just constants.
The following is my code for trapz():
gcn = sqrt(E).*((2^(1/2)*me_star^(3/2))/(pi^2*h_bar^3));
fen = 1./(1+(exp((E-Efn)./(kB*Temp))));
fun = gcn.*fen;
Nn = trapz(E,fun);
Right now, the integral() returns a vector with size 1x1000 while trapz() returns a single value. Why does trapz() return a single value? It is taking the same arguments, meaning vector inputs, but doesn't produce a vector output like integral()?
  댓글 수: 5
Walter Roberson
Walter Roberson 2020년 5월 31일
Watch out for the order of the inputs. cumtrapz(X,Y) with constant X is saying that all of the different Y values are at the same X location, and since dx = 0 since the X is constant, value * dx = 0 so the integral is going to be 0.
Independent variable goes first, dependent variable goes second.
abolfazl mahmoodpoor
abolfazl mahmoodpoor 2023년 6월 23일
이동: John D'Errico 2023년 6월 23일
Dear atsprink
have you succeed to calculate this integral?
I am trying to calculate similar integral using
Rspon = integral(fun,Eg2,Inf,'ArrayValued',true);
Eg2 is a const. number
but if gives inf or NAN

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2018년 3월 6일
If you want a vector value then you should consider cumtrapz()
Your use of integral() is wrong. It would be more valid to have used
gcn = @(E) sqrt(E).*((2^(1/2)*me_star^(3/2))/(pi^2*h_bar^3));
fen = @(E) 1./(1+(exp((E-Efn)./(kB*Temp))));
fun = @(E) gcn(E).*fen(E);
Nn = integral(fun,0,Eg);
You might notice this ignores the vector E of 1000 points.
integral() is strictly for integrating a function (which might possibly be multi-valued), and is never for use for calculating the integral at a restricted list of points.
trapz() is for doing a trapezoid numeric integration for the given points. It returns a scalar.
cumtrapz() is similar to trapz() but provides a cumulative answer from the beginning of the vector of x coordinates to the end.
  댓글 수: 2
atsprink
atsprink 2018년 3월 6일
편집: atsprink 2018년 3월 6일
Thanks for this information, but can you clarify what you mean by resitricted list of points? The matlab documentation for integral shows an xmin and xmax which to me seem like a restricted listed of points for the integral.
q = integral(fun,xmin,xmax)
Walter Roberson
Walter Roberson 2018년 3월 6일
xmin and xmax is a range of points, not a restricted list of points.
Your previous code had
fun = @(E)gcn.*fen
Nn = integral(fun,0,Eg,'ArrayValued',true);
this is the same as
fun = @(a_variable_not_used_anywhere_at_all) gcn.*fen
-- the E in the @(E) has no relationship to any E value anywhere else in the code you had given. You were ignoring the input which is the list of values to calculate the integral of. Effectively what you were doing was calculating a vector of constants and then asking to integrate the vector of constants. Well, the integral of a constant is just the constant times (upper bound minus lower bound) so instead of using integral you might as well have just calculated
Nn = gcn .* fen .* Eg;
This is trivial integration: each entry of the vector gcn .* fen just being multiplied by the distance between the bounds. This is not establishing a list of points "along the way" and asking to do numeric integration given those specific points: that kind of calculation is what you use trapz() or cumtrapz() for. Your various gcn .* fen values do not somehow interact with each other to calculate the "integral" in what you had.
integral() is for definite integration with formulas, like sin(exp(-x.^2)) where you know the beginning and end points but you want the routine to figure out how to adapt itself to calculate accurately over whatever shape the function turns out to have, where you do not know ahead of time which interior points to evaluate the function at.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by