TryMove Distance to Beacon 1 (0,0) — meters while leaving the other two distance controls unchanged. Compare the purple point with the dashed circles.
ObserveThe opening diagram labels B1 (0,0), B2 (5,0) and B3 (0,5), and marks the estimate in purple. The Residual error line reports Good fix.
ExplainDistance to Beacon 1 (0,0) — meters supplies one of the ranges used to minimize squared distance mismatches. Residual error is the mean of the absolute mismatches at the estimated point.
Technical boundaries. The solver assumes a flat plane with stationary beacons at (0,0), (5,0) and (0,5), and accepts distances from 0.5 to 8 meters. It starts from circle differences and takes at most 12 Gauss-Newton steps; radio propagation and obstacles are not simulated.
NoteTry It: BLE Trilateration Simulator
Place 3 beacons and adjust the measured distance from each to see where trilateration estimates your position. The circles represent the distance measurement from each beacon — the estimated position is where they intersect.
{// Beacon positionsconst b1 = [0,0], b2 = [5,0], b3 = [0,5];const d1 = tri_d1, d2 = tri_d2, d3 = tri_d3;// Use the algebraic circle-difference solution as an initial guess.const A =2* (b2[0] - b1[0]);const B =2* (b2[1] - b1[1]);const C = d1*d1 - d2*d2 - b1[0]*b1[0] + b2[0]*b2[0] - b1[1]*b1[1] + b2[1]*b2[1];const D =2* (b3[0] - b1[0]);constE=2* (b3[1] - b1[1]);const F = d1*d1 - d3*d3 - b1[0]*b1[0] + b3[0]*b3[0] - b1[1]*b1[1] + b3[1]*b3[1];const det = A *E- B * D;let px = det !==0? (C *E- F * B) / det :2.5;let py = det !==0? (A * F - D * C) / det :2.5;// Refine that guess by minimizing the sum of squared range residuals.// Gauss-Newton makes the widget match the least-squares model in the prose.const observations = [[b1, d1], [b2, d2], [b3, d3]];for (let iteration =0; iteration <12; iteration++) {let h00 =1e-9, h01 =0, h11 =1e-9, g0 =0, g1 =0;for (const [beacon, measured] of observations) {const dx = px - beacon[0];const dy = py - beacon[1];const predicted =Math.max(Math.hypot(dx, dy),1e-9);const jx = dx / predicted;const jy = dy / predicted;const residual = predicted - measured; h00 += jx * jx; h01 += jx * jy; h11 += jy * jy; g0 += jx * residual; g1 += jy * residual; }const normalDet = h00 * h11 - h01 * h01;if (Math.abs(normalDet) <1e-12) break;const stepX = (-g0 * h11 + h01 * g1) / normalDet;const stepY = (h01 * g0 - h00 * g1) / normalDet; px += stepX; py += stepY;if (Math.hypot(stepX, stepY) <1e-7) break; }// Check residualsconst r1 =Math.abs(Math.sqrt(px*px + py*py) - d1);const r2 =Math.abs(Math.sqrt((px-5)**2+ py*py) - d2);const r3 =Math.abs(Math.sqrt(px*px + (py-5)**2) - d3);const avgErr = ((r1 + r2 + r3) /3).toFixed(2);// SVG visualizationconst scale =50;// pixels per meterconst ox =40, oy =310;// origin offsetconst toX = m => ox + m * scale;const toY = m => oy - m * scale;const beacons = [[b1, d1,"#E74C3C","B1"], [b2, d2,"#3498DB","B2"], [b3, d3,"#16A085","B3"]];const circles = beacons.map(([b, d, c, l]) =>`<circle cx="${toX(b[0])}" cy="${toY(b[1])}" r="${d * scale}" fill="none" stroke="${c}" stroke-width="1.5" stroke-dasharray="5,5" opacity="0.5"/> <circle cx="${toX(b[0])}" cy="${toY(b[1])}" r="6" fill="${c}"/> <text x="${toX(b[0])}" y="${toY(b[1]) -10}" text-anchor="middle" font-size="12" fill="${c}" font-weight="bold">${l} (${b[0]},${b[1]})</text>` ).join("");const qualColor =parseFloat(avgErr) <0.5?"#16A085":parseFloat(avgErr) <1.5?"#E67E22":"#E74C3C";const qualLabel =parseFloat(avgErr) <0.5?"Good fix":parseFloat(avgErr) <1.5?"Moderate":"Poor — distances inconsistent";returnhtml`<div style="background:#f8f9fa;padding:1rem;border-radius:8px;border-left:4px solid #9B59B6;margin-top:0.5rem;"> <svg width="340" height="340" style="background:white;border:1px solid #dee2e6;border-radius:4px;"> <!-- Grid -->${Array.from({length:6}, (_, i) =>`<line x1="${toX(i)}" y1="${toY(0)}" x2="${toX(i)}" y2="${toY(5)}" stroke="#f0f0f0"/><line x1="${toX(0)}" y1="${toY(i)}" x2="${toX(5)}" y2="${toY(i)}" stroke="#f0f0f0"/><text x="${toX(i)}" y="${toY(-0.4)}" text-anchor="middle" font-size="10" fill="#999">${i}m</text>`).join("")} <!-- Distance circles -->${circles} <!-- Estimated position --> <circle cx="${toX(px)}" cy="${toY(py)}" r="8" fill="#9B59B6" stroke="white" stroke-width="2"/> <text x="${toX(px) +12}" y="${toY(py) +4}" font-size="11" fill="#9B59B6" font-weight="bold">(${px.toFixed(1)}, ${py.toFixed(1)})</text> </svg> <p style="font-size:13px;margin:8px 0 0;"><strong>Estimated position:</strong> (${px.toFixed(2)}, ${py.toFixed(2)}) | <strong>Residual error:</strong> <span style="color:${qualColor}">${avgErr}m — ${qualLabel}</span></p> </div>`;}
Checkpoint: you should now be able to explain why three noisy distances produce a best-fit position rather than a guaranteed exact location. The residual error tells you whether the three circles agree.