Skip to content

Commit 56dff41

Browse files
author
Spencer Cohen
committed
Add sin demo
Sin demo approximates the sin function using Smooth.js and prints out the error of the approximation
1 parent cd41eb3 commit 56dff41

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

sindemo.coffee

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
###
2+
sindemo
3+
4+
Sample and cubic interpolate the sin function, then print out max and average error.
5+
###
6+
7+
{Smooth} = require './Smooth'
8+
9+
s = (Math.sin 2*Math.PI*x for x in [0...1] by 1/8)
10+
11+
12+
#Scale the function
13+
smooth_sin = ((f) ->
14+
scaleVal = 0.5*s.length/Math.PI
15+
return (x) -> f x*scaleVal
16+
) Smooth s, method:Smooth.METHOD_CUBIC, clip:Smooth.CLIP_PERIODIC
17+
18+
totalError = 0
19+
count = 0
20+
maxError = 0
21+
for x in [-10..10] by .001
22+
error = Math.abs Math.sin(x) - smooth_sin(x)
23+
maxError = Math.max error, maxError
24+
totalError += error
25+
count++
26+
27+
console.log "Max Error:\t #{(100*maxError).toFixed(10)}%"
28+
console.log "Average Error:\t #{(100*totalError/count).toFixed(10)}%"

0 commit comments

Comments
 (0)