TExcelTAD
Home › Algebra › Solving Systems by Graphing

Solving Systems by Graphing

Every equation in two unknowns has a graph, and a solution of a system is a point that lies on all of the graphs at once. So you can solve a system by drawing the graphs and finding where they meet. Graphing is less precise than algebra, but it shows how many solutions to expect, and its geometric view is exactly how games test whether a bullet hits a wall.

Where the lines meet

Solve y=2x−1y = 2x - 1 and y=−x+5y = -x + 5 by graphing.

  1. Draw both lines (from their intercepts or slopes; see Graphing Linear Equations).
  2. Read the coordinates of the crossing point: (2,3)(2, 3).
  3. Check algebraically in both equations: 2⋅2−1=32 \cdot 2 - 1 = 3 ✓ and −2+5=3-2 + 5 = 3 ✓.

The check matters because a graph only gives an estimate. For y=x3+1y = \frac{x}{3} + 1 and y=−x+4y = -x + 4, a careful sketch suggests "about (2.2,1.8)(2.2, 1.8)"; the exact answer from substitution is (94,74)=(2.25,1.75)(\frac{9}{4}, \frac{7}{4}) = (2.25, 1.75). Use the graph to see the answer, then algebra to nail it.

The three cases in pictures

python · try it yourself
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-1, 5, 50)
cases = [
    ("one solution",        2 * x - 1, -x + 5,      "y = −x + 5"),
    ("no solution",         2 * x - 1, 2 * x + 2,   "y = 2x + 2"),
    ("infinitely many",     2 * x - 1, (4 * x - 2) / 2, "4x − 2y = 2"),
]
fig, axes = plt.subplots(1, 3, figsize=(9, 3), sharey=True)
for ax, (title, y1, y2, label2) in zip(axes, cases):
    ax.plot(x, y1, label="y = 2x − 1", linewidth=3, alpha=0.6)
    ax.plot(x, y2, "--", label=label2)
    ax.set_title(title); ax.grid(True, alpha=0.3); ax.legend(fontsize=7)
axes[0].plot(2, 3, "ko")
plt.tight_layout()
plt.show()
print("crossing, parallel, and the same line")
crossing, parallel, and the same line
Picture Slopes and intercepts Solutions
lines cross different slopes exactly one
parallel lines same slope, different intercepts none
one line drawn twice same slope, same intercept infinitely many

So you can classify a linear system without drawing anything: put both equations in the form y=mx+by = mx + b and compare mm and bb. In the third panel, 4x−2y=24x - 2y = 2 becomes y=2x−1y = 2x - 1, the same line.

Curves can meet more than once

Graphing works for any pair of equations, not only lines. A line and a parabola can meet twice, once (the line is tangent) or not at all; two parabolas or a line and a circle likewise. For y=x2y = x^2 and y=x+2y = x + 2 the graphs cross at (−1,1)(-1, 1) and (2,4)(2, 4), the solutions found in The Substitution Method. Graphing is often the fastest way to see how many solutions a non-linear system has before you solve it.

In software: segment intersection in 2D games

A game rarely asks where two infinite lines meet. It asks whether this bullet path, from where the bullet was last frame to where it is now, crosses this wall segment. That is a system of equations plus a range check.

Write each segment with a parameter: the bullet is P+t⋅rP + t \cdot r for 0≤t≤10 \le t \le 1 (r=end−startr = \text{end} - \text{start}), the wall is Q+u⋅sQ + u \cdot s for 0≤u≤10 \le u \le 1. They meet where

P+t⋅r=Q+u⋅s→t⋅r−u⋅s=Q−PP + t \cdot r = Q + u \cdot s \qquad \to \qquad t \cdot r - u \cdot s = Q - P

That is two equations (xx and yy components) in two unknowns tt and uu. Solving it with Cramer's rule gives formulas in terms of the 2D cross product cross⁡(a,b)=ax⋅by−ay⋅bx\operatorname{cross}(a, b) = a_x \cdot b_y - a_y \cdot b_x:

t=cross⁡(Q−P,s)cross⁡(r,s)u=cross⁡(Q−P,r)cross⁡(r,s)t = \frac{\operatorname{cross}(Q - P, s)}{\operatorname{cross}(r, s)} \qquad u = \frac{\operatorname{cross}(Q - P, r)}{\operatorname{cross}(r, s)}

If cross⁡(r,s)=0\operatorname{cross}(r, s) = 0 the segments are parallel. Otherwise the lines meet, and the segments meet only if both tt and uu lie between 0 and 1.

python · try it yourself
def cross(a, b):
    return a[0] * b[1] - a[1] * b[0]

def segment_hit(p, p_end, q, q_end):
    r = (p_end[0] - p[0], p_end[1] - p[1])
    s = (q_end[0] - q[0], q_end[1] - q[1])
    denom = cross(r, s)
    if denom == 0:
        return "parallel"
    qp = (q[0] - p[0], q[1] - p[1])
    t = cross(qp, s) / denom
    u = cross(qp, r) / denom
    if 0 <= t <= 1 and 0 <= u <= 1:
        return f"hit at ({p[0] + t * r[0]}, {p[1] + t * r[1]}), t = {t}"
    return f"lines meet but segments miss (t = {t}, u = {u})"

wall = ((0, 4), (4, 0))
print(segment_hit((0, 0), (4, 4), *wall))      # long shot through the wall
print(segment_hit((0, 0), (1, 1), *wall))      # short shot, stops before the wall
print(segment_hit((0, 5), (4, 5), (0, 6), (4, 6)))
hit at (2.0, 2.0), t = 0.5
lines meet but segments miss (t = 2.0, u = 0.5)
parallel

In the second call the lines still cross at (2,2)(2, 2), but that point is at t=2t = 2, twice as far as the bullet travelled this frame, so there is no hit yet. The value of tt is useful in its own right: it is the fraction of the frame at which the hit happens, which engines use to place the impact effect and for continuous collision detection. The same test powers line-of-sight checks, mouse picking of edges and polygon clipping. Real code also compares the denominator with a small tolerance instead of exactly 0, because coordinates are floats.

Exercises

Exercise 1: Graph and check

Solve y=x−2y = x - 2 and y=−0.5x+4y = -0.5x + 4 by graphing, then check algebraically.

Answer

(4,2)(4, 2).

Solution

Both lines pass through (4,2)(4, 2) on a sketch. Algebra:

x−2=−0.5x+4  →  1.5x=6  →  x=4,y=4−2=2check: −0.5⋅4+4=2 ✓\begin{aligned} &x - 2 = -0.5x + 4 \;\to\; 1.5x = 6 \;\to\; x = 4,\quad y = 4 - 2 = 2 \\ &\text{check: } -0.5 \cdot 4 + 4 = 2\ \checkmark \end{aligned}

Exercise 2: Classify without drawing

How many solutions does the system y=3x+2y = 3x + 2, 6x−2y=86x - 2y = 8 have?

Answer

None: the lines are parallel.

Solution
6x−2y=8  →  −2y=−6x+8  →  y=3x−4slopes 3 and 3 (equal), intercepts 2 and −4 (different)  →  parallel, no solution\begin{aligned} &6x - 2y = 8 \;\to\; -2y = -6x + 8 \;\to\; y = 3x - 4 \\ &\text{slopes } 3 \text{ and } 3 \text{ (equal), intercepts } 2 \text{ and } -4 \text{ (different)} \;\to\; \text{parallel, no solution} \end{aligned}

Exercise 3: Exact from estimate

A graph of y=x3+1y = \frac{x}{3} + 1 and y=−x+4y = -x + 4 suggests an intersection near (2.2,1.8)(2.2, 1.8). Find the exact point.

Answer

(94,74)=(2.25,1.75)(\frac{9}{4}, \frac{7}{4}) = (2.25, 1.75).

Solution
x3+1=−x+4  →  multiply by 3: x+3=−3x+12  →  4x=9  →  x=94y=−94+4=74\begin{aligned} &\frac{x}{3} + 1 = -x + 4 \;\to\; \text{multiply by 3: } x + 3 = -3x + 12 \;\to\; 4x = 9 \;\to\; x = \frac{9}{4} \\ &y = -\frac{9}{4} + 4 = \frac{7}{4} \end{aligned}
python · try it yourself
from sympy import symbols, Eq, Rational, solve
x, y = symbols("x y")
print(solve([Eq(y, x * Rational(1, 3) + 1), Eq(y, -x + 4)], [x, y]))
{x: 9/4, y: 7/4}

Exercise 4: Do the segments cross?

Does the segment from (1,1)(1, 1) to (5,3)(5, 3) cross the segment from (2,4)(2, 4) to (4,0)(4, 0)? If so, where?

Answer

Yes, at (3,2)(3, 2), halfway along both segments (t=u=0.5t = u = 0.5).

Solution
r=(4,2),s=(2,−4),Q−P=(1,3)cross⁡(r,s)=4⋅(−4)−2⋅2=−20cross⁡(Q−P,s)=1⋅(−4)−3⋅2=−10→  t=−10−20=0.5cross⁡(Q−P,r)=1⋅2−3⋅4=−10→  u=−10−20=0.5both in [0,1]: hit at (1,1)+0.5⋅(4,2)=(3,2)\begin{aligned} &r = (4, 2),\quad s = (2, -4),\quad Q - P = (1, 3) \\ \operatorname{cross}(r, s) &= 4 \cdot (-4) - 2 \cdot 2 = -20 \\ \operatorname{cross}(Q - P, s) &= 1 \cdot (-4) - 3 \cdot 2 = -10 && \to\; t = \frac{-10}{-20} = 0.5 \\ \operatorname{cross}(Q - P, r) &= 1 \cdot 2 - 3 \cdot 4 = -10 && \to\; u = \frac{-10}{-20} = 0.5 \\ &\text{both in } [0, 1]\text{: hit at } (1, 1) + 0.5 \cdot (4, 2) = (3, 2) \end{aligned}
python · try it yourself
def cross(a, b):
    return a[0] * b[1] - a[1] * b[0]
r, s, qp = (4, 2), (2, -4), (1, 3)
t, u = cross(qp, s) / cross(r, s), cross(qp, r) / cross(r, s)
print(t, u, (1 + t * r[0], 1 + t * r[1]))
0.5 0.5 (3.0, 2.0)

Test yourself

Two lines in a system have the same slope and different y-intercepts. What does the graph show?

  1. One crossing point
  2. Parallel lines: no solution
  3. The same line: infinitely many solutions
  4. Perpendicular lines

Why should you check a solution read from a graph?

  1. Graphs are always wrong
  2. A graph only gives an estimate; fractional coordinates are easy to misread
  3. Algebra and graphs disagree
  4. It is not necessary

In the segment test, the lines meet at t=1.4t = 1.4, u=0.3u = 0.3. Does the moving segment (0≤t≤10 \le t \le 1) hit the wall this frame?

  1. Yes, because uu is between 0 and 1
  2. No, because t>1t > 1: the crossing lies beyond the end of the segment
  3. Yes, at 140 % of the wall
  4. Only if t⋅u<1t \cdot u < 1