Computer Algebra Systems
The linear systems in this chapter are small enough that their solution by hand is easy. For large systems, including those involving thousands of equations, we need a computer. There are special purpose programs such as LINPACK for this. Also popular are general purpose computer algebra systems including Maple, Mathematica, or MATLAB, and Sage.
For example, in the Topic on Networks, we need to solve this.
Doing this by hand would take time and be error-prone. A computer is better.
Here is that system solved with Sage. (There are many ways to do this; the one here has the advantage of simplicity.)
sage: var('i0,i1,i2,i3,i4,i5,i6')
(i0, i1, i2, i3, i4, i5, i6)
sage: network_system=[i0-i1-i2==0, i1-i3-i5==0,
....: i2-i4+i5==0, i3+i4-i6==0, 5*i1+10*i3==10,
....: 2*i2+4*i4==10, 5*i1-2*i2+50*i5==0]
sage: solve(network_system, i0,i1,i2,i3,i4,i5,i6)
[[i0 == (7/3), i1 == (2/3), i2 == (5/3), i3 == (2/3),
i4 == (5/3), i5 == 0, i6 == (7/3)]]
Magic.
Here is the same system solved under Maple. We enter the array of coefficients and the vector of constants, and then we get the solution.
> A:=array( [[1,-1,-1,0,0,0,0],
[0,1,0,-1,0,-1,0],
[0,0,1,0,-1,1,0],
[0,0,0,1,1,0,-1],
[0,5,0,10,0,0,0],
[0,0,2,0,4,0,0],
[0,5,-2,0,0,50,0]] );
> u:=array( [0,0,0,0,10,10,0] );
> linsolve(A,u);
7 2 5 2 5 7
[ -, -, -, -, -, 0, - ]
3 3 3 3 3 3
If a system has infinitely many solutions then the program will return a parametrization.
Exercises
Exercise 1 Worked answer
Use the computer to solve the two problems that opened this chapter.
This is the Statics problem.
This is the Chemistry problem.
Answer.
Sage does this.
sage: var('h,c') (h, c) sage: statics = [40*h + 15*c == 100, ....: 25*c == 50 + 50*h] sage: solve(statics, h,c) [[h == 1, c == 4]]Other Computer Algebra Systems have similar commands. These Maple commands
> A:=array( [[40,15], [-50,25]] ); > u:=array([100,50]); > linsolve(A,u);get the answer
[1,4].Here there is a free variable. Sage gives this.
sage: var('h,i,j,k') (h, i, j, k) sage: chemistry = [7*h == 7*j, ....: 8*h + 1*i == 5*j + 2*k, ....: 1*i == 3*j, ....: 3*i == 6*j + 1*k] sage: solve(chemistry, h,i,j,k) [[h == 1/3*r1, i == r1, j == 1/3*r1, k == r1]]Similarly, this Maple session
> A:=array( [[7,0,-7,0], [8,1,-5,2], [0,1,-3,0], [0,3,-6,-1]] ); > u:=array([0,0,0,0]); > linsolve(A,u);prompts the same reply (but with parameter ).
Exercise 2 Worked answer
Use the computer to solve these systems from the first subsection, or conclude ‘many solutions’ or ‘no solutions’.
Answer.
The answer is and . A Sage session does this.
sage: var('x,y') (x, y) sage: system = [2*x + 2*y == 5, ....: x - 4*y == 0] sage: solve(system, x,y) [[x == 2, y == (1/2)]]A Maple session
> A:=array( [[2,2], [1,-4]] ); > u:=array([5,0]); > linsolve(A,u);gets the same answer, of course.
The answer is and .
sage: var('x,y') (x, y) sage: system = [-1*x + y == 1, ....: x + y == 2] sage: solve(system, x,y) [[x == (1/2), y == (3/2)]]This system has infinitely many solutions. In the first subsection, with as a parameter, we got and . Sage gets the same.
sage: var('x,y,z') (x, y, z) sage: system = [x - 3*y + z == 1, ....: x + y + 2*z == 14] sage: solve(system, x,y) [[x == -7/4*z + 43/4, y == -1/4*z + 13/4]]Maple responds with , preferring as a parameter.
There is no solution to this system. Sage gives an empty list.
sage: var('x,y') (x, y) sage: system = [-1*x - y == 1, ....: -3*x - 3*y == 2] sage: solve(system, x,y) []Similarly, When the array and vector are given to Maple and it is asked to
linsolve(A,u), it returns no result at all; that is, it responds with no solutions.Sage finds
sage: var('x,y,z') (x, y, z) sage: system = [ 4*y + z == 20, ....: 2*x - 2*y + z == 0, ....: x + z == 5, ....: x + y - z == 10] sage: solve(system, x,y,z) [[x == 5, y == 5, z == 0]]that the solutions is .
There are infinitely many solutions. Sage does this.
sage: var('x,y,z,w') (x, y, z, w) sage: system = [ 2*x + z + w == 5, ....: y - w == -1, ....: 3*x - z - w == 0, ....: 4*x + y + 2*z + w == 9] sage: solve(system, x,y,z,w) [[x == 1, y == r2 - 1, z == -r2 + 3, w == r2]]Maple gives .
Exercise 3 Worked answer
Use the computer to solve these systems from the second subsection.
Answer.
This system has infinitely many solutions. In the second subsection we gave the solution set as
and Sage finds
sage: var('x,y') (x, y) sage: system = [ 3*x + 6*y == 18, ....: x + 2*y == 6] sage: solve(system, x,y) [[x == -2*r3 + 6, y == r3]]while Maple responds with .
The solution set has only one member.
Sage gives this.
sage: var('x,y') (x, y) sage: system = [ x + y == 1, ....: x - y == -1] sage: solve(system, x,y) [[x == 0, y == 1]]This system’s solution set is infinite
Sage gives this
sage: var('x1,x2,x3') (x1, x2, x3) sage: system = [ x1 + x3 == 4, ....: x1 - x2 + 2*x3 == 5, ....: 4*x1 - x2 + 5*x3 == 17] sage: solve(system, x1,x2,x3) [[x1 == -r4 + 4, x2 == r4 - 1, x3 == r4]]and Maple gives .
There is a unique solution
and Sage finds it.
sage: var('a,b,c') (a, b, c) sage: system = [ 2*a + b - c == 2, ....: 2*a + c == 3, ....: a - b == 0] sage: solve(system, a,b,c) [[a == 1, b == 1, c == 1]]This system has infinitely many solutions; in the second subsection we described the solution set with two parameters.
Sage does the same.
sage: var('x,y,z,w') (x, y, z, w) sage: system = [ x + 2*y - z == 3, ....: 2*x + y + w == 4, ....: x - y + z + w == 1] sage: solve(system, x,y,z,w) [[x == r6, y == -r5 - 2*r6 + 4, z == -2*r5 - 3*r6 + 5, w == r5]]as does Maple .
The solution set is empty.
sage: var('x,y,z,w') (x, y, z, w) sage: system = [ x + z + w == 4, ....: 2*x + y - w == 2, ....: 3*x + y + z == 7] sage: solve(system, x,y,z,w) []
Exercise 4 Worked answer
What does the computer give for the solution of the general system?
Answer. Sage does this.
sage: var('x,y,a,b,c,d,p,q') (x, y, a, b, c, d, p, q) sage: system = [ a*x + c*y == p, ....: b*x + d*y == q] sage: solve(system, x,y) [[x == -(d*p - c*q)/(b*c - a*d), y == (b*p - a*q)/(b*c - a*d)]]In response to this prompting
> A:=array( [[a,c], [b,d]] ); > u:=array([p,q]); > linsolve(A,u);Maple gave this reply.