` a=trapz(y)`

` a=trapz(x,y)`

Inputs | |

`x` |
A vector. |

`y` |
A vector of the same size as `x` if the latter is specified. |

Outputs | |

`a` |
The integral of `y` obtained by the trapezoidal rule. |

If

`x`

is not specified, it is assumed to be the vector `[0:n-1]`

where
`n`

is the size of `y`

. The function returns the sum `(y'*h)/2`

where
`h[i]=x[i+1]-x[i-1]`

for `2<=i<=(n-1)`

, `h[1]=(x[2]-x[1])`

, and
`h[n]=(x[n]-x[n-1])`

. This is, obviously, the implementation of
the trapezoidal rule of integration.
>>// Integral of sin(x) over [0,pi]=2.>>x=[0:.02:1]'*pi>>y=sin(x)>>trapz(x,y)1.9993>>x=[0:.01:1]'*pi // Refined sampling>>y=sin(x)>>trapz(x,y)1.9998