Running area under the curve at each time step.

1 view (last 30 days)
Hello,
In the problem that I am working on, I am given an x and y set of data. The x being the time and the y being ft/s^2. I need to find the running area under the curve because that will be the velocity, needless to say I am having some troubles. The code is below
My code:
clear;
A=csvread('exc2.csv') %read data file
x=A(:,1)
y=A(:,2)
for i=1:length(A) %run the length of the array
v(i)=trapz(x(i),y(i)) %integral at each point
end
plot(x,v) %plot integration over same time step
I get an error stating "ORDER contains an invalid permutation index."
Am I at all on the right track for trying to solve this?
Thank you.

Answers (1)

ChristianW
ChristianW on 10 Feb 2013
Your input to trapz is a single point, you want to input a line. And for n = length(x) acceleration points you can only get n-1 velocity points.
clear;
x = 0:10;
y = sin(x);
for i=2:length(x) %run the length of the array
v(i-1) = trapz(x(1:i),y(1:i)); % integral at each point
end
subplot(211),plot(x,y)
subplot(212),plot(x(2:end),v)
  2 Comments
Jozef
Jozef on 10 Feb 2013
A Thousand thank yous! hours were wasted, the solution was to simple...
Walter Roberson
Walter Roberson on 10 Feb 2013
I would suggest using cumtrapz instead of this loop.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!