Multiple linear equations, one shared goal: finding consistent solutions across variables. Linear systems reveal structure in problems and form the basis for scalable algorithms in science and engineering. 🔄
Systems of Linear Equations
A system of linear equations is a collection of one or more linear equations involving the same set of variables \(x_1, x_2, \ldots, x_n\) .
\[
\begin{aligned}
a_1 x_1 + b_1 x_2 &= c_1 \\
a_2 x_1 + b_2 x_2 &= c_2 \\
\end{aligned}
\]
viewof a1 = Inputs. range ([- 10 , 10 ], {
step : 1 ,
value : 2 ,
label : tex `a_1` ,
width : 200
})
viewof b1 = Inputs. range ([- 10 , 10 ], {
step : 1 ,
value : 3 ,
label : tex `b_1` ,
width : 200
})
viewof c1 = Inputs. range ([- 20 , 20 ], {
step : 1 ,
value : 6 ,
label : tex `c_1` ,
width : 200
})
// Second equation parameters
viewof a2 = Inputs. range ([- 10 , 10 ], {
step : 1 ,
value : 1 ,
label : tex `a_2` ,
width : 200
})
viewof b2 = Inputs. range ([- 10 , 10 ], {
step : 1 ,
value : - 2 ,
label : tex `b_2` ,
width : 200
})
viewof c2 = Inputs. range ([- 20 , 20 ], {
step : 1 ,
value : 4 ,
label : tex `c_2` ,
width : 200
})
// Display both equations
html `<div style="text-align: center; font-size: 1.2em; margin: 1em 0;">
<p> ${ tex ` ${ a1} x_1 ${ b1 >= 0 ? "+" : "-" } ${ Math . abs (b1)} x_2 = ${ c1} ` } </p>
<p> ${ tex ` ${ a2} x_1 ${ b2 >= 0 ? "+" : "-" } ${ Math . abs (b2)} x_2 = ${ c2} ` } </p>
</div>`
solution = {
// Using Cramer's rule to solve the system
const det = a1 * b2 - a2 * b1;
if (Math . abs (det) < 1e-10 ) {
// Check if lines are parallel and coincident
const k = a1 !== 0 ? a2/ a1 : b2/ b1;
if (Math . abs (c2 - k * c1) < 1e-10 ) {
return { status : "Infinite solutions" , x1 : null , x2 : null };
} else {
return { status : "No solution" , x1 : null , x2 : null };
}
} else {
// Unique solution
const x1 = (c1 * b2 - c2 * b1) / det;
const x2 = (a1 * c2 - a2 * c1) / det;
return { status : "Unique solution" , x1, x2 };
}
}
// Generate points for first line
points1 = {
const result = [];
for (let i = 0 ; i < 200 ; i++ ) {
const x = i / 10 - 10 ;
if (b1 !== 0 ) {
const y = (c1 - a1 * x) / b1;
if (y >= - 10 && y <= 10 ) {
result. push ({x, y});
}
}
}
return result;
}
// Generate points for second line
points2 = {
const result = [];
for (let i = 0 ; i < 200 ; i++ ) {
const x = i / 10 - 10 ;
if (b2 !== 0 ) {
const y = (c2 - a2 * x) / b2;
if (y >= - 10 && y <= 10 ) {
result. push ({x, y});
}
}
}
return result;
}
// Create the plot
Plot. plot ({
style : "overflow: visible; display: block; margin: 0 auto;" ,
width : 500 ,
height : 400 ,
grid : true ,
x : {label : "x₁" , domain : [- 10 , 10 ]},
y : {label : "x₂" , domain : [- 10 , 10 ]},
marks : [
Plot. ruleY ([0 ]),
Plot. ruleX ([0 ]),
Plot. line (points1, {
x : "x" ,
y : "y" ,
stroke : "steelblue" ,
strokeWidth : 2
}),
Plot. line (points2, {
x : "x" ,
y : "y" ,
stroke : "crimson" ,
strokeWidth : 2
}),
solution. x1 !== null ? Plot. dot ([{x : solution. x1 , y : solution. x2 }], {
x : "x" ,
y : "y" ,
r : 6 ,
fill : "black" ,
stroke : "white" ,
strokeWidth : 2
}) : null
]
})
html `<div style="text-align: center; margin: 1em 0; padding: 10px; border-radius: 5px; background-color: ${ solution. status === "No solution" ? "#ffecec" : solution. status === "Infinite solutions" ? "#fffacd" : "#e6ffe6" } ;">
<p style="font-weight: bold;"> ${ solution. status } </p>
${ solution. x1 !== null ? html `<p> ${ tex `(x_1, x_2) = ( ${ solution. x1 . toFixed (2 )} , ${ solution. x2 . toFixed (2 )} )` } </p>` : '' }
</div>`