Why This Matters
A linear equation is the simplest kind of equation you can solve: it has one variable raised to the first power and its graph is a straight line. When you write 3x + 5 = 20, you are asking "what number, when tripled and increased by 5, gives 20?" Solving means isolating x on one side using inverse operations -- subtracting undoes adding, dividing undoes multiplying.
The concept of slope measures how steeply a line rises or falls. A line with slope 2 rises 2 units for every 1 unit you move right. The y-intercept is where the line crosses the y-axis, the starting value when x is zero. Together, slope and y-intercept define the equation y = mx + b, the most important formula in introductory algebra.
Linear equations appear everywhere in programming. Setting a variable equal to a computed value, solving for an unknown in a formula, and reasoning about rates of change all rely on the same skill: systematically undoing operations to find the value of the unknown.
Define Terms
Visual Model
The full process at a glance. Click Start tour to walk through each step.
Solving a linear equation by applying inverse operations step by step to isolate the variable.
Code Example
// Solve ax + b = c for x
function solveLinear(a, b, c) {
if (a === 0) {
return b === c ? "infinite solutions" : "no solution";
}
return (c - b) / a;
}
// 3x + 5 = 20 => x = 5
console.log("3x + 5 = 20:", solveLinear(3, 5, 20)); // 5
// -2x + 10 = 4 => x = 3
console.log("-2x + 10 = 4:", solveLinear(-2, 10, 4)); // 3
// 0.5x - 3 = 7 => x = 20
console.log("0.5x - 3 = 7:", solveLinear(0.5, -3, 7)); // 20
// Slope and y-intercept from y = mx + b
function lineInfo(m, b) {
console.log(`Slope: ${m}`);
console.log(`Y-intercept: ${b}`);
console.log(`At x=0: y=${m * 0 + b}`);
console.log(`At x=1: y=${m * 1 + b}`);
console.log(`At x=5: y=${m * 5 + b}`);
}
console.log("\nLine y = 2x + 3:");
lineInfo(2, 3);
// Find x-intercept (where y = 0)
function xIntercept(m, b) {
return -b / m;
}
console.log("\nX-intercept of y = 2x + 3:", xIntercept(2, 3)); // -1.5Interactive Experiment
Try these exercises:
- Solve 5x + 10 = 35 by hand. Then verify using the solveLinear function.
- What happens when a = 0 and b = c? What about when a = 0 and b is not equal to c? Why?
- Create a line with slope 3 and y-intercept -2. Compute y for x = -3, 0, 1, 4, 10.
- Find both the x-intercept and y-intercept of the line y = -4x + 8. What shape do they form?
- Solve 2(x + 3) = 14. First distribute, then solve. Verify by substitution.
Quick Quiz
Coding Challenge
Write a function called `solveForX` that takes three numbers a, b, and c, and returns the solution to ax + b = c. If a is 0 and b equals c, return the string 'infinite'. If a is 0 and b does not equal c, return the string 'none'. Otherwise return the numeric solution.
Real-World Usage
Linear equations underpin countless practical computations:
- Unit conversion: Converting Celsius to Fahrenheit uses F = 1.8C + 32, a linear equation you can solve in either direction.
- Business break-even analysis: Revenue = Cost becomes price * units = fixedCost + variableCost * units, a linear equation solved for units.
- Physics motion: Distance = rate * time (d = rt) is a linear equation. Solving for any one variable when you know the other two.
- Proportional scaling: Resizing images, adjusting recipes, or computing tax all involve linear relationships.
- Machine learning: Linear regression fits y = mx + b to data points -- the simplest predictive model.