Integration Limits on Trapz
조회 수: 16 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
채택된 답변
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
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.
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


