-
Notifications
You must be signed in to change notification settings - Fork 0
/
funTest.js
executable file
·111 lines (103 loc) · 2.69 KB
/
funTest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"use strict";
/*
test and time functions and their approximation
*/
function FunTest(){
this.nothing=function(x){return x;};
this.approxFunction=function(x){return x;};
this.trueFunction=function(x){return x;};
}
/*
check for maximum absolute error
*/
FunTest.prototype.absError=function(start,end,nSteps){
var x=start;
var xMax=-11111111;
var absError=0;
var absErrorMax=-1;
var step=(end-start)/nSteps;
while (x<=end){
absError=Math.abs(this.trueFunction(x)-this.approxFunction(x));
if (absError>absErrorMax){
absErrorMax=absError;
xMax=x;
}
x+=step;
}
console.log("max absolute error: "+absErrorMax+" at "+xMax);
}
/*
check for maximum relative error
*/
FunTest.prototype.relError=function(start,end,nSteps){
var x=start;
var xMax=0;
var relError=0;
var relErrorMax=start;
var step=(end-start)/nSteps;
var trueValue,approxValue;
while (x<=end){
trueValue=this.trueFunction(x);
approxValue=this.approxFunction(x);
relError=2*Math.abs(trueValue-approxValue)/(Math.abs(trueValue)+Math.abs(approxValue));
if (relError>relErrorMax){
relErrorMax=relError;
xMax=x;
}
x+=step;
}
console.log("max relative error: "+relErrorMax+" at "+xMax);
}
// compare at single point
FunTest.prototype.compareAt=function(x){
console.log(x.toPrecision(3)+" orig "+this.trueFunction(x).toPrecision(3)
+" approx "+this.approxFunction(x).toPrecision(3));
}
// compare the approximate function and a "correct" function
FunTest.prototype.compare=function(start,end,nSteps){
var x=start;
var step=(end-start)/nSteps;
var error;
console.log(this.approxFunction);
console.log("compare: x, true function, approximation, error");
while (x<=end+0.01){
error=(this.approxFunction(x)-this.trueFunction(x));
console.log(x.toPrecision(3)+" "+this.trueFunction(x).toPrecision(3)
+" "+this.approxFunction(x).toPrecision(3)
+" "+error.toPrecision(3));
x+=step;
}
}
// time the approximate function and a "correct" function
// test time should be at least tenths of seconds
FunTest.prototype.timing=function(start,end,nSteps){
var step=(end-start)/nSteps;
var x,sum,time;
x=start;
sum=0;
time=new Date().getTime();
while (x<=end){
sum+=this.trueFunction(x);
x+=step;
}
time=0.001*(new Date().getTime()-time);
console.log("time for true function: "+time.toPrecision(3));
x=start;
sum=0;
time=new Date().getTime();
while (x<=end){
sum+=this.approxFunction(x);
x+=step;
}
time=0.001*(new Date().getTime()-time);
console.log("time for approximate function: "+time.toPrecision(3));
x=start;
sum=0;
time=new Date().getTime();
while (x<=end){
sum+=this.nothing(x);
x+=step;
}
time=0.001*(new Date().getTime()-time);
console.log("time for nothing function: "+time.toPrecision(3));
}