Running area under the curve at each time step.

1 回表示 (過去 30 日間)
Jozef
Jozef 2013 年 2 月 10 日
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 Center および File ExchangeNumerical Integration and Differentiation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by