Borrar filtros
Borrar filtros

Difference between trapz vs integral

38 visualizaciones (últimos 30 días)
atsprink
atsprink el 6 de Mzo. de 2018
Movida: John D'Errico el 23 de Jun. de 2023
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 comentarios
Walter Roberson
Walter Roberson el 31 de Mayo de 2020
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 el 23 de Jun. de 2023
Movida: John D'Errico el 23 de Jun. de 2023
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

Iniciar sesión para comentar.

Respuestas (1)

Walter Roberson
Walter Roberson el 6 de Mzo. de 2018
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 comentarios
atsprink
atsprink el 6 de Mzo. de 2018
Editada: atsprink el 6 de Mzo. de 2018
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 el 6 de Mzo. de 2018
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Numerical Integration and Differentiation en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by