Skip to content

Commit

Permalink
pbrt 01
Browse files Browse the repository at this point in the history
  • Loading branch information
Hadopire committed Jun 11, 2024
1 parent 21dac1b commit 4565835
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions pbrt/01/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -109,12 +114,19 @@

function integrate(from, to, func) {
const max_iter = 16384;
table_element.innerHTML = '<tr><th>N</th><th>Monte Carlo</th><th>Trapezoid</th></tr>';
const m = [];
const t = [];
table_element.innerHTML = '<tr><th>N</th><th>Monte Carlo</th><th>MSE</th><th>Trapezoid</th></tr>';

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 += '<tr><th>' + i + '</th><th>' + m.toFixed(3) + '</th><th>' + t.toFixed(3) + '</th></th>';
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 += '<tr><th>' + Math.pow(2, i) + '</th><th>' + m[i].toFixed(3) + '</th><th>' + mse.toFixed(3) + '</th><th>' + t[i].toFixed(3) + '</th></th>';
}
}

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 4565835

Please sign in to comment.