Search code examples
matlabfunctionintegral

How Matlab knows the function that a vector is produced by to calculate cumtrapz?


I have maybe a silly question. When I write the following script in Matlab y=[1 4] and then when I use cumtrapz(y) I get [0 2.5]. I know cumtrapz thinks the deltaX=1 in this method. My question is that how matlab knows the vector 1,4 is created by f(x)=x^2? one could say it is created by f(x)=3x-2 by just identifying y vector values or many other type of functions. So How matlab knows which function has produced Y vector in order to approximate the integration of the function? Thanks


Solution

  • MATLAB has no idea where those value came from

    a = [1 4 5 9]
    s = cumtrapz(a)
    s =
    0.00000    2.50000    7.00000   14.00000
    

    standard step is 1 so:

    h = 1;
    s(i) = s(i-1) + h*(a(i-1)+a(i))/2