You are here:

CAD/MATLAB function trapz/cumtrapz

Advertisement


Question
QUESTION: 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.

Answer
Hi 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.

CAD

All Answers


Answers by Expert:


Ask Experts

Volunteer


George Moustris

Expertise

I have extensive experience in MATLAB which is a CAD for engineers.I can answer many questions on the programming script as well as the use of many toolboxes Matlab uses.However DO NOT ask me about the underlying theory of your projects.I will only answer questions regarding the USE of Matlab.

Experience

I have written many scripts in Matlab utilizing many toolboxes and libraries such as Fuzzy Logic toolbox,Neural Nets,Image processing,Wavelets,Signal processing etc.

Organizations
Aristotle University of Thessaloniki

©2012 About.com, a part of The New York Times Company. All rights reserved.