FF505/FY505 – Computational Science
Week 5, Spring 2013 [pdf format]

Contents:

MATLAB Exercises

Work in small groups at the computer to solve the following exercises.

  1. Read the “Getting Started” page.
  2. Use MATLAB to generate random 4× 4 matrices A=rand(4) and B=rand(4). For each of the following, compute A1, A2, A3, and A4 as indicated and determine which of the matrices are equal (you can use MATLAB to test whether two matrices are equal by computing their difference):
    1. A1=A*B, A2=B*A, A3=(A′*B′)′, A4=(B′*A′)′
    2. A1=A′*B′, A2=(A*B)′, A3=B′*A′, A4=(B*A)′
    3. A1=inv(A*B), A2=inv(A)*inv(B), A3=inv(B*A), A4=inv(B)*inv(A)
    4. A1=(inv((A*B)′), A2=inv(A′*B′), A3=inv(A′)*inv(B′),
      A4=(inv(A)*inv(B))′
  3. Solve the following systems of equations:
    x1+x2+x3=0
    x1+2x2+ =1
    2x1+ +2x3=−1
            
    x1+x2+x3=0
    x1+2x2+x3=1
    x1x2+2x3=−1
  4. Solve the following system of equations:
    3x1+(2−i)x2+x3=4−i 
    (−2+3i)x1+(1−i)x2x3=−2−2i 
    (2+i)x1+3x2+(3+i)x3=56+92i 
  5. Set n=200 and generate an n × n matrix and two vectors in ℝn, both having integer entries, by setting
    Afloor (10*rand (n)); 
    bsum (A′)′; 
    zones (n,1)
    1. The exact solution of the system should be the vector z. Why?. Explain. One could compute the solution in MATLAB using the "\" operation or by computing A−1 and then multiplying A−1 times b. Let us compare these two computational methods for both speed and accuracy. One can use MATLAB’s tic and toc commands to measure the elapsed time for each computation. To do this, use the commands
       ticx=A \ btoc 
       ticy=inv(A)*btoc
      Which method is faster?

      To compare the accuracy of the two methods, we can measure how close the computed solutions x and y are to the exact solution z. Do this with the commands

       max(abs(xz)) 
       max(abs(yz))

      Which method produces the most accurate solution?

    2. Repeat part (a), using n=500 and n=1000.
  6. Find all solutions of the following system
    2x14x2+5x3=−4
    −4x12x2+3x3=4
    2x1+6x28x3=0