How do I integrate two columns of discrete data?i need do operations between the data

HERE are two columns discrete data,the frequency is 20Hz,i need integrate them, the equation is
i try to write some codes,but i don't know it is right or not. Can someone help me check it??
but the discrete data should use the 'trap' function, how can i modify the code?
[str,pathname]=uigetfile('*.csv') ;
[xy]=csvread(str);
n=length(xy);% data length
x=xy(:,1);y=xy(:,2);
for i=1:n % step=1
% F=x/y*(x+y);
p=sum(x(i)/y(i)*(x(i)+y(i)))/20;% frenquency=20
end

 Accepted Answer

for i=1:n % step=1
% F=x/y*(x+y);
p(i)=(x(i)/y(i)*(x(i)+y(i)))/20;% frenquency=20
end
P = sum(p); % use sum outside of loop
Use sum outside of loop

7 Comments

Thank you very much! I'm wondering if the sum is the same as the integral here? can i use the 'trapz' funtion?
Yes sum acts like a discrete integrator which you want. But there are other functions in matlab which serve the same purpose. E.g. integral
Do you get the desired result after making changes to code ?
Yes,I get the result,but I think some tesults are not right, becase I set the X<Y, the results should be less than 1, but there are still some that are greater than 1.
And one more question , in the code you changed,is the equation the same as the next one?
Thank you very much!
%if true
for i=1:n % step=1
% F=x/y*(x+y);
p(i)=(x(i)/y(i))*((x(i)+y(i)))/20);% frenquency=20
end
P = sum(p); % use sum outside of loop
Yes the equation is same. But there is a difference. Notice the parenthesis in the earlier code. Can you try with the new one shown above ?
See earlier it was (x/y*(x+y)/20
Check this (x/y)*(x+y)/20;
I found the error that x<y was not always true,I changed some code about that,so I think I get the right answer ,thanks a lot !!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!