Running area under the curve at each time step.

2 次查看(过去 30 天)
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.

回答(1 个)

ChristianW
ChristianW 2013-2-10
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 个评论
Jozef
Jozef 2013-2-10
A Thousand thank yous! hours were wasted, the solution was to simple...
Walter Roberson
Walter Roberson 2013-2-10
I would suggest using cumtrapz instead of this loop.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Numerical Integration and Differentiation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by