CAD/MATLAB function trapz/cumtrapz
Expert: George Moustris - 12/27/2008
QuestionQUESTION: Dear Expert,
I need your help for my uni project. I have a data vector (e.g; znew=[0.089; 0.076; 0.068; 0.065; 0.076; 0.068; 0.087;]; ) and the interval form one data reading to next is 0.008 sec. I used the following to perform integration but I am getting the ans zero. Could you pls tell me what is the problem? 'Total' will be around 1000.
for i=1:1:total;
Final(i)=0.008*cumtrapz(znew(i));
end
ANSWER: Hi Yin Yin,
if a get it correctly, you want to perform numerical integration on the data points contained in the vector znew. This indeed can be performed with the cumtrapz function but you use it in the wrong way. From what i understand, you should do the following:
Final=0.008*cumtrapz(znew);
This would produce a vector called "Final" with as many elements as znew has, in this case 7. However, the cumtrapz function is the cumulative integral and thus if you want the whole integral (and not some intermediary value) you should get the last element of the "Final" vector:
total_Integral=Final(end);
From this point of view, i don't see where the "Total" parameter fits in since the integral vector ("Final" in this case) always has the same number of elements with the integrated vector (znew) and cannot be set to an arbitrary length (e.g. 1000 as in your example).
Hope it help.
Best
G.
---------- FOLLOW-UP ----------
QUESTION: Thank you for your answer. But, when I perform the integration as Final=0.008*cumtrapz(znew); I have the following error.
" ??? Attempt to execute SCRIPT cumtrapz as a function"
Looking forward to your favorable answer.
AnswerHi Yin Yin,
apparently you have overloaded the cumtrapz function. In other words, you have created a file with the name cumtrapz, thus when you call the cumtrapz function (which is native in matlab), matlab first checks to see whether there is a user file with that name and if not, if call the native function. The solution is simple: change the name of your file to something different than cumtrapz.
See also:
http://www.mathworks.com/support/solutions/data/1-18IIZ.html
Best
G.