integrate a partial area under a plotted curve
조회 수: 10 (최근 30일)
이전 댓글 표시
Hello every one,
I'm trying to calculate a partial area under a curve.
I've seen some asked questions about the subsject, but still struggling with some points.
I plotted a cuvre using numerical column vectors (electrical current versus time), then using this proposed solution, i tried to calculate the area under the curve:
yi = interp1(x, y, [x1:x2]); % x1 and x2 are the boundaries
shaded_area = trapz([4:5],yi+5)
Where y represents my electrical current vector and x my time vector, which contains by the way negative values, but i don't think that causes any problem.
When i execute the first line, it returns 'Nan' values!! So, I tried other column vectors which are extracted from the former ones (x and y) but i took only the positive values and again it returns NaN.
I thinking about extrapolating problem, but i have non idea about how to start with this issu?
It'll be very helpful if some one could give me some advice. Thank you!
Please find the attached source data file, function allowing importing data and the commands file (in the commands file, i changed the name of the dataset).
Using Matlab_2018b
댓글 수: 8
darova
2020년 2월 25일
I think these are the same values?
endingIndex = find(x==x(indexMax));
I suggest you use this instruction
% startingIndex = find(x==-2*1e-6);
startingIndex = find(x>-2e-6,1,'first');
Why not '=='? Example
>> a = [1 2 1/3];
ix = find(a==0.333)
ix =
Empty matrix: 1-by-0
채택된 답변
Star Strider
2020년 2월 25일
To get the area, add these lines to your code:
[TF,Q0] = ischange(ifuse, 'linear', 'Threshold', 1.5E+6);
[vl,vli] = min(ifuse(TF));
[pk,pki] = max(ifuse(TF));
Idx = find(TF);
idxrng = Idx(vli):Idx(pki);
Int_ifuse_pk = trapz(ox_axis1(idxrng), ifuse(idxrng))
figure
plot(ox_axis1,ifuse,'r')
hold on
% plot(ox_axis1(TF),ifuse(TF),'+g')
patch([ox_axis1(idxrng); flipud(ox_axis1(idxrng))], [ifuse(idxrng); ones(size(ifuse(idxrng)))*ifuse(Idx(vli))], 'k', 'FaceAlpha',0.25)
hold off
([min(ox_axis1) max(ox_axis1)])
text(max(ox_axis1(idxrng)), mean(ifuse(idxrng)), sprintf('\\leftarrow Area = %.3f', Int_ifuse_pk), 'HorizontalAlignment','left')
producing the desired area (as ‘Int_ifuse_pk’), and this plot:
Experiment with this to get different results.
댓글 수: 5
Star Strider
2020년 2월 25일
The best 'Threshold' parameter value may vary with different data. A value of 1.5E4 could work for all of them.
The ischange call just has to identify the beginning and peak of the pulse, with the min and max detection steps finding the correct indices, regardless of how many changes ischange returns.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Bartlett에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!