0% found this document useful (0 votes)
60 views

Very Basic Matlab PDF

This document provides a very basic introduction to MATLAB, covering topics such as matrices, vectors, arithmetic operations, complex numbers, and solving systems of linear equations. It explains how to define, access, and manipulate matrices and vectors, perform common arithmetic operations on them, and use the backslash operator or inverse to solve systems of linear equations.

Uploaded by

engrasheed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

Very Basic Matlab PDF

This document provides a very basic introduction to MATLAB, covering topics such as matrices, vectors, arithmetic operations, complex numbers, and solving systems of linear equations. It explains how to define, access, and manipulate matrices and vectors, perform common arithmetic operations on them, and use the backslash operator or inverse to solve systems of linear equations.

Uploaded by

engrasheed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Very Basic MATLAB

Peter J. Olver Marcel Arndt


September 10, 2007

1 Matrices
Type your matrix as follows. Use , or space to separate entries, and ; or
return after each row.
>> A = [4 5 6 -9;5 0 -3 6;7 8 5 0; -1 4 5 1]
or
>> A = [4,5,6,-9;5,0,-3,6;7,8,5,0;-1,4,5,1]
or
>> A = [ 4 5 6 -9
5 0 -3 6
7 8 5 0
-1 4 5 1 ]
The output will be:
A =
4 5 6 -9
5 0 -3 6
7 8 5 0
-1 4 5 1
You can identify an entry of a matrix by
>> A(2,3)
ans =
-3
A colon : indicates all entries in a row or column
>> A(2,:)
ans =
5 0 -3 6
>> A(:,3)
ans =

1
6
-3
5
5
You can use these to modify entries
>> A(2,3) = 10
A =
4 5 6 -9
5 0 10 6
7 8 5 0
-1 4 5 1
or to add in rows or columns
>> A(5,:) = [0 1 0 -1]
A =
4 5 6 -9
5 0 10 6
7 8 5 0
-1 4 5 1
0 1 0 -1
or to delete them
>> A(:,2) = []
A =
4 6 -9
5 10 6
7 5 0
-1 5 1
0 0 -1

Accessing Part of a Matrix


>> A = [4,5,6,-9;5,0,-3,6;7,8,5,0;-1,4,5,1]
A =
4 5 6 -9
5 0 -3 6
7 8 5 0
-1 4 5 1
>> A([1 3],:)
ans =
4 5 6 -9
7 8 5 0
>> A(:,2:4)
ans =

2
5 6 -9
0 -3 6
8 5 0
4 5 1
>> A(2:3,1:3)
ans =
5 0 -3
7 8 5

Switching two rows in a matrix


>> A([3 1],:) = A([1 3],:)
A =
7 8 5 0
5 0 -3 6
4 5 6 -9
-1 4 5 1

Special matrices
Zero matrix:
>> zeros(2,3)
ans =
0 0 0
0 0 0
>> zeros(3)
ans =
0 0 0
0 0 0
0 0 0
Identity Matrix:
>> eye(3)
ans =
1 0 0
0 1 0
0 0 1
Matrix of Ones:
>> ones(2,3)
ans =
1 1 1
1 1 1

Random Matrix:

3
>> A = rand(2,3)
A =
0.9501 0.4860 0.4565
0.2311 0.8913 0.0185
Note that the random entries all lie between 0 and 1.

Transpose of a Matrix
>> A = [4,5,6,-9;5,0,-3,6;7,8,5,0;-1,4,5,1]
A =
4 5 6 -9
5 0 -3 6
7 8 5 0
-1 4 5 1
>> transpose(A)
ans =
4 5 7 -1
5 0 8 4
6 -3 5 5
-9 6 0 1
>> A
ans =
4 5 7 -1
5 0 8 4
6 -3 5 5
-9 6 0 1

Diagonal of a Matrix
>> diag(A)
ans =
4
0
5
1

Vectors
Vectors are matrices of size 1 along one dimension.
Row vector:
>> v = [1 2 3 4 5]
v =
1 2 3 4 5
Column vector:

4
>> v = [1;2;3;4;5]
v =
1
2
3
4
5
or use transpose operation
>> v = [1 2 3 4 5]
v =
1
2
3
4
5

Forming Other Vectors


>> v = 1:5
v =
1 2 3 4 5
>> v = 10:-2:0
v =
10 8 6 4 2 0
>> v = linspace(0,1,6)
v =
0 0.2000 0.4000 0.6000 0.8000 1.0000

Important: To avoid output, particularly of large matrices, use a semicolon ;


at the end of the line:

>> v = linspace(0,1,100);

gives a row vector whose entries are 100 equally spaced points from 0 to 1.

Size of a Matrix
>> A = [4 5 6 -9 7;5 0 -3 6 -2;7 8 5 0 5 ; -1 4 5 1 -9 ]
A =
4 5 6 -9 7
5 0 -3 6 -2
7 8 5 0 5
-1 4 5 1 -9
>> size(A)
ans =

5
4 5
>> [m,n] = size(A)
m =
4
n =
5
>> size(A,1)
ans =
4
>> size(A,2)
ans =
5

2 Output Formats
The command format is used to change output format. The default is

>> format short


>> pi
ans =
3.1416
>> format long
>> pi
ans =
3.14159265358979
>> format rat
>> pi
ans =
355/113

This allows you to work in rational arithmetic and gives the best rational
approximation to the answer. Lets return to the default.
>> format short
>> pi
ans =
3.1416

3 Arithmetic operators
+ Matrix addition.
A + B adds matrices A and B. The matrices A and B must have the same
dimensions unless one is a scalar ( 1 1 matrix). A scalar can be added to
anything.

6
>> A = [4,5,6,-9;5,0,-3,6;7,8,5,0;-1,4,5,1]
A =
4 5 6 -9
5 0 -3 6
7 8 5 0
-1 4 5 1
>> B = [9 2 4 -9;1 4 -2 -6;8 1 7 0; -3 -4 5 9 ]
B =
9 2 4 -9
1 4 -2 -6
8 1 7 0
-3 -4 5 9
>> A + B
ans =
13 7 10 -18
6 4 -5 0
15 9 12 0
-4 0 10 10

- Matrix subtraction.
A - B subtracts matrix A from B. Note that A and B must have the same
dimensions unless one is a scalar.
>> A - B
ans =
-5 3 2 0
4 -4 -1 12
-1 7 -2 0
2 8 0 -8

Scalar multiplication
>> 3*A - 4*B
ans =
-24 7 2 9
11 -16 -1 42
-11 20 -13 0
9 28 -5 -33

Matrix multiplication.
A*B is the matrix product of A and B. A scalar (a 1-by-1 matrix) may multiply
anything. Otherwise, the number of columns of A must equal the number of
rows of B.
>> A * B

7
ans =
116 70 3 -147
3 -17 29 9
111 51 47 -111
32 15 28 -6
Note that two matrices must be compatible before we can multiply them.
The order of multiplication is important!
>> v = [1 2 3 4]
v =
1 2 3 4
>> w = [1;2;3;4]
w =
1
2
3
4
>> v * w
ans =
30
>> w * v
ans =
1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16

. Array multiplication
A.*B denotes element-by-element multiplication. A and B must have the same
dimensions unless one is a scalar. A scalar can be multiplied into anything.
>> a = [3 4 5 6 7 8 9]
a =
3 4 5 6 7 8 9
>> b = [8 6 2 4 5 6 -1]
b =
8 6 2 4 5 6 -1
>> a .* b
ans =
24 24 10 24 35 48 -9

Matrix power.
C = An is A to the n-th power if n is a scalar and A is square. If n is an integer
greater than one, the power is computed by repeated multiplication.

8
>> A = [4 5 6 -9;5 0 -3 6;7 8 5 0; -1 4 5 1 ]
A =
4 5 6 -9
5 0 -3 6
7 8 5 0
-1 4 5 1
>> A^3
ans =
501 352 351 -651
451 169 -87 174
1103 799 533 -492
445 482 413 -182

. Array power.
C = A.B denotes element-by-element powers. A and B must have the same
dimensions unless one is a scalar. A scalar can go in either position.
>> A = [8 6 2 4 5 6 -1 ]
A =
8 6 2 4 5 6 -1
>> A.^3
ans =
512 216 8 64 125 216 -1

Length of a Vector, Norm of a Vector, Dot Product


>> u = [8 -7 6 5 4 -3 2 1 9]
u =
8 -7 6 5 4 -3 2 1 9
>> length(u)
ans =
9
>> norm(u)
ans =
16.8819
>> v = [9 -8 7 6 -4 5 0 2 -4]
v =
9 -8 7 6 -4 5 0 2 -4
>> dot(u,v)
ans =
135
>> u*v
ans =
135

9
4 Complex Numbers
>> u = [2-3i, 4+6i,-3,+2i]
u =
2.0000- 3.0000i 4.0000+ 6.0000i -3.0000 0+ 2.0000i
>> conj(u)
ans =
2.0000+ 3.0000i 4.0000- 6.0000i -3.0000 0- 2.0000i

Hermitian transpose:
>> u
ans =
2.0000+ 3.0000i
4.0000- 6.0000i
-3.0000
0- 2.0000i
Other operations:
>> norm(u)
ans =
8.8318
>> dot(u,u)
ans =
78
>> sqrt(ans)
ans =
8.8318
>> u*u
ans =
78

5 Solving Systems of Linear Equations


The best way of solving a system of linear equations

Ax = b

in MatLab is to use the backslash operation \ (backwards division)

>> A = [1 2 3;-1 0 2;1 3 1]


A =
1 2 3
-1 0 2
1 3 1
>> b = [1; 0; 0]

10
b =
1
0
0
>> x = A \ b
x =
0.6667
-0.3333
0.3333

The backslash is implemented by using Gaussian elimination with partial piv-


oting. An alternative, but less accurate, method is to compute inverses:

>> B = inv(A)
B =
0.6667 -0.7778 -0.4444
-0.3333 0.2222 0.5556
0.3333 0.1111 -0.2222

or
>> B = A^(-1)
B =
0.6667 -0.7778 -0.4444
-0.3333 0.2222 0.5556
0.3333 0.1111 -0.2222
>> x = B * b
x =
0.6667
-0.3333
0.3333
Another method is to use the command rref:
To solve the following system of linear equations:

x1 + 4x2 2x3 + x4 = 2
2x1 + 9x2 3x3 2x4 = 5
x1 + 5x2 x4 = 3
3x1 + 14x2 + 7x3 2x4 = 6

we form the augmented matrix:


>> A = [1,4,-2,3,2; 2,9,-3,-2,5; 1,5,0,-1,3; 3,14,7,-2,6]
A =
1 4 -2 3 2
2 9 -3 -2 5
1 5 0 -1 3

11
3 14 7 -2 6
>> rref(A)
ans =
1.0000 0 0 0 -5.0256
0 1.0000 0 0 1.6154
0 0 1.0000 0 -0.2051
0 0 0 1.0000 0.0513
The solution is: x1 = 5.0256, x2 = 1.6154, x3 = 0.2051, x4 = 0.0513.
Case 1: Infinitely many solutions:

>> A = [-2 2 -2;1 -1 1; 2 -2 2]


A =
-2 2 -2
1 -1 1
2 -2 2
>> b = [-8; 4; 8]
b =
-8
4
8
>> A \ b
Warning: Matrix is singular to working precision.
ans =
NaN
NaN
NaN

MatLab is unable to find the solutions. In this case, we can apply rref to the
augmented matrix.
>> C = [A b]
C =
-2 2 -2 -8
1 -1 1 4
2 -2 2 8
>> rref(C)
ans =
1 -1 1 4
0 0 0 0
0 0 0 0
Conclusion: There are infinitely many solutions since row 2 and row 3 are all
zeros.
Case 2: No solutions:
>> A = [-2 1; 4 -2]
A =

12
-2 1
4 -2
>> b = [5; -1]
b =
5
-1
>> A \ b
Warning: Matrix is singular to working precision.
ans =
Inf
Inf
>> C = [A b]
C =
-2 1 5 4 -2 -1
>> rref(C)
ans =
1.0000 -0.5000 0
0 0 1.0000
Conclusion: Row 2 is not all zeros, and the system is incompatible.
Important: If the coefficient matrix A is rectangular (not square) then A \ b
gives the least squares solution (relative to the Euclidean norm) to the system
A x = b. If the solution is not unique, it gives the least squares solution x with
minimal Euclidean norm.

>> A = [1 1;2 1;-5, -1]


A =
1 1
2 1
-5 -1
>> b = [1;1;1]
b =
1
1
1
>> A \ b
ans =
-0.5385
1.7692

If you want the least squares solution in the square case, one trick is to add an
extra equation 0 = 0 to make the coefficient matrix rectangular:

>> A = [-2 2 -2;1 -1 1; 2 -2 2]


A =
-2 2 -2
1 -1 1

13
2 -2 2
>> b=[-8; 4; 8]
b =
-8
4
8
>> A \ b
Warning: Matrix is singular to working precision.
ans =
Inf
Inf
Inf
>> A(4,:) = 0
A =
-2 2 -2
1 -1 1
2 -2 2
0 0 0
>> b(4) = 0
b =
-8
4
8
0
>> A \ b
Warning: Rank deficient, rank = 1 tol = 2.6645e-15.
ans =
4.0000
0
0

6 Plotting Functions
Functions can be stored as vectors. Namely, a vector x and a vector y of the
same length correspond to the sampled function values (xi , yi ).
To plot the function y = x2 .5 x first enter an array of independent variables:
>> x = linspace(0,1,25)
>> y = x.^2 - .5*x;
>> plot(x,y)
The plot shows up in a new window. To plot in a different color, use
>> plot(x,y,r)
where the character string r means red. Use the helpwindow to see other
options.

14
To plot graphs on top of each other, use hold on.
>> hold on
>> z = exp(x);
>> plot(x,z)
>> plot(x,z,g)
hold off will stop simultaneous plotting. Alternatively, use
>> plot(x,y,r,x,z,g)

Surface Plots
Here x and y must give a regtangular array, and z is a matrix whose entries are
the values of the function at the array points.

>> x =linspace(-1,1,40); y = x;
>> z = x * (y.^2);
>> surf(x,y,z)

Typing the command

>> rotate3d

will allow you to use the mouse interactively to rotate the graph to view it from
other angles.

7 Functions, Subroutines, M-Files


Simple functions can be declared as anonymous functions:
>> f = @(x) 1./x
f =
@(x)1./x
>> f(5)
ans =
0.2000
>> f(1:5)
ans =
1.0000 0.5000 0.3333 0.2500 0.2000
For more complex functions or subroutines, use M-Files. Create a file with the
name of the subroutine and the suffix .m. For the trapezoidal rule
" n1
#
h X
Tn := f (a) + 2 f (a + ih) + f (b)
2 i=1

use

15
>> edit trapezoidal.m
An editor pops up. Enter the code of the subroutine:
function integral = trapezoidal(f, a, b, n)
h = (b-a)/n;
integral = 0;
for i=1:(n-1)
integral = integral + f(a+i*h);
end
integral = 0.5 * h * ( f(a) + 2*integral + f(b) );
Save the file.
>> trapezoidal(f, 1, 2, 4)
ans =
0.6970
The subroutine gets much faster for large numbers n by avoiding the loop in
your M-File. Save this code as trapezoidal2.m:
function integral = trapezoidal2(f, a, b, n)
h = (b-a)/n;
integral = sum(f(a+(1:(n-1))*h));
integral = 0.5 * h * ( f(a) + 2*integral + f(b) );
Multiple values can be returned as follows. Each entry can be a matrix itself.
Save this code as multreturn.m:

function [a,b,c] = multreturn(x,y)


a = x+y;
b = x-y;
c = rand(2,3);
Then

>> [u,v,w] = multreturn(6,2)


u =
8
v =
4
w =
0.6557 0.8491 0.6787
0.0357 0.9340 0.7577

16

You might also like