Matlab Tutorial 5: Linear Equations


Example 4

Let us exemplify the previous part:

x1‘(t)=x1(t) + 2x2(t)
x2‘(t)=2x1(t) -2x2(t)

We collect all coefficients in a matrix A=[1 2; 2 -2]. Calculate the eigenvalues and normalized eigenvectors for the coefficient matrix A.

b=     A=
  8    3 -2  4
 -5    5  8 -6
-17    9 -2  7

The general solution to the differential equations can then be written:

x1(t)= a1e-3t(-0.4472) + a2e2t (-0,8944) ( Eq1)
x2(t)= a1e3t (0.8944 ) + a2e2t (-0.4472) ( Eq2 )

The values of the constants a1 and a2 would have been known if we had the initial values of the differential equations. Other numerical values can be identified straight away from the X or D matrix. X has the 2 eigenvectors (columns) and D contains the eigenvalues (diagonal elements). Assume initial values for x1(t) and x2(t) and that these are:
x1(0)=1 and x2(0)=4.

This gives us an entire new equation system if the initial values are inserted in Eq1 and Eq2.

-0.4472 a1 – 0.8944 a2 = 1

0.8944 a1 – 0.4472 a2 = 4

Now we have an equation system with 2 unknowns and 2 equations. We can now find an exact solution. This is easily solved in Matlab.

X*a=b where a=[ a1 ; a2] , b=[ 1 ; 4] and X=[-0.4472 -0.8944; 0.8944 -0.4472]

The solution becomes:

>> X=inv(A)*b

It is now possible to plot the solutions x1(t) and x2(t) during the first second.

>> X=A\b

See the result of the plot below in Figure 2.

matlab-linear-equations-2

Solutions for Example 2

Sparse matrices are matrices with very few nonzero elements. This can be useful to know, when we are dealing with calculations and memory allocation. In Matlab we can request that a mxm – matrix be reduced to a sparse matrix.
Only the nonzero positions and the value need to be stored in the memory. Create an identity matrix!

X=
-36.7778
71.2778
65.2222

Check how many bytes are allocated in the memory (Workspace). Now use the sparse command to save memory.

>> B=sparse(A) % A has become a sparse matrix.
>> whos

Compare the memory allocation between A and B. What do you think? Also check the number of rows and columns in A and B. Does it matter, when it comes to time demand? Let us make a simple calculation. Multiply the matrix A with the scalar 3.

>> 3*A

We can simply measure the time to execute the command. This can be done using the commands tic and toc.

>> tic, 3*A, toc % tic starts clock and toc stops clock.

The time elapsed can be found in the command window. Compare it with the scalar multiplication of the sparse matrix below.

>> tic, 3*B, toc % Starts timer for the command 3*B.

We can see a clear difference, so it could matter if we must save both time and memory. In the table below we give some examples of Matlab commands that can be applied to sparse matrices.

speye(A): Gives a sparse matrix, with ones on the main diagonal.
sprand(A): Gives a sparse matrix with random numbers (0-1) on the nonzero positions.
sprandn(A): Same as above, but the elements are normally distributed (-1 to 1).
spones(A): Gives a sparse matrix with ones as the only nonzero elements.
full(A): Makes a full matrix from a sparse matrix A.
nnz(A): Gives the number of nonzero elements in the matrix A.
issparse(A): Returns 1, if the matrix is sparse or otherwise 0.
nonzeros(A): Returns a vector with all of the nonzero elements in A.
find(A): Gives an index for all nonzero elements in matrix A, regardless whether A is a sparse matrix or not.

Pages: 1 2 3 4 5