Integration Limits on Trapz

조회 수: 16 (최근 30일)
MLPhysics
MLPhysics 2021년 2월 24일
댓글: MLPhysics 2021년 2월 24일
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.

채택된 답변

Walter Roberson
Walter Roberson 2021년 2월 24일
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
Walter Roberson
Walter Roberson 2021년 2월 24일
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 2021년 2월 24일
Yes, I forgot about that. Thanks for remembering me. Now the calculation worked fine.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by