SME
Numerical Methods for Engineers · Problem type
Least-squares line and r²
Use this for “Fit y = mx + b by least squares to (0, 1.1), (1, 2.9), (2, 5.2), (3, 7.1), (4, 8.8) and find r².”
Chart
Least-squares line: y = 1.96x + 1.1
r ≈ 1: a strong positive correlation
Residual = actual − predicted: 2 positive (above the line), 2 negative
Sum of squared residuals ≈ 0.09
The least-squares line makes the sum of the squared residuals as small as it can be
This is an example. Type your own values to start a new problem.
x₁
Point 1, x
example
y₁
Point 1, y
example
x₂
Point 2, x
example
y₂
Point 2, y
example
x₃
Point 3, x
example
y₃
Point 3, y
example
x₄
Point 4, x
example
y₄
Point 4, y
example
x₅
Point 5, x
example
y₅
Point 5, y
example
Σx
Sum of x
worked out (not typed)
Σy
Sum of y
worked out (not typed)
Σx²
Sum of x²
worked out (not typed)
Σxy
Sum of xy
worked out (not typed)
Σy²
Sum of y²
worked out (not typed)
m
Slope
worked out (not typed)
b
Intercept
worked out (not typed)
r²
Coefficient of determination
worked out (not typed)
Formulas
Σx
=
x₁
+
x₂
+
x₃
+
x₄
+
x₅
10 = 0 + 1 + 2 + 3 + 4
Σy
=
y₁
+
y₂
+
y₃
+
y₄
+
y₅
25.1 = 1.1 + 2.9 + 5.2 + 7.1 + 8.8
Σx²
=
x₁
2
+
x₂
2
+
x₃
2
+
x₄
2
+
x₅
2
30
=
0
2
+
1
2
+
2
2
+
3
2
+
4
2
Σxy
=
x₁
×
y₁
+
x₂
×
y₂
+
x₃
×
y₃
+
x₄
×
y₄
+
x₅
×
y₅
69.8 = 0 × 1.1 + 1 × 2.9 + 2 × 5.2 + 3 × 7.1 + 4 × 8.8
Σy²
=
y₁
2
+
y₂
2
+
y₃
2
+
y₄
2
+
y₅
2
164.51
=
1.1
2
+
2.9
2
+
5.2
2
+
7.1
2
+
8.8
2
m
=
(5
×
Σxy
−
Σx
×
Σy)
÷
(5
×
Σx²
−
(Σx)
2
)
1.96
=
(5
×
69.8
−
10
×
25.1)
÷
(5
×
30
−
(10)
2
)
b
=
Σy −
m
× Σx
5
1.1
=
25.1 − 1.96 × 10
5
r
2
=
(5 × Σxy − Σx × Σy)
2
÷
((5
×
Σx²
−
(Σx)
2
)
×
(5
×
Σy²
−
(Σy)
2
))
0.9976
=
(5 × 69.8 − 10 × 25.1)
2
÷
((5
×
30
−
(10)
2
)
×
(5
×
164.51
−
(25.1)
2
))
Assumptions
•
The line y = mx + b makes the sum of squared vertical residuals as small as any line can.
•
The x values are exact and not all equal; the scatter is in y only.
•
r² is the share of the spread in y the line explains: 1 means every point is on it.
Step-by-step
WE KNOW
x₁ = 0, y₁ = 1.1, x₂ = 1, y₂ = 2.9, x₃ = 2, y₃ = 5.2, x₄ = 3, y₄ = 7.1, x₅ = 4, y₅ = 8.8
FIND
sum of x (Σx), sum of y (Σy), sum of x² (Σx²), sum of xy (Σxy), sum of y² (Σy²), slope (m), intercept (b), coefficient of determination (r²)
Step 1 · Find sum of x (Σx)
Use Σx = x₁ + x₂ + x₃ + x₄ + x₅
Add the five x values.
Σx = 0 + 1 + 2 + 3 + 4
Σx = 3 + 3 + 4
Σx = 10
Step 2 · Find sum of y (Σy)
Use Σy = y₁ + y₂ + y₃ + y₄ + y₅
Add the five y values.
Σy = 1.1 + 2.9 + 5.2 + 7.1 + 8.8
Σy = 25.1
Step 3 · Find sum of x² (Σx²)
Use Σx² = x₁² + x₂² + x₃² + x₄² + x₅²
Square each x and add.
Σx²
=
0
2
+
1
2
+
2
2
+
3
2
+
4
2
Σx² = 0 + 1 + 4 + 9 + 16
Σx² = 30
Step 4 · Find sum of xy (Σxy)
Use Σxy = x₁ × y₁ + x₂ × y₂ + x₃ × y₃ + x₄ × y₄ + x₅ × y₅
Multiply each x by its own y and add.
Σxy = 0 × 1.1 + 1 × 2.9 + 2 × 5.2 + 3 × 7.1 + 4 × 8.8
Σxy = 0 + 2.9 + 10.4 + 21.3 + 35.2
Σxy = 69.8
Step 5 · Find sum of y² (Σy²)
Use Σy² = y₁² + y₂² + y₃² + y₄² + y₅²
Square
each
y
and
add;
r
2
needs
it.
Σy²
=
1.1
2
+
2.9
2
+
5.2
2
+
7.1
2
+
8.8
2
Σy² = 1.21 + 8.41 + 27.04 + 50.41 + 77.44
Σy² = 164.51
Step 6 · Find slope (m)
Use m = (5 × Σxy − Σx × Σy) ÷ (5 × Σx² − (Σx)²)
Set
the
derivatives
of
the
squared
error
by
m
and
by
b
to
0
(the
normal
equations)
and
solve
them
for
the
slope.
m
=
(5
×
69.8
−
10
×
25.1)
÷
(5
×
30
−
(10)
2
)
m
=
349 − 251
5 × 30 − 100
m
=
98
150 − 100
m
=
98
50
m
=
1.96
Step 7 · Find intercept (b)
Use b = (Σy − m × Σx) ÷ 5
The second normal equation: the line passes through the mean point (x̄, ȳ).
b
=
25.1 − 1.96 × 10
5
b
=
25.1 − 19.6
5
b
=
5.5
5
b
=
1.1
Step 8 · Find coefficient of determination (r²)
Use r² = (5 × Σxy − Σx × Σy)² ÷ ((5 × Σx² − (Σx)²) × (5 × Σy² − (Σy)²))
The square of the correlation r: the explained part of the spread in y over the whole spread.
r
2
=
(5 × 69.8 − 10 × 25.1)
2
÷
((5
×
30
−
(10)
2
)
×
(5
×
164.51
−
(25.1)
2
))
r
2
=
(5 × 69.8 − 10 × 25.1)
2
÷
((5
×
30
−
100)
×
(5
×
164.51
−
630.01))
r
2
=
(5 × 69.8 − 10 × 25.1)
2
÷
((150
−
100)
×
(822.55
−
630.01))
r
2
=
(5 × 69.8 − 10 × 25.1)
2
÷
(50
×
192.54)
r
2
=
(349 − 251)
2
÷
9,627
r
2
=
98
2
9,627
r
2
=
9,604
9,627
r
2
=
0.9976
Check
Put the numbers back in. Both sides should match.
10 = 0 + 1 + 2 + 3 + 4 ✓
25.1 = 1.1 + 2.9 + 5.2 + 7.1 + 8.8 ✓
30
=
0
2
+
1
2
+
2
2
+
3
2
+
4
2
✓
69.8 = 0 × 1.1 + 1 × 2.9 + 2 × 5.2 + 3 × 7.1 + 4 × 8.8 ✓
164.51
=
1.1
2
+
2.9
2
+
5.2
2
+
7.1
2
+
8.8
2
✓
1.96
=
(5
×
69.8
−
10
×
25.1)
÷
(5
×
30
−
(10)
2
)
✓
1.1
=
25.1 − 1.96 × 10
5
✓
0.9976
=
(5 × 69.8 − 10 × 25.1)
2
÷
((5
×
30
−
(10)
2
)
×
(5
×
164.51
−
(25.1)
2
))
✓
Topic check
See what you know: 5 questions on this topic
New questions each time. Pass to mark it complete.