algorithm - Trapez Method in Java -
i found formula in net calculating trapezoid method , works should, not see why should performed next lines in trapez method:
sum = 0.5 * bef + (h * sum); i= i+ 2
the first iteration performed next command in main :
tra[0] = 0.5 * ((b - a) / n) * (function(a) + function(b)); //calculates first step value
the trapez method next iterations:
/** * calculate next step trapez method * @param -lower limit * @param b -upper limit * @param bef -previous step value * @param n -number of dividing points * @return integral area */ public static double trapz(double a, double b,double bef, int n) { double sum = 0; double h = ((b - a)/n); (int = 1; <= n; = + 2) { sum += function(a + (i) * h); } sum = 0.5 * bef + (h * sum); homecoming sum; }
the function used in conjunction driver loop doubles number of subintervals @ each iteration, refining estimated integral until difference 1 iteration next less threshold criterion. desirable in such endeavor avoid repeating computations have been performed, , that's point of lines asked about.
consider function values needed when applying trapezoid rule on given number of subintervals. consider function values needed splitting each subinterval in half , applying trapezoid rule subintervals. half (give or take 1) of function values needed in latter case same ones needed in former. code presented reuses computed values (0.5 * bef
), adding them new values (i = + 2
). must scale downwards previous estimate factor of 2 business relationship splitting subintervals in two.
note code right, appears argument n
must represent number of subintervals of integration region, not number of dividing points documentation claims.
java algorithm math integral
No comments:
Post a Comment