← Programming Concepts in Mechanical Engineering (25ME34I)
SQC Calculations and Process Capability
Programming Concepts in Mechanical Engineering (25ME34I) · C-25 Diploma Mechanical, Semester III
Read with diagrams, search and self-test
After studying this chapter you will be able to:
• Calculate sample average, average of averages, sample range and average range.
• Calculate mean, variance and standard deviation by hand and in Python.
• Estimate the process standard deviation from Rm /d₂.
• Calculate and interpret process capability Cp and process capability index Cpk.
• Generate X-bar and R charts and identify in-control and out-of-control points with Python.
11.1 Measures of Central Tendency and Dispersion
Every set of measurements has a centre (where the values cluster) and a spread (how much they scatter). Quality depends on both: a shaft process may be centred at 25.00 mm but scatter too widely, or it may be very consistent but set at the wrong size.
Measures of central tendency
| Measure | Definition | Example: 12, 15, 11, 15, 17 |
|---|---|---|
| Arithmetic mean xn | Σx / n | 70/5 = 14 |
| Median | Middle value of sorted data | Sorted 11, 12, 15, 15, 17 → 15 |
| Mode | Most frequent value | 15 |
Measures of dispersion
| Measure | Formula | Remarks |
|---|---|---|
| Range R | x_max − x_min | Simple; used for small samples (n ≤ 10) |
| Variance (population) σ² | Σ(x − μ)² / N | When all items are measured |
| Variance (sample) s² | Σ(x − x)n ² / (n − 1) | Estimate from a sample (Bessel's correction) |
| Standard deviation | √variance | Same unit as data; most important measure |
| Coefficient of variation | (s / x)n × 100 % | Relative spread, unit-free |
Worked Example 11.1
Diameters of 5 pins (mm): 25.02, 24.98, 25.01, 25.00, 24.99.
| x | x − x_ | (x − x)_ ² |
|---|---|---|
| 25.02 | 0.02 | 0.0004 |
| 24.98 | −0.02 | 0.0004 |
| 25.01 | 0.01 | 0.0001 |
| 25.00 | 0.00 | 0.0000 |
| 24.99 | −0.01 | 0.0001 |
| Σx = 125.00 | Σ = 0.0010 |
• Mean xn = 125.00 / 5 = 25.00 mm
• Range R = 25.02 − 24.98 = 0.04 mm
• Sample variance s² = 0.0010 / 4 = 0.00025 mm²; s = √0.00025 = 0.01581 mm
• Population variance σ² = 0.0010 / 5 = 0.0002 mm²; σ = 0.01414 mm
11.2 SQC Quantities for Control Charts
| Sym bol | Name | Formula |
|---|---|---|
| Xm | Sample average | Σx / n for each sample |
| X̿(cid:13) | Average of averages (grand mean) | ΣX m/ k |
| R | Sample range | x_max − x_min of each sample |
| Rm | Average range | ΣR / k |
| σ(cid:17) | Estimated process standard deviation | Rm / d₂ (within-subgroup) |
| σ_Xm | Standard deviation of sample means | σ / √n |
Note that A₂ = 3 / (d₂√n). For n = 5: 3 / (2.326 × 2.236) = 0.577. So X̿(cid:13) ± A₂Rm is the same as X̿(cid:13) ± 3σ/√n.
11.3 Process Capability
Process capability is the inherent ability of a process to produce parts within specification limits when it is in statistical control. It compares what the customer wants (the tolerance) with what the process can do (its natural spread of 6σ).
• Specification limits (USL, LSL) are set by the designer/customer – the "voice of the customer".
• Control limits are calculated from process data – the "voice of the process". They must never be confused.
• Natural tolerance or process spread = 6σ.
Process Capability Ratio Cp
Cp = (USL − LSL) / 6σ = specified tolerance / process spread
Cp compares spread only; it assumes the process is centred midway between the limits.
Process Capability Index Cpk
Cpu = (USL − μ) / 3σ, Cpl = (μ − LSL) / 3σ, Cpk = minimum of (Cpu, Cpl)
Cpk considers both spread and centring. Always Cpk ≤ Cp; Cpk = Cp only when the process is perfectly centred. A negative Cpk means the mean is outside the specification.
Fig. 11.1 Effect of spread and centring on Cp and Cpk
Interpretation
| Cp or Cpk value | Interpretation | Approx. defective (centred) | Action |
|---|---|---|---|
| < 1.00 | Not capable – process spread exceeds tolerance | > 2700 ppm | Improve process, 100 % inspection, review tolerance |
| = 1.00 | Barely capable – no margin | 2700 ppm | Tight control needed |
| 1.00 – 1.33 | Capable but marginal | 63–2700 ppm | Monitor closely |
| ≥ 1.33 | Capable (common industry minimum) | ≤ 63 ppm | Normal control |
| ≥ 1.67 | Highly capable (safety-critical parts) | ≈ 0.6 ppm | – |
| 2.00 | Six Sigma level | 0.002 ppm (3.4 ppm with 1.5σ shift) | – |
Worked Example 11.2 – capability of the shaft process
From Example 10.1: X̿(cid:13) = 25.0046 mm, Rm = 0.042 mm, n = 5 (d₂ = 2.326). Specification 25.00 ± 0.05 mm → USL = 25.05, LSL = 24.95.
• σ = Rm / d₂ = 0.042 / 2.326 = 0.01806 mm
• 6σ = 0.1083 mm, tolerance = 0.10 mm
• Cp = 0.10 / 0.1083 = 0.923
• Cpu = (25.05 − 25.0046) / (3 × 0.01806) = 0.0454 / 0.05418 = 0.838
• Cpl = (25.0046 − 24.95) / 0.05418 = 0.0546 / 0.05418 = 1.008
• Cpk = min(0.838, 1.008) = 0.838
Conclusion: although the process is in statistical control (Chapter 10), it is not capable: its natural spread (0.108 mm) is larger than the tolerance (0.10 mm) and it is slightly off-centre towards the USL. Some oversize shafts will be produced. Actions: re-centre the process at 25.000 mm (Cpk rises to Cp = 0.92), then reduce variation (rigid tool holding, better spindle bearings, coolant control) or request a wider tolerance if the design permits.
"In control" and "capable" are different. In control means only chance causes are present (stable). Capable means the stable process fits within the specification. A process can be in control yet not capable – as in Example 11.2.
11.4 Practice Programs (Week 11)
Program 11.1 – Mean, variance and standard deviation (three ways)
# Program 11.1: Mean, variance, standard deviation
import statistics as st
import numpy as np
x = [25.02, 24.98, 25.01, 25.00, 24.99, 25.03, 24.97, 25.01]
n = len(x)
# (a) from first principles
mean = sum(x) / n
var_s = sum((xi - mean) ** 2 for xi in x) / (n - 1)
var_p = sum((xi - mean) ** 2 for xi in x) / n
print(f"Manual : mean={mean:.4f} s^2={var_s:.6f} "
f"s={var_s**0.5:.5f} sigma={var_p**0.5:.5f}")
# (b) statistics module
print(f"statistics: mean={st.mean(x):.4f} s^2={st.variance(x):.6f} "
f"s={st.stdev(x):.5f} sigma={st.pstdev(x):.5f}")
# (c) NumPy (ddof=1 for sample)
a = np.array(x)
print(f"NumPy : mean={a.mean():.4f} s^2={a.var(ddof=1):.6f} "
f"s={a.std(ddof=1):.5f} sigma={a.std():.5f}")
print(f"Median = {st.median(x)}, Range = {max(x)-min(x):.2f}, "
f"CV = {100*st.stdev(x)/mean:.4f} %")Manual : mean=25.0012 s^2=0.000413 s=0.02031 sigma=0.01900 statistics: mean=25.0012 s^2=0.000413 s=0.02031 sigma=0.01900 NumPy : mean=25.0012 s^2=0.000413 s=0.02031 sigma=0.01900 Median = 25.005000000000003, Range = 0.06, CV = 0.0812 %
All three methods agree. statistics.stdev() and np.std(ddof=1) give the sample standard deviation; pstdev() and np.std() give the population value.
Program 11.2 – Complete SQC study: X^, X̿(cid:8) , R, R^ , σ, Cp, Cpk with charts
# Program 11.2: Control chart and capability study import numpy as np import matplotlib.pyplot as plt
data = np.array([
[25.02, 24.98, 25.01, 25.00, 24.99],
[25.01, 25.03, 24.99, 25.00, 25.02],
[24.98, 25.00, 25.01, 24.99, 25.00],
[25.03, 25.01, 25.02, 25.00, 24.99],
[25.00, 24.97, 25.01, 25.02, 24.99],
[25.02, 25.01, 25.00, 24.98, 25.03],
[24.99, 25.00, 25.02, 25.01, 25.00],
[25.01, 24.98, 25.00, 25.03, 25.02],
[25.00, 25.02, 24.99, 25.01, 24.98],
[25.02, 25.00, 25.01, 24.99, 25.04]])
USL, LSL = 25.05, 24.95
A2, D3, D4, d2 = 0.577, 0, 2.114, 2.326 # n = 5xbar = data.mean(axis=1) # sample averages
R = data.max(axis=1) - data.min(axis=1) # sample ranges
X2bar, Rbar = xbar.mean(), R.mean()
sigma = Rbar / d2
Cp = (USL - LSL) / (6 * sigma)
Cpu = (USL - X2bar) / (3 * sigma)
Cpl = (X2bar - LSL) / (3 * sigma)
Cpk = min(Cpu, Cpl)
print(f"X-double-bar = {X2bar:.4f} mm, R-bar = {Rbar:.4f} mm")
print(f"sigma (R-bar/d2) = {sigma:.5f} mm, 6 sigma = {6*sigma:.4f}")
print(f"Overall std dev of all readings = {data.std(ddof=1):.5f}")
print(f"Cp = {Cp:.3f}, Cpu = {Cpu:.3f}, Cpl = {Cpl:.3f}, "
f"Cpk = {Cpk:.3f}")
if Cpk >= 1.33:
print("Process is CAPABLE")
elif Cpk >= 1.0:
print("Process is MARGINALLY capable")
else:
print("Process is NOT capable - reduce variation / re-centre")fig, ax = plt.subplots(1, 2, figsize=(7.5, 3))
allx = data.flatten()
ax[0].hist(allx, bins=8, color="#9DC3E6", edgecolor="k")
for v, l in [(LSL, "LSL"), (USL, "USL")]:
ax[0].axvline(v, color="r", ls="--")
ax[0].text(v, 11.5, l, ha="center", color="r")
ax[0].axvline(X2bar, color="g")
ax[0].set_title(f"Histogram Cp={Cp:.2f} Cpk={Cpk:.2f}", fontsize=9)
ax[0].set_xlabel("Diameter (mm)"); ax[0].set_ylim(0, 13)
t = np.linspace(24.93, 25.07, 200)
pdf = np.exp(-(t - X2bar)**2 / (2 * sigma**2))
pdf /= sigma * np.sqrt(2 * np.pi)
ax[1].plot(t, pdf, "navy"); ax[1].fill_between(t, pdf, alpha=.3)
for v in (LSL, USL): ax[1].axvline(v, color="r", ls="--")
ax[1].axvline(X2bar - 3*sigma, color="gray", ls=":")
ax[1].axvline(X2bar + 3*sigma, color="gray", ls=":")
ax[1].set_title("Process spread (±3σ) vs specification", fontsize=9)
ax[1].set_yticks([]); ax[1].set_xlabel("Diameter (mm)")
plt.tight_layout(); plt.show()X-double-bar = 25.0046 mm, R-bar = 0.0420 mm sigma (R-bar/d2) = 0.01806 mm, 6 sigma = 0.1083 Overall std dev of all readings = 0.01593 Cp = 0.923, Cpu = 0.838, Cpl = 1.008, Cpk = 0.838 Process is NOT capable - reduce variation / re-centre
Program 11.3 – Generate X-bar and R charts and identify in-control and out-of-control points
The data below are 15 samples of a bore diameter (n = 4). Sample 9 was machined after a tool change with a wrong offset.
# Program 11.3: Identify in-control and out-of-control points
import numpy as np
import matplotlib.pyplot as plt
data = np.array([
[40.01, 39.99, 40.00, 40.02], [40.00, 40.01, 39.98, 40.00],
[39.99, 40.00, 40.01, 40.01], [40.02, 40.00, 39.99, 40.00],
[40.00, 39.98, 40.01, 39.99], [40.01, 40.02, 40.00, 39.99],
[39.98, 40.00, 40.00, 40.01], [40.00, 40.01, 40.02, 40.00],
[40.05, 40.06, 40.04, 40.07], [40.00, 39.99, 40.01, 40.00],
[40.01, 40.00, 39.98, 40.01], [39.99, 40.01, 40.00, 40.00],
[40.00, 40.04, 39.95, 40.01], [40.01, 39.99, 40.00, 40.02],
[40.00, 40.00, 40.01, 39.99]])
n = data.shape[1]
A2, D3, D4 = 0.729, 0, 2.282 # n = 4
xbar = data.mean(axis=1); R = np.ptp(data, axis=1)
X2, Rb = xbar.mean(), R.mean()
lim = {"X": (X2 - A2*Rb, X2, X2 + A2*Rb), "R": (D3*Rb, Rb, D4*Rb)}
print("X-bar chart LCL/CL/UCL:",
[round(float(v), 4) for v in lim["X"]])
print("R chart LCL/CL/UCL:",
[round(float(v), 4) for v in lim["R"]])
s = np.arange(1, len(xbar) + 1)
oc_x = s[(xbar > lim["X"][2]) | (xbar < lim["X"][0])]
oc_r = s[(R > lim["R"][2]) | (R < lim["R"][0])]
print("Out of control on X-bar chart:", [int(i) for i in oc_x])
print("Out of control on R chart :", [int(i) for i in oc_r])
print("In-control samples:", [int(i) for i in s
if i not in oc_x and i not in oc_r])fig, axs = plt.subplots(2, 1, figsize=(6.5, 5.5), sharex=True)
for ax, y, key, oc, nm in [(axs[0], xbar, "X", oc_x, "X-bar"),
(axs[1], R, "R", oc_r, "R")]:
lo, cl, hi = lim[key]
ax.plot(s, y, "o-", color="navy")
ax.plot(oc, y[oc - 1], "o", ms=11, mfc="none", mec="red", mew=2)
ax.axhline(hi, color="r", ls="--")
ax.axhline(lo, color="r", ls="--")
ax.axhline(cl, color="g"); ax.set_title(f"{nm} chart (red = out)")
ax.grid(alpha=.3)
axs[1].set_xlabel("Sample number"); axs[1].set_xticks(s)
plt.tight_layout(); plt.show()X-bar chart LCL/CL/UCL: [39.9825, 40.0048, 40.0272] R chart LCL/CL/UCL: [0.0, 0.0307, 0.07] Out of control on X-bar chart: [9] Out of control on R chart : [13] In-control samples: [1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 14, 15]
Interpretation: sample 9 is above the UCL on the X-bar chart (process mean shifted – wrong tool offset) and sample 13 is above the UCL on the R chart (sudden increase in spread – possibly a loose work-piece). These samples should be investigated, the causes removed, and the limits recalculated after excluding them ("revised control limits").
Program 11.4 – Revised control limits after removing assignable causes
import numpy as np
xbar = np.array([40.005, 39.9975, 40.0025, 40.0025, 39.995,
40.005, 39.9975, 40.0075, 40.055, 40.0,
40.0, 40.0, 40.0, 40.005, 40.0])
R = np.array([.03, .03, .02, .03, .03, .03, .03, .02, .03,
.02, .03, .02, .09, .03, .02])
keep = np.ones(15, bool); keep[[8, 12]] = False # drop 9 and 13
X2, Rb = xbar[keep].mean(), R[keep].mean()
print(f"Revised X-double-bar = {X2:.4f}, R-bar = {Rb:.4f}")
print(f"Revised X-bar limits: {X2-0.729*Rb:.4f} to {X2+0.729*Rb:.4f}")
print(f"Revised R chart UCL = {2.282*Rb:.4f}")Revised X-double-bar = 40.0013, R-bar = 0.0262 Revised X-bar limits: 39.9823 to 40.0204 Revised R chart UCL = 0.0597
Program 11.5 – Capability calculator function
def capability(mean, sigma, LSL, USL):
cp = (USL - LSL) / (6 * sigma)
cpk = min(USL - mean, mean - LSL) / (3 * sigma)
return round(cp, 3), round(cpk, 3)m = float(input("Process mean : "))
s = float(input("Process sigma : "))
lsl = float(input("LSL : "))
usl = float(input("USL : "))
cp, cpk = capability(m, s, lsl, usl)
print("Cp =", cp, " Cpk =", cpk)
print("Capable" if cpk >= 1.33 else "Not capable")Process mean : 50.00 Process sigma : 0.012 LSL : 49.97 USL : 50.03 Cp = 0.833 Cpk = 0.833 Not capable
11.5 Solved Exam Questions
Q1. Define standard deviation. Calculate the mean, variance and standard deviation of 25.02, 24.98, 25.01, 25.00, 24.99. (6 marks)
Answer: Standard deviation is the root-mean-square deviation of values from their mean; it measures spread in the same unit as the data. See Worked Example 11.1: mean 25.00 mm, s² = 0.00025 mm², s = 0.0158 mm.
Q2. Define Cp and Cpk. How are they interpreted? (6 marks)
Answer: Cp = (USL − LSL)/6σ compares tolerance with process spread. Cpk = min[(USL − μ)/3σ, (μ − LSL)/3σ] also accounts for centring. Cp, Cpk < 1: not capable; 1–1.33: marginal; ≥ 1.33: capable. Cpk < Cp indicates an off-centre process.
Q3. A process has mean 50.02 mm and σ = 0.01 mm. Specification is 50.00 ± 0.05 mm. Find Cp and Cpk and comment. (6 marks)
Answer: Cp = 0.10 / (6 × 0.01) = 1.667. Cpu = (50.05 − 50.02)/0.03 = 1.0; Cpl = (50.02 − 49.95)/0.03 = 2.333; Cpk = 1.0. The process has enough potential capability (Cp = 1.67) but is off-centre towards USL, so actual capability is only 1.0. Re-centring at 50.00 would raise Cpk to 1.67.
Q4. Differentiate between control limits and specification limits. (4 marks)
Answer: Control limits are calculated from process data (±3σ of the statistic) and show what the process does; specification limits are given by the designer or customer and show what is required. Control limits apply to sample statistics (Xm, R); specification limits apply to individual parts.
Q5. Write a Python program to calculate mean, variance and standard deviation and generate X-bar and R control charts from sample data. (20 marks, SEE)
Answer: Programs 11.1 and 11.3 together.
11.6 Multiple Choice Questions
1. Cp is defined as a) (USL − LSL)/3σ b) (USL − LSL)/6σ c) 6σ/(USL − LSL) d) (USL − μ)/3σ
Answer: (b) Reason: Cp is tolerance divided by the natural process spread 6σ.
2. Cpk considers a) only spread b) only centring c) both spread and centring d) neither
Answer: (c) Reason: It uses the distance of the mean from the nearer specification limit.
3. For a perfectly centred process a) Cpk > Cp b) Cpk = Cp c)Cpk=0 d)Cp=0
Answer: (b) Reason: Both one-sided indices are equal, so the minimum equals Cp.
4. A process with Cp = 1.5 and Cpk = 0.8 is a) capable and centred b) potentially capable but off-centre c) incapable due to spread d) perfect
Answer: (b) Reason: High Cp shows small spread; low Cpk shows poor centring.
5. The commonly accepted minimum Cpk in industry is a) 0.5 b) 1.0 c) 1.33 d) 3.0
Answer: (c) Reason: 1.33 corresponds to a ±4σ margin within specification.
6. The process standard deviation estimated from R^ is a)Rm ×d₂ b)Rm /d₂ c) Rm / n d) A₂Rm
Answer: (b) Reason: σ(cid:5) = R[ /d₂ with d₂ depending on sample size.
7. Sample variance uses the divisor a)n b)n−1 c)n+1 d)n²
Answer: (b) Reason: Dividing by n − 1 (Bessel's correction) gives an unbiased estimate.
8. In NumPy, the sample standard deviation is computed by a) np.std(a) b) np.std(a, ddof=1) c) np.var(a) d) np.mean(a)
Answer: (b) Reason: ddof=1 changes the divisor from n to n − 1.
9. Specification limits are decided by a) the process b) the designer or customer c) the control chart d) the machine operator only
Answer: (b) Reason: They express functional requirements; control limits come from data.
10. If USL = 10.06, LSL = 9.94 and σ = 0.02, Cp is a) 1.0 b) 0.5 c) 2.0 d) 1.33
Answer: (a) Reason: Cp = 0.12 / (6 × 0.02) = 0.12/0.12 = 1.0.
11. A process can be a) in control but not capable b) capable only if out of control c) never in control d) capable only if Cp < 1
Answer: (a) Reason: Stability (control) and capability (fit to tolerance) are separate properties.
12. A negative Cpk indicates a) excellent process b) process mean lies outside the specification limits c) zero variation d) calculation is impossible
Answer: (b) Reason: One of the distances (USL − μ) or (μ − LSL) is negative.
13. statistics.pstdev() returns a) sample standard deviation b) population standard deviation c) variance d) median
Answer: (b) Reason: The "p" stands for population (divisor n).
14. The coefficient of variation is a)s×xn b)(s/xn)×100% c)xn/s d)R/xn
Answer: (b) Reason: It expresses spread relative to the mean, without units.
11.7 Exercises
1. A turning process has X̿(cid:13) = 30.012 mm and Rm = 0.035 mm with n = 4. Specification 30.00 ± 0.06 mm. Find σ, Cp and Cpk.
2. Write a program that reads samples from a CSV file and reports Cp and Cpk.
3. Explain why Cpk can never exceed Cp.
4. Generate 25 samples of random data with NumPy and plot X-bar and R charts.
5. Calculate the variance and standard deviation of the hardness values 210, 205, 215, 200, 220 manually and verify with Python.