Integration Limits on Trapz

13 views (last 30 days)
MLPhysics
MLPhysics on 24 Feb 2021
Commented: MLPhysics on 24 Feb 2021
Hi Everyone!
I have a very simple question. I used MATLAB to solve a set of coupled ODEs from zero to Inf. And now I have the vectors containing the solution for these functions and their first derivatives in this semi-infinite interval.
My question is: How can I ask trapz to integrate a functional (depending on these solutions and their derivatives) from xmin=1e-5 to xmax=100, only? Is it enough to insert extra inputs into trapz?
Thank you,
MdL.

Accepted Answer

Walter Roberson
Walter Roberson on 24 Feb 2021
x = sort(rand(1,20));
y = exp(sin(2*pi*x));
yint = cumtrapz(x, y);
plot(x, y, 'k', x, yint, 'b')
syms X B
Y = exp(sin(2*pi*X));
Yint = int(Y, X, 0, X);
fplot([Y, Yint], [0 1])
This illustrates that you can use trapz() with irregular spacing, and that if the values are close enough, it will approximate the true integral.
If you only want to integrate over a particular range, then find the endpoints of the range in the data and only do that portion:
mask = pi/10 <= x & x <= 2*pi/10;
px = x(mask);
py = y(mask);
pyint = cumtrapz(px, py);
plot(px, py, 'k', px, pyint, 'b')
The reason this does not look the same is that you are not bringing in the accumulated value from 0 to pi/10, and definite integration always starts at 0.
  9 Comments
Walter Roberson
Walter Roberson on 24 Feb 2021
Watch out for the places you had 1/s.sx1tau : s.sx1tau is a vector so 1/ it is matrix division . Notice I corrected them to 1./
The .* I put in are important too.
MLPhysics
MLPhysics on 24 Feb 2021
Yes, I forgot about that. Thanks for remembering me. Now the calculation worked fine.

Sign in to comment.

More Answers (0)

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!