← Programming Concepts in Mechanical Engineering (25ME34I)
Statistical Quality Control: Basic Concepts and Control Charts
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:
• Explain the role of quality control in modern manufacturing and the advantages of Python for quality analysis.
• Define SQC, list its benefits and distinguish process control from product control.
• Explain chance and assignable causes of variation.
• Construct X-bar and R charts for variables, the p-chart for fraction defective and the c-chart for defects per unit.
• Write Python programs to calculate sample averages and ranges and plot control charts with Matplotlib.
10.1 Quality and Quality Control
Quality is "fitness for use" (J. M. Juran) or "conformance to requirements" (P. B. Crosby). For a manufactured part, quality means that its dimensions, finish, strength and performance meet the specification consistently. ISO 9000 defines quality as the degree to which a set of inherent characteristics fulfils requirements.
Quality control (QC) is the set of techniques and activities used to measure actual quality, compare it with the standard and act on the difference.
Role of quality control in modern manufacturing
• Customer satisfaction: consistent products build reputation and repeat orders.
• Reduced cost: less scrap, rework, warranty claims and inspection cost (cost of poor quality).
• Interchangeability: parts made in different shifts and plants assemble without fitting – essential for mass production.
• Process improvement: data reveals machine wear, tool problems and operator errors early.
• Competitiveness and certification: ISO 9001, IATF 16949 (automotive) require statistical process control.
• Safety and reliability: critical parts (brakes, pressure vessels) must not fail.
• Higher productivity: fewer stoppages and defects mean more good output per hour.
Advantages of Python in quality analysis and control
• Automatic calculation of means, ranges, standard deviation, control limits, Cp and Cpk without manual errors.
• Visualisation: control charts, histograms, Pareto charts and fishbone diagrams with Matplotlib.
• Handles large data from CMMs, gauges and sensors; reads Excel/CSV through pandas.
• Real-time monitoring: with Raspberry Pi sensors, a program can plot control charts live and raise alarms.
• Statistical power: SciPy provides t-tests, ANOVA and distributions; statsmodels supports DOE.
• Free and repeatable: no licence cost (unlike commercial SPC software), and the same script can be reused every day.
10.2 Statistical Quality Control (SQC)
Statistical Quality Control is the application of statistical methods to monitor, control and improve the quality of products and processes. Instead of inspecting every item, small samples are measured and statistics are used to judge the whole process or lot. SQC was pioneered by Dr. Walter A. Shewhart at Bell Laboratories (control chart, 1924) and spread worldwide by W. Edwards Deming.
Benefits of SQC
1. Detects trouble early, before defective parts are produced in large numbers.
2. Reduces inspection cost because only samples are checked.
3. Reduces scrap and rework, so cost of production falls.
4. Distinguishes natural variation from real problems, avoiding unnecessary machine adjustments.
5. Provides a factual basis for decisions on process capability, tolerances and machine selection.
6. Improves the quality consciousness of operators.
7. Helps in vendor evaluation and acceptance of incoming material.
8. Builds customer confidence through documented evidence of quality.
Variation and its causes
No two parts are exactly alike. Variation in a quality characteristic arises from two kinds of causes:
| Chance (common / random) causes | Assignable (special) causes |
|---|---|
| Many small, unavoidable causes inherent in the process | Few, large, identifiable causes |
| Examples: slight vibration, minor material variation, ambient temperature | Examples: tool wear, wrong setting, new untrained operator, bad batch of material |
| Produce a stable, predictable (normal) pattern | Produce unusual patterns or points outside limits |
| Process is "in statistical control" | Process is "out of control" |
| Reduced only by changing the process (management action) | Can be found and removed by the operator/supervisor |
Fig. 10.1 Normal distribution: 99.73 % of values lie within ±3σ
When only chance causes act, measurements usually follow the normal distribution. About 68.27 % of values lie within μ ± 1σ, 95.45 % within μ ± 2σ and 99.73 % within μ ± 3σ. This is why control limits are set at ±3σ: a point outside them has only a 0.27 % chance of occurring naturally, so it almost certainly signals an assignable cause.
10.3 Techniques of SQC: Process Control and Product Control
| Process control | Product control (acceptance sampling) |
|---|---|
| Controls quality during manufacture | Decides acceptance after manufacture (lot inspection) |
| Tool: control charts | Tool: sampling plans (single, double, sequential), OC curves |
| Aim: prevent defects | Aim: accept good lots, reject bad lots |
| Used by the producer on the shop floor | Used by the purchaser/inspection department, also for incoming goods |
| Example: X-bar and R chart for shaft diameter on a lathe | Example: inspect 50 bolts from a lot of 2000; accept if ≤ 2 defective |
10.4 Control Charts
A control chart is a graph of a sample statistic (mean, range, fraction defective, number of defects) plotted in time order, with three horizontal lines:
• Central Line (CL) – the average value of the statistic.
• Upper Control Limit (UCL) = CL + 3σ_statistic.
• Lower Control Limit (LCL) = CL − 3σ_statistic (taken as 0 if negative for p and c charts).
Fig. 10.2 Structure of a Shewhart control chart
Interpretation – the process is out of control if:
1. One or more points fall outside the control limits.
2. A run of 7 or more consecutive points lies on one side of the central line.
3. A trend of 6–7 points steadily rising or falling (e.g. tool wear).
4. Cycles or periodic patterns (shift changes, temperature cycles).
5. Points hugging the control limits or the central line (mixing of data, wrong limits).
Types of control charts
| Chart | Type of data | Statistic plotted | Typical use |
|---|---|---|---|
| X-bar chart | Variables (measurements) | Sample mean | Centre (setting) of process – diameter, length, weight |
| R chart | Variables | Sample range | Spread (dispersion) of process |
| σ (s) chart | Variables | Sample standard deviation | Spread when sample size > 10 |
| p chart | Attributes | Fraction defective | Proportion of defective items (go/no-go gauge) |
| np chart | Attributes | Number defective | Constant sample size |
| c chart | Attributes | Number of defects per unit | Defects on a casting, cloth, painted panel |
| u chart | Attributes | Defects per unit, variable size | Defects per m² |
Variables are measured on a continuous scale (25.02 mm). Attributes are counted (good/bad, number of blowholes).
10.5 X-bar and R Charts
Take k samples (subgroups), each of n items (usually n = 4 or 5), at regular intervals.
| Quantity | Formula |
|---|---|
| Sample average | X m= (x₁ + x₂ + ... + xₙ)/n |
| Sample range | R = x_max − x_min |
| Grand average (average of averages) | X̿(cid:13) = ΣX m/ k |
| Average range | Rm = ΣR / k |
| X-bar chart limits | CL = X̿(cid:13), UCL = X̿(cid:13) + A₂Rm, LCL = X̿(cid:13) − A₂Rm |
| R chart limits | CL = Rm, UCL = D₄Rm, LCL = D₃Rm |
| Process standard deviation (estimate) | σ = Rm / d₂ |
Control chart constants
| n | A₂ | D₃ | D₄ | d₂ |
|---|---|---|---|---|
| 2 | 1.880 | 0 | 3.267 | 1.128 |
| 3 | 1.023 | 0 | 2.574 | 1.693 |
| 4 | 0.729 | 0 | 2.282 | 2.059 |
| 5 | 0.577 | 0 | 2.114 | 2.326 |
| 6 | 0.483 | 0 | 2.004 | 2.534 |
| 7 | 0.419 | 0.076 | 1.924 | 2.704 |
| 8 | 0.373 | 0.136 | 1.864 | 2.847 |
| 10 | 0.308 | 0.223 | 1.777 | 3.078 |
Worked Example 10.1 (manual calculation)
Ten samples of five shafts each were measured (nominal diameter 25.00 mm, specification 25.00 ± 0.05 mm).
| Sample | x₁ | x₂ | x₃ | x₄ | x₅ | X^ | R |
|---|---|---|---|---|---|---|---|
| 1 | 25.02 | 24.98 | 25.01 | 25.00 | 24.99 | 25.000 | 0.04 |
| 2 | 25.01 | 25.03 | 24.99 | 25.00 | 25.02 | 25.010 | 0.04 |
| 3 | 24.98 | 25.00 | 25.01 | 24.99 | 25.00 | 24.996 | 0.03 |
| 4 | 25.03 | 25.01 | 25.02 | 25.00 | 24.99 | 25.010 | 0.04 |
| 5 | 25.00 | 24.97 | 25.01 | 25.02 | 24.99 | 24.998 | 0.05 |
| 6 | 25.02 | 25.01 | 25.00 | 24.98 | 25.03 | 25.008 | 0.05 |
| 7 | 24.99 | 25.00 | 25.02 | 25.01 | 25.00 | 25.004 | 0.03 |
| 8 | 25.01 | 24.98 | 25.00 | 25.03 | 25.02 | 25.008 | 0.05 |
| 9 | 25.00 | 25.02 | 24.99 | 25.01 | 24.98 | 25.000 | 0.04 |
| 10 | 25.02 | 25.00 | 25.01 | 24.99 | 25.04 | 25.012 | 0.05 |
| Total | 250.046 | 0.42 |
• X̿(cid:13) = 250.046 / 10 = 25.0046 mm; Rm = 0.42 / 10 = 0.042 mm
• For n = 5: A₂ = 0.577, D₃ = 0, D₄ = 2.114
• X-bar chart: UCL = 25.0046 + 0.577 × 0.042 = 25.0288 mm; LCL = 25.0046 − 0.0242 = 24.9804 mm
• R chart: UCL = 2.114 × 0.042 = 0.0888 mm; LCL = 0 × 0.042 = 0
All sample averages lie between 24.9804 and 25.0288, and all ranges lie below 0.0888. Hence the process is in statistical control.
10.6 Charts for Attributes
p-chart (fraction defective)
For k samples of size n with d defectives in each: p = d/n.
• CL=pn=Σd/(k×n)
• UCL = pn + 3√[pn(1 − pn)/n], LCL = pn − 3√[pn(1 − pn)/n] (0 if negative)
Example 10.2. Ten lots of 100 bushes were inspected with a go/no-go gauge; defectives: 4, 6, 3, 5, 8, 2, 5, 7, 4, 6 (total 50). pn = 50/1000 = 0.05; σp = √(0.05 × 0.95/100) = 0.02179; UCL = 0.05 + 3 × 0.02179 = 0.1154; LCL = 0.05 − 0.0654 < 0, so LCL = 0. The largest fraction defective is 0.08, so the process is in control.
c-chart (number of defects per unit)
When each unit may contain several defects (blowholes in a casting, surface flaws on a sheet), the number of defects c follows the Poisson distribution.
• CL=cn=Σc/k
• UCL = cn + 3√cn, LCL = cn − 3√cn (0 if negative)
Example 10.3. Defects found on 12 castings: 3, 5, 2, 4, 6, 1, 3, 4, 11, 2, 3, 4 (total 48). cn = 48/12 = 4; UCL = 4 + 3√4 = 10; LCL = 4 − 6 < 0 → 0. Casting 9 has 11 defects, above the UCL – an assignable cause (for example, a wet sand batch or low pouring temperature) must be investigated.
10.7 Practice Programs (Week 10)
Program 10.1 – Calculate X^ and R and plot X-bar and R charts using Matplotlib
Algorithm
1. Start; store the sample data as a list of lists (k samples × n readings).
2. For each sample compute Xm = sum/n and R = max − min.
3. Compute X̿(cid:13) and Rm .
4. Read A₂, D₃, D₄ for n from the constants table; compute the control limits.
5. Print a results table and identify any points outside limits.
6. Plot the X-bar chart and R chart with CL, UCL and LCL lines.
7. Stop.
# Program 10.1: X-bar and R charts import matplotlib.pyplot as plt
data = [[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]]
const = {2: (1.880, 0, 3.267), 3: (1.023, 0, 2.574),
4: (0.729, 0, 2.282), 5: (0.577, 0, 2.114)}n = len(data[0]) xbar = [sum(s) / n for s in data] R = [max(s) - min(s) for s in data] X2bar = sum(xbar) / len(xbar) Rbar = sum(R) / len(R) A2, D3, D4 = const[n] UCLx, LCLx = X2bar + A2 * Rbar, X2bar - A2 * Rbar UCLr, LCLr = D4 * Rbar, D3 * Rbar
print("Sample X-bar R")
for i, (x, r) in enumerate(zip(xbar, R), 1):
print(f"{i:4d} {x:8.3f} {r:6.3f}")
print(f"X-double-bar = {X2bar:.4f} R-bar = {Rbar:.4f}")
print(f"X-bar chart: UCL = {UCLx:.4f}, CL = {X2bar:.4f}, "
f"LCL = {LCLx:.4f}")
print(f"R chart : UCL = {UCLr:.4f}, CL = {Rbar:.4f}, "
f"LCL = {LCLr:.4f}")
out = [i + 1 for i, x in enumerate(xbar) if not LCLx <= x <= UCLx]
out += [i + 1 for i, r in enumerate(R) if not LCLr <= r <= UCLr]
print("Process IN CONTROL" if not out else f"Out of control: {out}")k = range(1, len(data) + 1)
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6.5, 6))
ax1.plot(k, xbar, "o-", color="navy")
for v, c, t in [(UCLx, "r", "UCL"), (X2bar, "g", "CL"),
(LCLx, "r", "LCL")]:
ax1.axhline(v, color=c, ls="--" if t != "CL" else "-")
ax1.text(10.3, v, f"{t}={v:.4f}", va="center", fontsize=8)
ax1.set_title("X-bar Chart"); ax1.set_ylabel("Sample mean (mm)")
ax2.plot(k, R, "s-", color="darkorange")
for v, c, t in [(UCLr, "r", "UCL"), (Rbar, "g", "CL"),
(LCLr, "r", "LCL")]:
ax2.axhline(v, color=c, ls="--" if t != "CL" else "-")
ax2.text(10.3, v, f"{t}={v:.4f}", va="center", fontsize=8)
ax2.set_title("R Chart"); ax2.set_ylabel("Sample range (mm)")
ax2.set_xlabel("Sample number")
for a in (ax1, ax2):
a.set_xticks(list(k)); a.grid(alpha=0.3); a.set_xlim(0.5, 12.5)
plt.tight_layout(); plt.show()Sample X-bar R 1 25.000 0.040 2 25.010 0.040 3 24.996 0.030 4 25.010 0.040 5 24.998 0.050 6 25.008 0.050 7 25.004 0.030 8 25.008 0.050 9 25.000 0.040 10 25.012 0.050 X-double-bar = 25.0046 R-bar = 0.0420 X-bar chart: UCL = 25.0288, CL = 25.0046, LCL = 24.9804 R chart : UCL = 0.0888, CL = 0.0420, LCL = 0.0000 Process IN CONTROL
Explanation: the program reproduces the manual results of Example 10.1. List comprehensions compute all Xm and R values; a dictionary stores the constants; axhline() draws the limit lines; subplots(2, 1) places the two charts one below the other, which is the standard way of presenting X-bar and R charts.
Program 10.2 – Out-of-control detection with a tool-wear trend
# Program 10.2: X-bar chart with an assignable cause
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(5)
n, k = 5, 20
data = np.random.normal(50.0, 0.02, (k, n))
data[14:] += np.linspace(0.01, 0.06, 6)[:, None] # tool wear
xbar, R = data.mean(axis=1), np.ptp(data, axis=1)
X2, Rb = xbar[:12].mean(), R[:12].mean() # limits from first 12
UCL, LCL = X2 + 0.577 * Rb, X2 - 0.577 * Rb
bad = np.where((xbar > UCL) | (xbar < LCL))[0] + 1
print(f"UCL = {UCL:.4f} CL = {X2:.4f} LCL = {LCL:.4f}")
print("Samples out of control:", [int(i) for i in bad])
plt.figure(figsize=(6.5, 3))
plt.plot(range(1, k + 1), xbar, "o-")
plt.plot(bad, xbar[bad - 1], "ro", ms=10, mfc="none", mew=2)
for v in (UCL, LCL): plt.axhline(v, color="r", ls="--")
plt.axhline(X2, color="g")
plt.title("X-bar chart showing tool-wear trend")
plt.xlabel("Sample"); plt.ylabel("Mean length (mm)")
plt.grid(alpha=.3); plt.tight_layout(); plt.show()UCL = 50.0263 CL = 50.0027 LCL = 49.9790 Samples out of control: [17, 18, 19, 20]
The upward trend from sample 15 onwards shows gradual tool wear; the operator should re-set or change the tool.
Program 10.3 – p-chart for fraction defective
# Program 10.3: p-chart
import math
import matplotlib.pyplot as plt
n = 100
defectives = [4, 6, 3, 5, 8, 2, 5, 7, 4, 6]
p = [d / n for d in defectives]
pbar = sum(defectives) / (n * len(defectives))
sig = math.sqrt(pbar * (1 - pbar) / n)
UCL = pbar + 3 * sig
LCL = max(0, pbar - 3 * sig)
print(f"p-bar = {pbar:.4f}, UCL = {UCL:.4f}, LCL = {LCL:.4f}")
print("Fractions:", p)
print("In control" if all(LCL <= x <= UCL for x in p)
else "Out of control")
plt.figure(figsize=(6.5, 3))
plt.plot(range(1, 11), p, "o-", color="purple")
plt.axhline(UCL, color="r", ls="--", label=f"UCL {UCL:.3f}")
plt.axhline(pbar, color="g", label=f"CL {pbar:.3f}")
plt.axhline(LCL, color="r", ls=":", label=f"LCL {LCL:.3f}")
plt.title("p-chart (fraction defective)"); plt.xlabel("Lot")
plt.ylabel("Fraction defective"); plt.legend(fontsize=8)
plt.grid(alpha=.3); plt.tight_layout(); plt.show()p-bar = 0.0500, UCL = 0.1154, LCL = 0.0000 Fractions: [0.04, 0.06, 0.03, 0.05, 0.08, 0.02, 0.05, 0.07, 0.04, 0.06] In control
Program 10.4 – c-chart for number of defects per casting
# Program 10.4: c-chart
import math
import matplotlib.pyplot as plt
c = [3, 5, 2, 4, 6, 1, 3, 4, 11, 2, 3, 4]
cbar = sum(c) / len(c)
UCL = cbar + 3 * math.sqrt(cbar)
LCL = max(0, cbar - 3 * math.sqrt(cbar))
print(f"c-bar = {cbar}, UCL = {UCL}, LCL = {LCL}")
for i, x in enumerate(c, 1):
if x > UCL or x < LCL:
print(f"Casting {i} with {x} defects is OUT of control")
plt.figure(figsize=(6.5, 3))
plt.plot(range(1, len(c) + 1), c, "o-", color="teal")
plt.axhline(UCL, color="r", ls="--"); plt.axhline(cbar, color="g")
plt.axhline(LCL, color="r", ls=":")
plt.title("c-chart (defects per casting)")
plt.xlabel("Casting number"); plt.ylabel("Defects")
plt.grid(alpha=.3); plt.tight_layout(); plt.show()c-bar = 4.0, UCL = 10.0, LCL = 0 Casting 9 with 11 defects is OUT of control
10.8 Solved Exam Questions
Q1. Explain the role of quality control in modern manufacturing. (6 marks)
Answer: Refer Section 10.1: customer satisfaction, reduced cost of poor quality, interchangeability for mass production, early detection of process problems, certification requirements (ISO 9001, IATF 16949), safety and reliability, productivity.
Q2. What is SQC? State its benefits. (6 marks)
Answer: SQC is the use of statistical methods, based on samples, to monitor and control quality. Benefits are listed in Section 10.2: early detection, low inspection cost, less scrap, separating chance and assignable causes, factual decisions, quality consciousness, vendor evaluation and customer confidence.
Q3. Differentiate between process control and product control. (5 marks)
Answer: See the table in Section 10.3.
Q4. Distinguish chance causes and assignable causes of variation. (5 marks)
Answer: See the table in Section 10.2.
Q5. What is a control chart? Explain its construction and interpretation. (7 marks)
Answer: A control chart is a time-ordered plot of a sample statistic with CL, UCL and LCL (±3σ). Construction: collect samples, compute statistic, compute limits from formulas, plot points and lines. Interpretation: points outside limits, runs of 7 on one side, trends, cycles indicate assignable causes (Section 10.4).
Q6. Write the formulas for X-bar, R, p and c charts. (6 marks)
Answer: X-bar: X̿(cid:13) ± A₂Rm ; R: D₃Rm to D₄Rm ; p: pn ± 3√(pn(1−pn)/n); c: cn ± 3√cn.
Q7. Develop a Python program to calculate the sample average and range for given data and plot X-bar and R charts using Matplotlib. (20 marks, SEE)
Answer: Program 10.1 with algorithm, output and the charts.
10.9 Multiple Choice Questions
1. The control chart was developed by a) Deming b) Walter Shewhart c) Juran d) Ishikawa
Answer: (b) Reason: Shewhart introduced the control chart at Bell Laboratories in 1924.
2. Control limits are usually set at a) ±1σ b) ±2σ c) ±3σ d) ±6σ
Answer: (c) Reason: ±3σ covers 99.73 % of values under chance causes alone.
3. The X-bar chart monitors the a) spread of the process b) central tendency (setting) of the process c) number of defects d) fraction defective
Answer: (b) Reason: Sample means show whether the process average shifts.
4. The R chart monitors a) process average b) process variability (dispersion) c) defects per unit d) cost
Answer: (b) Reason: Range measures spread within each sample.
5. For n = 5, the value of A₂ is a) 0.729 b) 0.577 c) 1.023 d) 2.114
Answer: (b) Reason: From the table of control chart constants.
6. The UCL of an R chart is a) A₂Rm b) D₃Rm c) D₄Rm d) d₂Rm
Answer: (c) Reason: UCL_R = D₄R[ and LCL_R = D₃R[ .
7. A p-chart is used for a) variables b) fraction defective c) defects per unit d) range
Answer: (b) Reason: p is the proportion of defective items in a sample.
8. A c-chart plots a) sample mean b) number of defects per unit c) standard deviation d) fraction defective
Answer: (b) Reason: c is the count of defects on each inspected unit (Poisson data).
9. For c_ = 9, the UCL of the c-chart is a) 12 b) 18 c) 27 d) 15
Answer: (b) Reason: UCL = 9 + 3√9 = 9 + 9 = 18.
10. Tool wear in a lathe usually appears on an X-bar chart as a) a single point outside limits b) a steady trend c) random pattern d) no effect
Answer: (b) Reason: Gradual wear changes the dimension progressively, producing a trend.
11. Acceptance sampling is a technique of a) process control b) product control c) design control d) cost control
Answer: (b) Reason: It decides acceptance or rejection of finished lots.
12. Chance causes of variation are a) large and few b) small, many and inherent in the process c) easily removed by the operator d) always due to the operator
Answer: (b) Reason: Common causes are natural, random and built into the system.
13. Diameter of a shaft is an example of a) attribute data b) variable data c) defect data d) qualitative data
Answer: (b) Reason: It is measured on a continuous scale.
14. When LCL of a p-chart is negative, it is taken as a) negative value b) zero c) one d) pn
Answer: (b) Reason: A fraction defective cannot be negative.
15. The percentage of values within ±3σ of a normal distribution is a) 68.27 % b) 95.45 % c) 99.73 % d) 100 %
Answer: (c) Reason: Only 0.27 % fall outside under natural variation.
16. In Matplotlib, a horizontal line for UCL is drawn with a) plt.vline() b) plt.axhline() c) plt.bar() d) plt.pie()
Answer: (b) Reason: axhline(y) draws a line across the axes at height y.
10.10 Exercises
1. The means and ranges of 8 samples of 4 pins are given. Compute the control limits for X-bar and R charts and comment.
2. Modify Program 10.1 to read the data from a CSV file exported from Excel.
3. In 15 lots of 200 items the number of defectives were recorded; write a Python program for the p-chart.
4. Explain the patterns on a control chart that indicate an out-of-control process.
5. Write a program for an np-chart: CL = npn, limits npn ± 3√(npn(1 − pn)).