Integration Limits on Trapz

123 vues (au cours des 30 derniers jours)
MLPhysics
MLPhysics le 24 Fév 2021
Commenté : MLPhysics le 24 Fév 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.

Réponse acceptée

Walter Roberson
Walter Roberson le 24 Fév 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 commentaires
Walter Roberson
Walter Roberson le 24 Fév 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 le 24 Fév 2021
Yes, I forgot about that. Thanks for remembering me. Now the calculation worked fine.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Numerical Integration and Differentiation dans Help Center et File Exchange

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by