Build an IML module tr that calculates an integral (“area under the curve”) using the trapezoidal rule. The module should accept vectors x
and y
as input parameters and return the value of auc
. Show that your module works by applying it as follows:
x = do(-2,5,0.01);
print "Integral of a over x is" ( tr(x,0#x+1) );
print "Integral of b over x is" ( tr(x,1#x) );
print "Integral of c over x is" ( tr(x,x##2) );
print "Integral of d over x is" ( tr(x,abs(x)) );
print "Integral of y over x is" ( tr(x,sin(x##2)) );
print "Integral of z over x is" ( tr(x,log(2+exp(x))) );
Here is my code:
proc iml;
start tr(x, y);
do i=1 to 2000;
auc = (sum(x[i]-(x[i-1]))#(y[i]+(y[i-1])))/2;
return (auc);
end;
finish;
When I try to run the verification code provided, I get the error message that the subscript is invalid or out of date. What does this mean and how do I fix my code so that it works?