0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Sum–Difference Method (System of Linear Equations) — Full Explanation + Interactive Python

0
Posted at

Sum–Difference Method (System of Linear Equations) — Full Explanation + Interactive Python

1. Overview of the Sum–Difference Problem

A sum–difference problem gives you:

  • The sum of two unknown numbers
  • The difference between the same two numbers

Your task is to find the actual numeric values.

This appears frequently in:

  • Elementary arithmetic (“Wasasan”)
  • Middle school algebra
  • Linear equation solving
  • Word problems involving comparison

2. Mathematical Formulation

Let the two unknown quantities be:

  • (x): Ichiro’s allowance
  • (y): Jiro’s allowance

The problem states:

(1) Sum

[
x + y = S
]

(2) Difference

[
x - y = D
]

These two linear equations form a system of equations:

[
\begin{cases}
x + y = S
x - y = D
\end{cases}
]

Where:

  • (S): known total amount
  • (D): known difference

3. Why the Method Works (Derivation)

Add the two equations:

[
(x + y) + (x - y) = S + D
]

Left side simplifies:

[
2x = S + D
]

Therefore:

Solution for x

[
x = \frac{S + D}{2}
]

Subtract the second equation from the first:

[
(x + y) - (x - y) = S - D
]

Left side simplifies:

[
2y = S - D
]

So:

Solution for y

[
y = \frac{S - D}{2}
]


4. Why the Line-Segment Diagram (Bar Model) Works

The visualization uses two horizontal bars:

  • The longer bar: (x)
  • The shorter bar: (y)
  • The small red segment between them: the difference (D)
|---------------- x ----------------|
|------------ y ------------|
                |--- D ---|

This visually shows:

[
x = y + D
]

When both bars are “cut in half” conceptually, you obtain:

  • The overlapping length = value of (y)
  • The extra segment = difference (D)

This representation helps beginners intuitively understand the algebraic structure.


5. What the Interactive Program Does

This Python program:

  • Lets the user control
    Sum (S) and Difference (D) using sliders

  • Draws bars representing (x) and (y)

  • Plots the red “difference segment” between them

  • Computes the exact solution using:
    [
    x = \frac{S + D}{2},\quad y = \frac{S - D}{2}
    ]

  • Displays the full system of equations (typeset with math)

  • Includes:

    • Reset button
    • PNG saving
    • Mathematically accurate diagram

You can interactively change numbers and watch the bars adjust—this helps build intuition.


6. Full Interactive Python Code (English Version)

Copy & run in Google Colab.

# ============================================================
# Program Name: sum_difference_interactive.py
# Creation Date: 202501
# Purpose: Interactive Sum–Difference (System of Equations) Visualization
# ============================================================

!pip install numpy matplotlib ipywidgets --quiet

import numpy as np
import matplotlib.pyplot as plt
import ipywidgets as widgets
from ipywidgets import HBox, VBox, Button, Output
import datetime

# ------------------------------------------------------------
# PARAM_INIT
# ------------------------------------------------------------
PARAM_INIT = {
    "SUM": 2400,   # Sum (S)
    "DIFF": 400    # Difference (D)
}

# ------------------------------------------------------------
# Compute solution for x and y
# ------------------------------------------------------------
def compute_values(S, D):
    """
    Inputs:
        S : sum
        D : difference
    Outputs:
        x : larger value
        y : smaller value
    Process:
        System of linear equations:
            x + y = S
            x - y = D
        Solve:
            x = (S + D) / 2
            y = (S - D) / 2
    """
    x = (S + D) / 2
    y = (S - D) / 2
    return x, y

# ------------------------------------------------------------
# Plot function
# ------------------------------------------------------------
def plot_sum_diff(S, D):
    x, y = compute_values(S, D)

    fig, ax = plt.subplots(figsize=(8, 3))
    ax.set_xlim(0, S * 0.7)
    ax.set_ylim(-0.2, 1.2)
    ax.axis('off')

    # -------------------------------
    # Bars: Line segment diagram
    # -------------------------------
    ax.plot([0, x], [0.8, 0.8], lw=14, color="skyblue")
    ax.text(x + 20, 0.8, f"x = {int(x)}", va='center')

    ax.plot([0, y], [0.4, 0.4], lw=14, color="orange")
    ax.text(y + 20, 0.4, f"y = {int(y)}", va='center')

    # Difference bar
    ax.plot([y, x], [0.6, 0.6], lw=6, color="red")
    ax.text((x + y) / 2, 0.63, f"Difference = {D}", ha='center', color='red')

    # -------------------------------
    # System of Equations (drawn on figure)
    # -------------------------------
    eq_text = (
        r"System of Linear Equations:" "\n"
        r"$\;x + y = S$" "\n"
        r"$\;x - y = D$" "\n"
        fr"S = {S},  D = {D}" "\n"
        fr"$\Rightarrow x = {(S + D) / 2},\; y = {(S - D) / 2}$"
    )
    ax.text(0, -0.1, eq_text, fontsize=11, va='top')

    plt.title(f"Sum = {S}, Difference = {D}", fontsize=14)
    plt.show()

# ------------------------------------------------------------
# Widgets
# ------------------------------------------------------------
sum_slider = widgets.IntSlider(
    value=PARAM_INIT["SUM"], min=100, max=5000, step=50,
    description="Sum (S)", continuous_update=False
)

diff_slider = widgets.IntSlider(
    value=PARAM_INIT["DIFF"], min=0, max=2000, step=10,
    description="Diff (D)", continuous_update=False
)

out = Output()

def update_plot(*args):
    with out:
        out.clear_output(wait=True)
        plot_sum_diff(sum_slider.value, diff_slider.value)

sum_slider.observe(update_plot, names='value')
diff_slider.observe(update_plot, names='value')

# ------------------------------------------------------------
# Reset button
# ------------------------------------------------------------
reset_btn = Button(description="Reset", button_style="warning")

def reset_values(b):
    sum_slider.value = PARAM_INIT["SUM"]
    diff_slider.value = PARAM_INIT["DIFF"]

reset_btn.on_click(reset_values)

# ------------------------------------------------------------
# PNG save button
# ------------------------------------------------------------
save_btn = Button(description="Save PNG", button_style="success")

def save_png(b):
    ts = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
    fname = f"sumdiff_{ts}.png"
    S = sum_slider.value
    D = diff_slider.value

    x, y = compute_values(S, D)

    fig, ax = plt.subplots(figsize=(8, 3))
    ax.set_xlim(0, S * 0.7)
    ax.set_ylim(-0.2, 1.2)
    ax.axis('off')

    ax.plot([0, x], [0.8, 0.8], lw=14, color="skyblue")
    ax.plot([0, y], [0.4, 0.4], lw=14, color="orange")
    ax.plot([y, x], [0.6, 0.6], lw=6, color="red")

    ax.text(0, -0.1, f"x={(S + D)/2}, y={(S - D)/2}", fontsize=12)
    plt.title(f"Sum = {S}, Diff = {D}")
    plt.savefig(fname, dpi=200)
    plt.close()

    with out:
        print(f"Saved: {fname}")

save_btn.on_click(save_png)

# ------------------------------------------------------------
# Display UI
# ------------------------------------------------------------
display(VBox([
    HBox([sum_slider, diff_slider]),
    HBox([reset_btn, save_btn]),
    out
]))

# Initial plot
update_plot()
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?