MATLAB: Matlab integral not giving right result as trapz function

numerical integration

I have the following code
x = 0:0.1:1;
y = x.*x;
dy = gradient(y);
dx = gradient(x);
dx_fun = @(z) spline(x,dx,z);
dy_fun = @(z) spline(x,dy,z);
integral(dy_fun,0,1)
trapz(dy_fun(x))
the result obtained by the integral function does not match the result obtained by trapz function. Both bounds of the intergation are the same for both function. Value of trapz(dy_fun(x)) = y(end,1)

Best Answer

Try this
integral(dy_fun,0,1)
trapz(dy_fun(x).*dx) % <== multiply the function value with dx