Difference between trapz vs integral
50 次查看(过去 30 天)
显示 更早的评论
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
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
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
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 个评论
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!