Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168690
Image: ubuntu2004
Section 1.1
# 13
Note that "#" starts a comment which will not be evaluated by Sage. Thus, the matrix A is defined below, but everything on the same line after "#" is ignored.
A = matrix(QQ,[[1,0,-3],[2,2,9],[0,1,5]]) # define non-augmented matrix A A
\newcommand{\Bold}[1]{\mathbf{#1}}\left(103229015\begin{array}{rrr} 1 & 0 & -3 \\ 2 & 2 & 9 \\ 0 & 1 & 5 \end{array}\right)
b = vector(QQ, [8,7,-2]) # define b b
\newcommand{\Bold}[1]{\mathbf{#1}}\left(8,\,7,\,-2\right)
C = A.augment(b) # augment b in order to row reduce the system C
\newcommand{\Bold}[1]{\mathbf{#1}}\left(103822970152\begin{array}{rrrr} 1 & 0 & -3 & 8 \\ 2 & 2 & 9 & 7 \\ 0 & 1 & 5 & -2 \end{array}\right)
C.rref() # perform row operations ("reduced row echelon form", coming soon section 1.2!)
\newcommand{\Bold}[1]{\mathbf{#1}}\left(100501030011\begin{array}{rrrr} 1 & 0 & 0 & 5 \\ 0 & 1 & 0 & 3 \\ 0 & 0 & 1 & -1 \end{array}\right)
Therefore, the solution vector is (5,3,-1). This should concur with your paper and pencil computations.
# 15. Instead of "rref", we'll proceed using the matrix's built-in elementary row operations. Note, each row operation below alters the matrix M "in place"
M = matrix(QQ, [[1,-6,0,0,5],[0,1,-4,1,0],[-1,6,1,5,3],[0,-1,5,4,0]]) # M is the augmented matrix for the system in 15 M
\newcommand{\Bold}[1]{\mathbf{#1}}\left(16005014101615301540\begin{array}{rrrrr} 1 & -6 & 0 & 0 & 5 \\ 0 & 1 & -4 & 1 & 0 \\ -1 & 6 & 1 & 5 & 3 \\ 0 & -1 & 5 & 4 & 0 \end{array}\right)
M.add_multiple_of_row(2,0,1) M # Typing ";M" is a shortcut to put a second command on the same line, and print M # The syntax above tells Sage to "add 1 times row0 to row2". Sage uses 0-based indexing for rows.
M.add_multiple_of_row(3,1,1);M
\newcommand{\Bold}[1]{\mathbf{#1}}\left(16005014100015800150\begin{array}{rrrrr} 1 & -6 & 0 & 0 & 5 \\ 0 & 1 & -4 & 1 & 0 \\ 0 & 0 & 1 & 5 & 8 \\ 0 & 0 & 1 & 5 & 0 \end{array}\right)
M.add_multiple_of_row(3,2,-1);M
\newcommand{\Bold}[1]{\mathbf{#1}}\left(16005014100015800008\begin{array}{rrrrr} 1 & -6 & 0 & 0 & 5 \\ 0 & 1 & -4 & 1 & 0 \\ 0 & 0 & 1 & 5 & 8 \\ 0 & 0 & 0 & 0 & -8 \end{array}\right)
The system is inconsistent. The last line requires that 0=-8 in order for a solution to exist.
#25
Define new variables g,h,k. The "domain=QQ" is Sage's way of saying make the variables rationals. You could also make them reals (decimals in this case) by typing "RR" instead of "QQ". This forces the row reductions to output decimal answers (which in my opinion are not as nice).
var('g,h,k', domain=QQ)
\newcommand{\Bold}[1]{\mathbf{#1}}\left(g, h, k\right)
The parent() function is just telling us that Sage stores g as a symbolic representation (SR), as opposed to a real (RR) or rational (QQ).
parent(g)
\newcommand{\Bold}[1]{\mathbf{#1}}\text{SR}
S = matrix(SR,[[1,-4,7,g],[0,3,-5,h],[-2,5,-9,k]]) # !! Note the different system "SR". Needed for the symbolic computations using g,h, and k. !! S
\newcommand{\Bold}[1]{\mathbf{#1}}\left(147g035h259k\begin{array}{rrrr} 1 & -4 & 7 & g \\ 0 & 3 & -5 & h \\ -2 & 5 & -9 & k \end{array}\right)
S.add_multiple_of_row(2,0,2);S # add 2*row0 to row2
\newcommand{\Bold}[1]{\mathbf{#1}}\left(147g035h0352g+k\begin{array}{rrrr} 1 & -4 & 7 & g \\ 0 & 3 & -5 & h \\ 0 & -3 & 5 & 2 \, g + k \end{array}\right)
S.add_multiple_of_row(2,1,1);S
\newcommand{\Bold}[1]{\mathbf{#1}}\left(147g035h0002g+h+k\begin{array}{rrrr} 1 & -4 & 7 & g \\ 0 & 3 & -5 & h \\ 0 & 0 & 0 & 2 \, g + h + k \end{array}\right)
# In order for S to be consistent, 2g+k+k = 0 must hold.