Would you like to make this site your homepage? It's fast and easy...
Yes, Please make this my home page!
1 --calc_func : declared in main module as function
2 --rv : round to value actualy may be nill too
3 function Simpson_Method(calc_func,rv)
4 print("\nSimpson method");
5 print("I=h/3*[f(a)+f(b)+2*Sigma[f(x(even))]+4*Sigma[f(x(odd))]]");
6 local prevI;
7 local curentI=0;
8 local n=startn;
9 local hofset;
10 local iteration=0;
11 local doneornot=0;
12 local evenssigma;
13 local oddssigma;
14 while doneornot~=1 do
15 iteration=iteration+1;
16 evenssigma=0;
17 oddssigma=0;
18 hofset=(endval-startval)/n;--calculate h
19 --begin sumarize the midle placed values
20 local tcount=1;
21 local i=startval+hofset;
22 while i<endval do
23 if 0==mod(tcount,2) then
24 evenssigma=evenssigma+calc_func(i);
25 else
26 oddssigma=oddssigma+calc_func(i);
27 end
28 i=i+hofset;
29 tcount=tcount+1;
30 end --of for
31 --just by formula
32 curentI=roundval(hofset/3*(calc_func(startval)+calc_func(endval)+oddssigma*4+evenssigma*2),rv);
33 --we not checking first iteration because we havn't previous
34 if iteration~=1 then
35 if err>abs(curentI-prevI) then --actualy it is called epsilon
36 doneornot=1;
37 end
38 end
39 print("Curent iteration: "..iteration,"I: "..curentI);
40 prevI=curentI;--store curent value for later use
41 if doneornot==0 then
42 n=n*2;--make it bigger by two
43 end
44 end --of while
45 return curentI,iteration,n,hofset;
46 end