integrate a partial area under a plotted curve

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 Comments

Please make a simple drawing of the result you want
Hello Darov,
there it is, i'd like to calculate the area under the curve represented by the dashed line triangle.
Thank you
So you want to find 2 points (red on my drawing)
I wanted to calculate the area covered by the triangle:
I found a way to do that with trapz, but i wasn't sure.
startingIndex = find(x==-2*1e-6);
endingIndex = find(x==x(indexMax));
desiredX = x(startingIndex:endingIndex);
desiredY = ifuse2(startingIndex:endingIndex);
area = trapz(desiredX,desiredY);
What are those values?
they are the x axis boundaries, indexMax is the index of the cell corresponding to the max. value of my Y axis (ifuse2
here).
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

Sign in to comment.

 Accepted Answer

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 Comments

Hello Star Strider,
Thanks a lot, i had quiet the same idea in my previous comment and i compared the two methods, the results are the same. Still it was a combination between your former ideas in other related questions and some others.
But yours is more general and applicable to any curve shaped like this one, so i gonna use it for the my next data traitements (and i have quiet some to do!).
Thank you again for your help!
Amir
As always, my pleasure!
ADDENDUM —
The easiest way to read this file and produce the appropriate variables:
[~,~,raw] = xlsread('tek0005ALL.csv');
ox_axis = cell2mat(raw(16:end, 1));
vbus = cell2mat(raw(16:end, 2));
vfuse = cell2mat(raw(16:end, 3));
ifuse = cell2mat(raw(16:end, 4));
ox_axis1=(ox_axis);
ox_axis1=ox_axis1.*1e6;
That may vary for other files of this type, however it works for this one. (This was excerpted from ‘routine_datas.m’)
Thank you, very simple!
Though, i do have a little information concerning the value 1.5E6.
When i try with another data file (still the same kind of shape), it doesn't calculates the area, so i lowered the threshould value to 1.5E4, and it worked.
It may be helpful for someone who wants to try this!
Amir
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!