% Example 12.5a (Physics 330) function g=indefint(y,dx) %********************************************************* % returns the indefinite integral of the function % represented by the array y. y(1) is assumed to % be y(a), the function value at the lower limit of the % integration. The function values are assumed to be % values at the edges of the subintervals rather than % the midpoint values. Hence, we have to use the % trapezoid rule instead of the midpoint rule: % % integral(y(x)) from x to x+dx is (y(x)+y(x+dx))/2*dx % The answer is returned as an array of values defined % at the same points as y %********************************************************* % the first value of g(x) is zero because at this first value % x is at the lower limit so the integral is zero g(1)=0; N=length(y); % step across each subinterval and use the trapezoid area % rule to find each successive addition to the indefinite % integral for n=2:N % Trapezoid rule g(n)=g(n-1)+(y(n-1)+y(n))*.5*dx; end