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.
Set up the screen
Set Screen1 title to "Linear Equation Solver". Set BackgroundColor to dark theme. Add a Label at the top: "Enter Coefficients" with large font.
Add mode toggle buttons
Add a HorizontalArrangement with 2 Buttons: "2-Variable" and "3-Variable". These switch between 2-eq and 3-eq input modes.
Create input for 2-variable system
Add a VerticalArrangement named "TwoVarPanel". Add 2 HorizontalArrangements (one per equation). Row 1: TextBox (aβ), Label "x +", TextBox (bβ), Label "y =", TextBox (cβ) Row 2: TextBox (aβ), Label "x +", TextBox (bβ), Label "y =", TextBox (cβ) Set all TextBoxes: NumbersOnly = true, Width = 15%.
Create input for 3-variable system
Add a VerticalArrangement named "ThreeVarPanel" (set Visible = false initially). Add 3 rows with TextBoxes for a, b, c coefficients and d constant each. Label each row: "Equation 1", "Equation 2", "Equation 3".
Add Solve and Clear buttons
Add a HorizontalArrangement with: Button "SOLVE" (green background, bold) Button "CLEAR" (red background)
Create result display area
Add Labels: "Solution:" as a header label. ResultLabel for showing x = ..., y = ..., z = ... StatusLabel for showing "Unique Solution", "No Solution", or "Infinite Solutions".
π§©Part B β Blocks View (Logic & Calculation)
Switch to Blocks view. Now add the logic that makes your app actually work.
Mode toggle logic
When TwoVarButton.Click: Set TwoVarPanel.Visible = true Set ThreeVarPanel.Visible = false When ThreeVarButton.Click: Set TwoVarPanel.Visible = false Set ThreeVarPanel.Visible = true
Calculate determinant D for 2 variables
When SolveButton.Click: First, read all values: set a1 = number(TextBoxA1.Text) set b1 = number(TextBoxB1.Text) set c1 = number(TextBoxC1.Text) (constant) set a2 = number(TextBoxA2.Text) set b2 = number(TextBoxB2.Text) set c2 = number(TextBoxC2.Text) Calculate D = (a1 Γ b2) - (a2 Γ b1)
Check if solution exists
if D = 0 then: Set StatusLabel.Text = "No unique solution exists!" Set StatusLabel.TextColor = red Stop here (don't calculate further). else: Continue to next step.
Calculate Dx and Dy
Dx = (c1 Γ b2) - (c2 Γ b1) Dy = (a1 Γ c2) - (a2 Γ c1)
Find x and y values
x = Dx / D y = Dy / D Set ResultLabel.Text to: join "x = " x "\ny = " y Set StatusLabel.Text = "β Unique Solution Found" Set StatusLabel.TextColor = green
Build 3-variable solver (advanced)
For 3 variables, calculate the 3Γ3 determinant: D = a1(b2c3 - b3c2) - b1(a2c3 - a3c2) + c1(a2b3 - a3b2) Then Dx, Dy, Dz by replacing each column with the constants. x = Dx/D, y = Dy/D, z = Dz/D
Clear button logic
When ClearButton.Click: Set all TextBox.Text = "" Set ResultLabel.Text = "" Set StatusLabel.Text = ""
Input validation
Before solving, check each TextBox: if any TextBox is empty β show Notifier alert "Please fill all coefficients" This prevents crashes from empty or non-numeric input.
π§ͺ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