Integrating above and below x axis separately
Ältere Kommentare anzeigen
I have 90 trials of walking data that vary in duration, but the longest lasting 93 timestamps (so, I have a 93X90 matrix named Powers). Below is a plot of one trial, where the x axis is time and the y axis is Joint Power:

I am wanting to integrate the curve below and above the x axis separately so that I have the total positive and negative work. I have tried using if end statements and the trapz function but that didnt seem to work and I'm not sure why.
if Powers > 0
PosWork = trapz(Powers,1)
end
if Powers < 0
NegWork = trapz(Powers, 1)
end
How would I integrate a curve below and above the x axis separately for every column of my matrix? Any help is appreciated. Thank you in advance.
Akzeptierte Antwort
Weitere Antworten (1)
Image Analyst
am 23 Dez. 2020
Try
if Powers > 0
p2 = Powers
p2(p2 <= 0) = 0; % Zero out negative going values.
PosWork = trapz(p2, 1)
end
if Powers < 0
p2 = Powers
p2(p2 >= 0) = 0; % Zero out positive going values.
NegWork = trapz(p2, 1)
end
5 Kommentare
LRW
am 23 Dez. 2020
Image Analyst
am 23 Dez. 2020
What columns? You show a single line plot. If it's not storing any values, then it's not getting into the if block. Not sure why those are there anyway. Get rid of them
p2 = Powers
p2(p2 <= 0) = 0; % Zero out negative going values.
PosWork = trapz(p2, 1)
p2 = Powers
p2(p2 >= 0) = 0; % Zero out positive going values.
NegWork = trapz(p2, 1)
You forgot to attach any data so how can I help?
Image Analyst
am 23 Dez. 2020
Looks like you no longer need help since you accepted Star's answer.
Image Analyst
am 23 Dez. 2020
OK, I did it because it doesn't look like Star's solution reads in your data and does it over all your columns like you wanted. Try this:
clc; % Clear the command window.
clear all;
close all;
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 15;
fprintf('Beginning to run %s.m ...\n', mfilename);
Powers = csvread('Powers.csv')
x = Powers(:, 1);
subplot(1, 2, 1);
[rows, columns] = size(Powers);
for col = 2 : columns
thisY = Powers(:, col);
thisX = x;
% Remove any nans
nanInidexes = isnan(thisY);
thisY(nanInidexes) = [];
thisX(nanInidexes) = [];
p2 = thisY; % Initialize
% Plot it.
plot(thisX, thisY, '-');
grid on;
hold on;
p2(thisY <= 0) = 0; % Zero out negative going values.
PosWork(col-1) = trapz(p2, 1)
p2 = thisY; % Re-initialize.
p2(thisY >= 0) = 0; % Zero out positive going values.
NegWork(col-1) = trapz(p2, 1)
end
title('Individual Data', 'fontSize', fontSize);
subplot(1, 2, 2);
plot(PosWork, 'b-', 'LineWidth', 2);
hold on;
plot(NegWork, 'b-', 'LineWidth', 2);
grid on;
xlabel('x', 'fontSize', fontSize);
ylabel('Work', 'fontSize', fontSize);
legend('Positive Work', 'Negative Work');
title('Work', 'fontSize', fontSize);
g = gcf;
g.WindowState = 'maximized';
fprintf('Done running %s.m ...\n', mfilename);

Kategorien
Mehr zu Numerical Integration and Differentiation finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!