MATLAB Answers

0

Difference between Integral and trapz

Asked by Kushal Kumar on 3 Jun 2019
Latest activity Answered by Geoff Hayes
on 3 Jun 2019
Hi, Thank you for viewing the question.
I have tried to do a comparison between Integral and trapz for a simple function of x.^2. with upper and lower limits as 9 and 0 its a simple sanity check I am trying to do
Intergal value for this function is 243.
To check trapz accuracy I ended up giving x and y values taken from the above function, like x = [0:1:9] and y = [0,1,4,9,16,25,36,49,64,81] and trapz is showing a value of 244.50.
May I know why there is such a difference and what other functions are available for me to get the integral of the above discrete data correct.

  0 Comments

Sign in to comment.

1 Answer

Answer by Geoff Hayes
on 3 Jun 2019

Kushal - from trapz, this function computes the approximate integral of Y via the trapezoidal method with unit spacing. So it is an approximation only. If you increase the number of points, then you will achieve a better result...but it will still be an approximation.
>> x = linspace(0,9,1000);
>> y = x.^2;
>> trapz(x,y)
ans =
243.0001

  0 Comments

Sign in to comment.