Difference between trapz vs integral
Show older comments
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 Comments
Rodger Herbst
on 30 May 2020
Edited: Walter Roberson
on 31 May 2020
not sure if "comments" is the place to ask another question, but ...
according to MATLAB documentation:
Z = cumtrapz(X,Y) computes the cumulative integral of Y with respect to X using trapezoidal integration. X and Y must be vectors of the same length….
I just want to integrate a vector of ground speed in knots, GNDSKTS, using a vector expressed in time, to give distance in knots.
First I tried the time vector, in hours, whose value in one second in each element is 0.00027778, P272M4, to provide distance in nautical miles, DNAUTM.
In the MATLAB window,
DNAUTM = cumtrapz(P272M4 ,GNDSKTS) %yields a vector of all zeros.
Next I tried a time vector ONE, of 1 second per element 1, and converted GNDSKTS to nautical miles per second, NMPS by dividing GNDSKTS by 3600:
NMPS = GNDSKTS./THIR6HUN
Then I tried:
DNAUTM = cumtrapz(ONE ,NMPS) %Still all zeros
Then I converted vector ONE, with elements of 1 to vector ONEP, with elements 1.0,
DNAUTM = cumtrapz(ONEP ,NMPS) %still all zeros.
All the vectors are of the same length.
I am obviously doing something wrong, but I have no idea what
madhan ravi
on 30 May 2020
Roger Herbst: Es wäre besser , wenn du die Frage als separate gestellt.
Rodger Herbst
on 31 May 2020
Never mind, I figured it out, even though it does not make sense to me, I get correct answers.
Walter Roberson
on 31 May 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
on 23 Jun 2023
Moved: John D'Errico
on 23 Jun 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
Answers (1)
Walter Roberson
on 6 Mar 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 Comments
Walter Roberson
on 6 Mar 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.
Categories
Find more on Numerical Integration and Differentiation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!