Would you like to make this site your homepage? It's fast and easy...
Yes, Please make this my home page!
1 print("Enviroment:",_VERSION);-- It will work in Lua4
2 print("Programed by Dmitry Salomatine\nIsrael 2003.");
3 print("Numerical Integration.");
4 print("Integral value calculation in 4 methods.");
5 print("System representation of e :"..exp(1));
6 startval=1;-- integral from
7 endval=2;-- integral to
8 print("Integral of x*exp(x)dx from "..startval.." to "..endval);
9 handbookvalue=exp(endval)*(endval-1)-exp(startval)*(startval-1);
10 print("HandBook value :"..handbookvalue);
11 err=0.0005; -- epsilon for delta (error) checks
12 print("Epsilon used: "..err);
13 grv=12;--default round level
14 print("Default round to: "..grv.." after point");
15 startn=10;-- start number of area partitioning
16 --begin loadin codes from ...
17 dofile("TrapezaM.lua");--contain Trapezoid method function
18 dofile("SimpsonM.lua");--contain Simpson aproximation method function
19 dofile("POTM.lua");--contain Taylor's series method function
20 dofile("MonteCM.lua");--contain Monte-Carlo method function
21 --rounds values to requested reliability
22 function roundval(v,to)
23 local fs;
24 if to then
25 fs=format("%%.%df",to);
26 else
27 fs=format("%%.%df",grv);--default round to
28 end
29 --return rounded value according to accuracy
30 return (0+format(fs,v));
31 end
32 --setup our function
33 function our_f(val)
34 return val*exp(val);
35 end
36 --Create the end tables
37 t1={name,bi,bv,bn,bh,relerr};
38 t2={name,bi,bv,bn,bh,relerr};
39 t3={name,bi,bv,bn,bh,relerr};
40 t4={name,bi,bv,bn,bh,relerr};
41 --create the union table
42 tm={};
43 --set up member values
44 tm[1]=t1;
45 tm[2]=t2;
46 tm[3]=t3;
47 tm[4]=t4;
48 --call to the methods
49 tm[1].name="Trapezoid";
50 tm[1].bv,tm[1].bi,tm[1].bn,tm[1].bh=Trapezoid_Method(our_f);
51 print("Value: "..tm[1].bv,"Iterations: "..tm[1].bi,"N: "..tm[1].bn,"h: "..tm[1].bh);
52 tm[1].relerr=(abs(tm[1].bv-handbookvalue))/handbookvalue*100;
53 print("Relative error: "..tm[1].relerr.."(%)");
54 print("Press Enter");
55 read"*l";
56 tm[2].name="Simpson";
57 tm[2].bv,tm[2].bi,tm[2].bn,tm[2].bh=Simpson_Method(our_f);
58 print("Value: "..tm[2].bv,"Iterations: "..tm[2].bi,"N: "..tm[2].bn,"h: "..tm[2].bh);
59 tm[2].relerr=(abs(tm[2].bv-handbookvalue))/handbookvalue*100;
60 print("Relative error: "..tm[2].relerr.."(%)");
61 print("Press Enter");
62 read"*l";
63 tm[3].name="Taylor";
64 tm[3].bn=0;
65 tm[3].bh=0;
66 tm[3].bi=0;
67 tm[3].bv=PolinomOfTaylor_Method();
68 print("Value: "..tm[3].bv);
69 tm[3].relerr=(abs(tm[3].bv-handbookvalue))/handbookvalue*100;
70 print("Relative error: "..tm[3].relerr.."(%)");
71 print("Press Enter");
72 read"*l";
73 tm[4].name="Monte-Carlo";
74 tm[4].bh=0;
75 tm[4].bi=0;
76 tm[4].bn=0;
77 tm[4].bv=MonteCarlo_Method(our_f,1000000);
78 print("Value: "..tm[4].bv);
79 tm[4].relerr=(abs(tm[4].bv-handbookvalue))/handbookvalue*100;
80 print("Relative error: "..tm[4].relerr.."(%)");
81 print("Press Enter");
82 read"*l";
83 --print header of table
84 function hmy()
85 print(format("%-11s|%-14s|%-5s|%-10s|%-9s|%-14s|","Method","Value","Part","Iteration","The h","Err(%)"));
86 end
87 --print data of table
88 function pmy(t)
89 ts=format("%-11s|%14.12f|%5d|%10d|%9.7f|%14.12f|",t.name,t.bv,t.bn,t.bi,t.bh,t.relerr);
90 print(ts);
91 end
92 hmy();
93 pmy(tm[1]);
94 pmy(tm[2]);
95 print("\nMore methods have been called")
96 hmy();
97 pmy(tm[3]);
98 pmy(tm[4]);
99 -- just to wait for responce and not to close window automaticaly
100 print("Press Enter To Exit");
101 read"*l";
102 exit();