How to find the area between the x axis and positive section of a graph?

Hi
I'm trying to find the 2 separate areas for a Force * Displacement graph. The graph is basically 2 non-perfect, not-symmetrical half ellipses that connect at the x axis.
I need to specifically find the work done (area) for positive and negative forces but not sure how I could do it?
I have the data stored in 2 vectors but is there any way to only find the area of the graph above or below the x axis?
Or is my only choice to truncate the vectors to separate the graph into 2 and use 'trapz' function?
I am a beginner at MATLAB so please go easy on me!
Thank you for any help/tips :)
EDIT:
The reason I dislike the idea of removing the data to separate into positive and negative values of Y axis is because the data alternates and so becomes tiring having to remember which data values to remove from both vectors to maintain the correct plots.
I have found an easy way to separate the Y values from positive to negative (testing(testing < 0) = [];) but then I do not know which elements to delete in the X axis vector (as they can be both +ve and -ve).
If I do trapz(Y_Vector) when it has been separated into positive values, would it work to just multiply by the range of the X_Vector?

2 Comments

What are the 2 vectors you already have? Are they yPositive and yNegative, or are they y (both positive and negative in the same vector) and x?
I have a vector of Force ( contains both +ve and -ve values) and in plots is the y axis.
and I have a vector of Displacement ( contains both +ve and -ve values, corresponding to both +ve and -ve values of Force) and is the x axis.
edit: Maybe this image of the plots and data can help understand what I have:

Sign in to comment.

 Accepted Answer

Without your data it’s not possible to say definitively. It might be best to separate the positive and negative forces. I would use the trapz or cumtrapz functions, depending on the result you want.
EDIT — I get the impression that the +ve and -ve values of force are each sampled at different values of displacement. If that is correct, you would likely need to integrate the +ve and -ve forces separately. If the force values are sampled at the same values of displacement, you can take the differences and integrate them using trapz. Use the relevant displacement values as your x-vector in each instance.

5 Comments

I attached the text file with the data if it helps. If I was to separate the data, is there an easy way to remove elements for a given range rather than individually?
I only know how to do X(7,:)=[] %Deletes the 7th row of the matrix. Is there a way to remove for example, from 5th element to 123 element?
I’m working on the text file and the integration. (Took a break.)
To eliminate the 5th to 123rd element, you would address it as:
X(5:123, :) = [];
This is how I would approach the integration:
fidi = fopen('dada55_nospring_50psi_compression0_rebound0_speed2.txt');
D = textscan(fidi, '%f%f%f', 'Delimiter','\t', 'CollectOutput',1, 'HeaderLines',8);
force = D{:}(:,1);
displ = D{:}(:,2);
idxpos = find(force >= 0); % Force >= 0 Indices
idxneg = find(force < 0); % Force < 0 Indices
[Idispl,ia,ib] = intersect(displ(idxpos), displ(idxneg)); % Common Displacements
workpos = trapz(displ(idxpos), force(idxpos));
workneg = trapz(displ(idxneg), force(idxneg));
figure(1)
plot(displ, force)
grid
xlabel('Displacement')
ylabel('Force')
axis([-15 15 -0.5 0.5])
text(-7, 0.15, sprintf('Work: Force > 0 = %.3f kN\\cdotmm', workpos))
text(-7, -0.15, sprintf('Work: Force < 0 = %.3f kN\\cdotmm', workneg))
text(-5, 0.01, sprintf('Total Work = %.3f kN\\cdotmm', workpos + workneg))
I wanted to see if the displacement values between the positive and negative force values were the same, and out of 2000 total values, only 356 were common to both, so I did the integrals separately.
Thank you very much, you not only helped me with the matlab but also identified a mistake I was doing with my intentions haha. You are right that the integrals needed to be done separately as there were multiple cycles of the data. Thank you and everyone else who gave me tips

Sign in to comment.

More Answers (1)

Why don't you just sum the values above and below the axes
sumAbove = sum(Displacement1(Displacement1 >= 0));
sumBelow = -sum(Displacement1(Displacement1 < 0));
You can multiply by the deltaX spacing if you want calibrated units.

Asked:

on 15 Nov 2015

Commented:

on 15 Nov 2015

Community Treasure Hunt

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

Start Hunting!