Octave Examples for Solving Linear Algebra Equations


Linear Algebra Beginner Examples:

MathLab> octave
Octave, version 1.1.1.
Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton.
This is free software with ABSOLUTELY NO WARRANTY.
For details, type `warranty'.


Matrix Input:

octave:1> # 
octave:1> # Sample Input of 4X4 Matrix with Rows Separated by Semicolons,
octave:1> #    and elements in each row Separated by Commas:
octave:1> A=[0,2,0,1;2,2,3,2;4,-3,0,1.;6,1,-6,-5]

A =

   0   2   0   1
   2   2   3   2
   4  -3   0   1
   6   1  -6  -5

octave:2> # 
octave:2> # Sample Input of Column Vector for Matrix Multiplication:
octave:2> B=[0;-2;-7;6]

B =

   0
  -2
  -7
   6


Matrix Solve for A*ANS=B:

octave:3> # 
octave:3> # Matrix Solve is A\B in Octave for A^(-1)*B in Math:
octave:3> #     (Back Slash (\) ALERT: "\" for "divided into")
octave:3> A\B

ans =

  -0.50000
   1.00000
   0.33333
  -2.00000

octave:4> # Note that "# " at Beginning of Line is Only a Comment!

Matrix Random Input:

octave:4> # 
octave:4> # Another Example using Random Function "rand" to Get Test Matrix:
octave:4> C=rand(5,5)

C =

  0.0532493  0.4991650  0.0078347  0.5046233  0.0838328
  0.0455471  0.2675484  0.9240972  0.1908562  0.0828382
  0.2804574  0.9667465  0.0979988  0.8394614  0.4128971
  0.1344571  0.9892287  0.9268662  0.4925555  0.1661428
  0.0068033  0.2083562  0.1163075  0.7727603  0.3052436

octave:5> # 
octave:5> # Still Another Example using "rand" to Get Test Vector:
octave:5> D=rand(5,1)

D =

  0.8632258
  0.2418298
  0.0467658
  0.3533209
  0.0082342


More Matrix Solve, C*ANS=D:

octave:6> # 
octave:6> # Solving C*X=D for the Vector X:
octave:6> C\D

ans =

   5.49438
  -1.02725
   0.31745
   3.55790
  -8.52249


Matrix Norms:

octave:7> # 
octave:7> # 'norm(A)' means 2-norm; else use norm(A,q) for q = 1, 2 or 'Inf':
octave:7> norm(C)

ans = 2.6476

octave:8> norm(C,2)

ans = 2.6476

octave:9> norm(C,1)

ans = 2.9868

octave:10> norm(C,Inf)

ans = 3.1385


Condition Numbers:

octave:11> # 
octave:11> # Condition Number Examples, 2-norm assumed and singular values used:
octave:11> # 'cond(A)' allows no other norms and 'cond(A,1)' gives error!
octave:11> cond(A)

ans = 9.7626

octave:12> cond(C)

ans = 55.430


Inverse Matrix Applied to Condition Number:

octave:13> # 
octave:13> # To get condition number for other norms, use the definition: 
octave:13> cond1A=norm(A,1)*norm(inverse(A),1)

cond1A = 16.513


Determinants:

octave:14> # 
octave:14> # Determinant Examples:
octave:14> det(A)

ans = -234

octave:15> det(C)

ans = -0.0073705


EigenValue Problems:

octave:16> # 
octave:16> # Eigenvalue and Eigenvector Examples:
octave:16> #    (Remark: For Long Output, Press "SPACE" when ":" for more?)
octave:16> [EVECT,EVAL]=eig(A)

EVECT =

 Columns 1 through 3:

   0.13288 + 0.00000i  -0.47184 + 0.00000i   0.08069 - 0.07887i
   0.15971 + 0.00000i  -0.78385 + 0.00000i  -0.27971 + 0.17554i
   0.18827 + 0.00000i   0.01455 + 0.00000i  -0.42233 - 0.43165i
  -0.95989 + 0.00000i  -0.40340 + 0.00000i   0.71661 + 0.00000i

 Column 4:

   0.08069 + 0.07887i
  -0.27971 - 0.17554i
  -0.42233 + 0.43165i
   0.71661 + 0.00000i

EVAL =

 Columns 1 through 3:

  -4.82011 + 0.00000i   0.00000 + 0.00000i   0.00000 + 0.00000i
   0.00000 + 0.00000i   4.17748 + 0.00000i   0.00000 + 0.00000i
   0.00000 + 0.00000i   0.00000 + 0.00000i  -1.17869 + 3.19871i
   0.00000 + 0.00000i   0.00000 + 0.00000i   0.00000 + 0.00000i

 Column 4:

   0.00000 + 0.00000i
   0.00000 + 0.00000i
   0.00000 + 0.00000i
  -1.17869 - 3.19871i

octave:17> #
octave:17> # (Remark: "eig(A)" alone give only eigenvalues in a column,
octave:17> #      but "[Evect,Eval]=eig(A)" gives eigenvalues in a diagonal
octave:17> #      array under the name "Eval" or chosen alias.)
octave:17> eig(C)

ans =

   2.07361 + 0.00000i
   0.08970 + 0.00000i
  -0.66921 + 0.00000i
  -0.13875 + 0.19989i
  -0.13875 - 0.19989i

octave:18> [v,lambda]=eig(C)

v =

 Columns 1 through 3:

   0.26024 + 0.00000i  -0.62139 + 0.00000i   0.43779 + 0.00000i
   0.36832 + 0.00000i   0.04759 + 0.00000i  -0.64186 + 0.00000i
   0.54959 + 0.00000i  -0.00112 + 0.00000i   0.62553 + 0.00000i
   0.61130 + 0.00000i  -0.21678 + 0.00000i  -0.01319 + 0.00000i
   0.34768 + 0.00000i   0.75141 + 0.00000i   0.06999 + 0.00000i

 Columns 4 and 5:

   0.58836 + 0.00000i   0.58836 + 0.00000i
  -0.34870 - 0.07804i  -0.34870 + 0.07804i
   0.10391 - 0.07563i   0.10391 + 0.07563i
   0.08158 + 0.40094i   0.08158 - 0.40094i
   0.22797 - 0.53875i   0.22797 + 0.53875i

lambda =

 Columns 1 through 3:

   2.07361 + 0.00000i   0.00000 + 0.00000i   0.00000 + 0.00000i
   0.00000 + 0.00000i   0.08970 + 0.00000i   0.00000 + 0.00000i
   0.00000 + 0.00000i   0.00000 + 0.00000i  -0.66921 + 0.00000i
   0.00000 + 0.00000i   0.00000 + 0.00000i   0.00000 + 0.00000i
   0.00000 + 0.00000i   0.00000 + 0.00000i   0.00000 + 0.00000i

 Columns 4 and 5:

   0.00000 + 0.00000i   0.00000 + 0.00000i
   0.00000 + 0.00000i   0.00000 + 0.00000i
   0.00000 + 0.00000i   0.00000 + 0.00000i
  -0.13875 + 0.19989i   0.00000 + 0.00000i
   0.00000 + 0.00000i  -0.13875 - 0.19989i


LU Decomposition:

octave:19> # 
octave:19> # LU Decomposition Examples:
octave:19> [L,U,P]=lu(A)

L =

   1.00000   0.00000   0.00000   0.00000
   0.66667   1.00000   0.00000   0.00000
   0.33333  -0.45455   1.00000   0.00000
   0.00000  -0.54545   0.32000   1.00000

U =

   6.00000   1.00000  -6.00000  -5.00000
   0.00000  -3.66667   4.00000   4.33333
   0.00000   0.00000   6.81818   5.63636
   0.00000   0.00000   0.00000   1.56000

P =

  0  0  0  1
  0  0  1  0
  0  1  0  0
  1  0  0  0

octave:20> # 
octave:20> # Note that P is the Pivot Permutation Matrix;
octave:20> #    And if you use the short Form "lu(A)" you get a difficult 
octave:20> #    to understand Permuted LU Decomposition Answer:
octave:20> lu(A)

ans =

   0.00000  -0.54545   0.32000   1.00000
   0.33333  -0.45455   1.00000   0.00000
   0.66667   1.00000   0.00000   0.00000
   1.00000   0.00000   0.00000   0.00000

octave:21> [L,U,P]=lu(C)

L =

   1.00000   0.00000   0.00000   0.00000   0.00000
   0.47942   1.00000   0.00000   0.00000   0.00000
   0.16240   0.21026   1.00000   0.00000   0.00000
   0.02426   0.35170  -0.27037   1.00000   0.00000
   0.18987   0.60031  -0.74529   0.43497   1.00000

U =

   0.28046   0.96675   0.09800   0.83946   0.41290
   0.00000   0.52575   0.87988   0.09010  -0.03181
   0.00000   0.00000   0.72317   0.03558   0.02247
   0.00000   0.00000   0.00000   0.73033   0.31249
   0.00000   0.00000   0.00000   0.00000  -0.09464

P =

  0  0  1  0  0
  0  0  0  1  0
  0  1  0  0  0
  0  0  0  0  1


Quit:

octave:22> quit

MathLab> 



Web Source: http://www.math.uic.edu/~hanson/Octave/OctaveLinearAlgebra.html

Email Comments or Questions to hanson@uic.edu