□ Measurement · statistics
Calibrate with a least-squares line
Fit the best straight line through noisy calibration data for a load cell.
- Level 4 · Systems Engineer
- 4 tests
- technically checked
Your task
To calibrate a load cell you hang known masses (x) and record readings (y). The best straight line y = slope·x + intercept minimises the sum of squared errors:
slope = Σ(x − x̄)(y − ȳ) / Σ(x − x̄)², intercept = ȳ − slope·x̄
Write fitLine(xs, ys) returning { slope, intercept }. If every x is the same (the denominator is 0), return null: a vertical line cannot be fitted this way.
Hints (0 of 2 shown)
What gets checked
- fitLine([0, 1, 2, 3], [1, 3, 5, 7]) → { slope: 2, intercept: 1 }
- fitLine([1, 2, 3], [2, 2, 5]) → { slope: 1.5, intercept: 0 }
Plus 2 hidden tests for edge cases. They run in the sandbox like the others; only the result is shown.
Tab indents · Esc then Tab leaves the editor · Ctrl/⌘ Enter runs · Ctrl/⌘ Shift Enter checks · Ctrl/⌘ / comments
Press Run to see what your program prints.
Your code runs only in this browser, inside an isolated sandbox with no network access, and stops after 3 s. Nothing you type is sent to Tactiverge; drafts and progress are saved in this browser only.