diff --git a/pbrt/01/index.html b/pbrt/01/index.html index 69005d0..35fb8f5 100644 --- a/pbrt/01/index.html +++ b/pbrt/01/index.html @@ -98,6 +98,11 @@ return sum; } + function normal_dist(mean, variance, x) { + const dx = x - mean; + return (1 / Math.sqrt(2 * Math.PI * variance)) * Math.exp(-(dx * dx) / (2 * variance)); + } + function monte_carlo(from, to, func, iter) { let sum = 0; for (let i = 0; i < iter; i += 1) { @@ -109,12 +114,19 @@ function integrate(from, to, func) { const max_iter = 16384; - table_element.innerHTML = 'NMonte CarloTrapezoid'; + const m = []; + const t = []; + table_element.innerHTML = 'NMonte CarloMSETrapezoid'; for (let i = 1; i <= max_iter; i *= 2) { - const m = monte_carlo(from, to, func, i); - const t = trapezoid(from, to, func, i); - table_element.innerHTML += '' + i + '' + m.toFixed(3) + '' + t.toFixed(3) + ''; + m.push(monte_carlo(from, to, func, i)); + t.push(trapezoid(from, to, func, i)); + } + + const E = t[m.length - 1]; + for (let i = 0; i < m.length; i += 1) { + const mse = (m[i] - E) * (m[i] - E); + table_element.innerHTML += '' + Math.pow(2, i) + '' + m[i].toFixed(3) + '' + mse.toFixed(3) + '' + t[i].toFixed(3) + ''; } } @@ -146,7 +158,12 @@ } } - const initial_expr = 'cos(x) + sin(2x) + 2cos(4x^2)'; + for (let i = 0; i <= 10; i += 1) { + const t = i / 10 * 2 - 1; + console.log(t + ': ' + normal_dist(0, 0.2, t)); + } + + const initial_expr = 'sqrt(x)+2x-3x^2+x^3sin(x)'; expr_element.value = initial_expr; from_element.valueAsNumber = 0; to_element.valueAsNumber = 3;