How to symbolically solve a system of linear equations

1.4k Views Asked by At

A fellow user had an interesting question:

Less-tedious way of solving this system of linear equations?

Is there a shortcut to solving for $w_1, w_2, w_3$ in $$ \left[ \begin{array}{ccc} 1 & 1 & 1\\ \frac{3}{4}a+\frac{1}{4}b & \frac{1}{2}a+\frac{1}{2}b & \frac{1}{4}a+\frac{3}{4}b\\ (\frac{3}{4}a+\frac{1}{4}b)^2 & (\frac{1}{2}a+\frac{1}{2}b)^2 & (\frac{1}{4}a+\frac{3}{4}b)^2\\ \end{array} \right] \cdot \left[ \begin{array}{c} w_1\\ w_2\\ w_3 \end{array} \right] = \left[ \begin{array}{c} b-a\\ \frac{b^2-a^2}{2}\\ \frac{b^3-a^3}{3} \end{array} \right] $$ where $a, b$ are constants? (I'm trying to derive the formula for a 3-point open Newton-Cotes quadrature rule.) Thanks!

Unfortunately the question was deleted (link), but I think it might be interesting in general.

2

There are 2 best solutions below

0
On

One way is to use the free Maxima computer algebra system.

You can enter your matrix like this:

A: matrix(
 [1,1,1], 
 [(3/4)*a+(1/4)*b,(1/2)*a+(1/2)*b,(1/4)*a+(3/4)*b], 
 [((3/4)*a+(1/4)*b)^2,((1/2)*a+(1/2)*b)^2,((1/4)*a+(3/4)*b)^2]
 );

$$ \begin{pmatrix}1 & 1 & 1\\ \frac{b}{4}+\frac{3a}{4} & \frac{b}{2}+\frac{a}{2} & \frac{3b}{4}+\frac{a}{4}\\ {{\left( \frac{b}{4}+\frac{3a}{4}\right) }^{2}} & {{\left( \frac{b}{2}+\frac{a}{2}\right) }^{2}} & {{\left( \frac{3b}{4}+\frac{a}{4}\right) }^{2}}\end{pmatrix} $$
and the result vector like that:

bcol: matrix(
 [b-a], 
 [(b^2-a^2)/2], 
 [(b^3-a^3)/3]
);

$$ \begin{pmatrix}b-a\\ \frac{{{b}^{2}}-{{a}^{2}}}{2}\\ \frac{{{b}^{3}}-{{a}^{3}}}{3}\end{pmatrix} $$ This command will solve your linear system by $LU$ decomposition:

ls : linsolve_by_lu(A,bcol);

You extract and simplify the solution vector like this:

xycol: ratsimp(first(ls));

$$ \begin{pmatrix}\frac{2b-2a}{3}\\ -\frac{b-a}{3}\\ \frac{2b-2a}{3}\end{pmatrix} $$ where first picks the first element of the result and ratsimp is one of Maxima's simplification functions.

With this you can verify the result:

ratsimp(A . xycol);

$$ \begin{pmatrix}b-a\\ \frac{{{b}^{2}}-{{a}^{2}}}{2}\\ \frac{{{b}^{3}}-{{a}^{3}}}{3}\end{pmatrix} $$ Note that Maxima uses a dot for the matrix multiplication operator.

0
On

One can also make use of the Mathematica programming language and proceed as follows:

screenshot of the interactive session where we solve the system with the Mathematica programming language

where the lines with MatrixForm[$\cdots$] were just to check if the matrices were fine.

The command LinearSolve[matrix, result] finds the solution and the added // MatrixForm is just for pretty printing.