Integrating above and below x axis separately
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/469642/image.png)
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.
0 commentaires
Réponse acceptée
Star Strider
le 23 Déc 2020
With no if blocks:
t = linspace(0, 10); % Independent Variable
Powers = sin(2*pi*t) + randn(size(t))*0.1; % Signal (With Some Noise)
Poswork = trapz(t, Powers .* (Powers > 0)); % With Independent Variable (More Accurate)
Negwork = trapz(t, Powers .* (Powers < 0)); % With Independent Variable (More Accurate)
Poswork = trapz(Powers(:) .* (Powers(:) > 0), 1); % With Point Spacing, Forcing Column Vector
Negwork = trapz(Powers(:) .* (Powers(:) < 0), 1); % With Point Spacing, Forcing Column Vector
.
2 commentaires
Plus de réponses (1)
Image Analyst
le 23 Déc 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 commentaires
Image Analyst
le 23 Déc 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);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/469837/image.png)
Voir également
Catégories
En savoir plus sur Numerical Integration and Differentiation dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!