Linear Equation Solver
Build an app that solves systems of 2 and 3 variable linear equations using Cramer's Rule. Users enter the coefficients and constants, and the app calculates the values of each unknown variable.
🎯 Learning Goals
- ▹ Understand Linear Systems and intersections
- ▹ Apply Cramer's Rule for algebraic solving
- ▹ Learn determinant usage in solving and checks
- ▹ Build conditional logic and error handling in apps
🌎 Why This Matters
Solving equations is how we find 'equilibrium' in economics, optimize flight paths in aviation, and balance chemical equations. This app automates a process that is essential for every scientific field.
📖Understanding Systems of Linear Equations
Theory MasterclassA system of linear equations is a set of equations where each equation is a straight line (in 2D) or a plane (in 3D). The solution is the point where they intersect. For 2 variables: a₁x + b₁y = c₁ a₂x + b₂y = c₂ For 3 variables: a₁x + b₁y + c₁z = d₁ a₂x + b₂y + c₂z = d₂ a₃x + b₃y + c₃z = d₃ Cramer's Rule uses determinants to find the solution: 1. Calculate D (determinant of coefficient matrix) 2. Calculate Dx (replace x-column with constants) 3. Calculate Dy (replace y-column with constants) 4. x = Dx/D, y = Dy/D If D = 0, the system has no unique solution (either no solution or infinite solutions).
Mathematical Foundation
🎨Part A — Designer View (UI Design)
Open MIT App Inventor → Switch to Designer view. Follow each step below to build the interface.
1. Screen & Layout Setup
• Go to the **Properties** (right) for **Screen1**. • Set **Title** to "Linear Equation Solver". • Go to **Palette** (left) -> **Layout**. • Drag a **VerticalScrollArrangement** to the screen. Set its **Width** and **Height** to 'Fill Parent'. • This center container will hold all our inputs.
2. Setting up the Equations UI
• We need to solve: a1(x) + b1(y) = c1 and a2(x) + b2(y) = c2. • Drag 3 **HorizontalArrangements** from **Layout**. • In Row 1, drag 3 **TextBoxes**. Rename them: 'A1', 'B1', 'C1'. • In Row 2, drag 3 **TextBoxes**. Rename them: 'A2', 'B2', 'C2'. • Set all TextBoxes to **NumbersOnly** in Properties.
3. Adding the Action Button
• Drag a **Button** from **User Interface**. • Rename it to 'SolveBtn'. • Change its **Text** to "SOLVE FOR X & Y". • Change **BackgroundColor** to Blue and **FontBold** to checked.
4. Result Display
• Drag two **Labels** to the bottom. • Rename them to 'ResultX' and 'ResultY'. • Set their **Text** to "X = ?" and "Y = ?". • Set **TextColor** to green for high visibility.
🧩Part B — Blocks View (Logic & Calculation)
Switch to Blocks view. Now add the logic that makes your app actually work.
1. Switch to Blocks
• Look at the top right of the screen. • Click the **Blocks** button to start building your logic.
2. Setting up the Variables
• Go to the **Variables** drawer (Orange). Drag 'initialize global name to'. • Rename 'name' to 'D'. • Go to the **Math** drawer (Blue). Drag out a '0' block and snap it to your variable. • Do this again for 'Dx' and 'Dy'.
3. Calculating the Determinant (D)
• Click **SolveBtn**. Drag 'when SolveBtn.Click' into the workspace. • Go to **Variables**. Drag 'set global D' and snap it inside the button block. • From **Math** (Blue), drag subtraction '-' and multiplication '*' blocks. • Formula: (A1 * B2) - (A2 * B1). • Use **A1.Text**, **B2.Text**, etc., from their respective drawers.
4. Finding the Final Answer
• X = global Dx / global D. • Go to **ResultX**. Drag 'set ResultX.Text to'. • Go to **Math** (Blue). Get the '/' (division) block. • Snap them together: [set ResultX.Text] to [get global Dx] / [get global D].
5. Handling Mistakes (Zero D)
• Go to **Control** (Dark Orange). Drag 'if...then' and put it at the start of your code. • Go to **Math**. Drag '='. Check if [get global D] = [0]. • If true, 'set ResultX.Text to "Error: No unique solution"'.
🧪Testing Your App
- ✓Test: 2x + 3y = 8, x + y = 3 → Expected: x=1, y=2
- ✓Test: x + y = 5, 2x + 2y = 10 → D=0, infinite solutions
- ✓Test: x + y = 5, x + y = 3 → D=0, no solution
- ✓Test with negative coefficients: -3x + 2y = 1
- ✓Test the 3-variable mode with known solutions
🚀Bonus Challenges
Extra credit — impress your instructor
- ★Show the step-by-step calculation process, not just the answer
- ★Add a graphical representation showing the two lines intersecting
- ★Support decimal coefficients (not just integers)
- ★Add Gauss-Jordan elimination as an alternative method