Introduction To Matlab
Introduction To Matlab
1.1 Introduction
1.2 Matrices
Matrices are the fundamental object of MATLAB and are particularly important
in this book. Matrices can be created in MATLAB in many ways, the simplest one
obtained by the commands
>> A=[1 2 3;4 5 6;7 8 9]
A =
1 2 3
4 5 6
7 8 9
Note the semi-colon at the end of each matrix line. We can also generate matrices
by pre-defined functions, such as random matrices
>> rand(3)
ans =
0.8147 0.9134 0.2785
0.9058 0.6324 0.5469
0.1270 0.0975 0.9575
A.J.M. Ferreira, MATLAB Codes for Finite Element Analysis: 1
Solids and Structures, Solid Mechanics and Its Applications 157,
c Springer Science+Business Media B.V. 2009
2 1 Short introduction to MATLAB
We can add, subtract, multiply, and transpose matrices. For example, we can
obtain a matrix C = A + B, by the following commands
>> a=rand(4)
a =
0.2769 0.6948 0.4387 0.1869
0.0462 0.3171 0.3816 0.4898
0.0971 0.9502 0.7655 0.4456
0.8235 0.0344 0.7952 0.6463
>> b=rand(4)
b =
0.7094 0.6551 0.9597 0.7513
0.7547 0.1626 0.3404 0.2551
0.2760 0.1190 0.5853 0.5060
0.6797 0.4984 0.2238 0.6991
>> c=a+b
c =
0.9863 1.3499 1.3985 0.9381
0.8009 0.4797 0.7219 0.7449
0.3732 1.0692 1.3508 0.9515
1.5032 0.5328 1.0190 1.3454
The matrices can be multiplied, for example E = A ∗ D, as shown in the following
example
>> d=rand(4,1)
d =
0.8909
0.9593
0.5472
0.1386
>> e=a*d
e =
1.1792
0.6220
1.5 Matrix functions 3
1.4787
1.2914
The transpose of a matrix is given by the apostrophe, as
>> a=rand(3,2)
a =
0.1493 0.2543
0.2575 0.8143
0.8407 0.2435
>> a’
ans =
0.1493 0.2575 0.8407
0.2543 0.8143 0.2435
1.4 Statements
Some examples of such functions are given in the following commands (here we
build matrices by blocks)
>> [eye(3),diag(eye(3)),rand(3)]
ans =
1.0000 0 0 1.0000 0.9293 0.2511 0.3517
0 1.0000 0 1.0000 0.3500 0.6160 0.8308
0 0 1.0000 1.0000 0.1966 0.4733 0.5853
If there are many options, it may better to use switch instead. For instance:
switch units
case ’length’
disp(’meters’)
case ’volume’
disp(’cubic meters’)
case ’time’
disp(’hours’)
otherwise
disp(’not interested’)
end
>> f=[1 2]
f =
1 2
>> for i=3:10;f(i)=f(i-1)+f(i-2);end;
>> f
f =
1 2 3 5 8 13 21 34 55 89
x =
1
x =
1 4
x =
1 4 9
x =
1 4 9 16
and in inverse form
>> x = []; for i = 4:-1:1, x=[x,i^2], end
x =
16
x =
16 9
x =
16 9 4
x =
16 9 4 1
Note the initial values of x = [] and the possibility of decreasing cycles.
1.8 Relations
ans =
1
ans =
0
ans =
0
The same is obtained for matrices, as in
>> a = rand(5), b = triu(a), a == b
a =
0.1419 0.6557 0.7577 0.7060 0.8235
0.4218 0.0357 0.7431 0.0318 0.6948
0.9157 0.8491 0.3922 0.2769 0.3171
0.7922 0.9340 0.6555 0.0462 0.9502
0.9595 0.6787 0.1712 0.0971 0.0344
b =
0.1419 0.6557 0.7577 0.7060 0.8235
0 0.0357 0.7431 0.0318 0.6948
0 0 0.3922 0.2769 0.3171
0 0 0 0.0462 0.9502
0 0 0 0 0.0344
ans =
1 1 1 1 1
0 1 1 1 1
0 0 1 1 1
0 0 0 1 1
0 0 0 0 1
Some MATLAB functions are applied to scalars only. Some of those functions are
listed in table 1.4. Note that such functions can be applied to all elements of a
vector or matrix, as in
>> a=rand(3,4)
a =
0.4387 0.7952 0.4456 0.7547
>> x=1:10
x =
1 2 3 4 5 6 7 8 9 10
>> sum(x)
ans =
55
>> mean(x)
ans =
5.5000
>> max(x)
ans =
10
>> A=rand(3)
A =
0.8147 0.9134 0.2785
0.9058 0.6324 0.5469
0.1270 0.0975 0.9575
>> y=eig(A)
y =
-0.1879
1.7527
0.8399
>> [V,D]=eig(A)
V =
0.6752 -0.7134 -0.5420
-0.7375 -0.6727 -0.2587
-0.0120 -0.1964 0.7996
D =
-0.1879 0 0
0 1.7527 0
0 0 0.8399
1.12 Submatrix
>> x=1:8
x =
1 2 3 4 5 6 7 8
or using increments
>> x=1.2:0.5:3.7
x =
1.2000 1.7000 2.2000 2.7000 3.2000 3.7000
This sort of vectorization programming is quite efficient, no for/end cycles are used.
This efficiency can be seen in the generation of a table of sines,
>> x=0:pi/2:2*pi
x =
0 1.5708 3.1416 4.7124 6.2832
>> b=sin(x)
b =
0 1.0000 0.0000 -1.0000 -0.0000
>> [x’ b’]
ans =
0 0
1.5708 1.0000
3.1416 0.0000
4.7124 -1.0000
6.2832 -0.0000
The colon can also be used to access one or more elements from a matrix, where
each dimension is given a single index or vector of indices. A block is then extracted
from the matrix, as illustrated next.
>> a=rand(3,4)
a =
0.6551 0.4984 0.5853 0.2551
0.1626 0.9597 0.2238 0.5060
0.1190 0.3404 0.7513 0.6991
>> a(2,3)
ans =
0.2238
1.12 Submatrix 11
>> a(1:2,2:3)
ans =
0.4984 0.5853
0.9597 0.2238
>> a(1,end)
ans =
0.2551
>> a(1,:)
ans =
0.6551 0.4984 0.5853 0.2551
>> a(:,3)
ans =
0.5853
0.2238
0.7513
It is interesting to note that arrays are stored linearly in memory, from the first
dimension, second, and so on. So we can in fact access vectors by a single index,
as show below.
>> a
a =
1 2 3
4 5 6
9 8 7
>> b
b =
1 2 3
4 5 6
>> b(1,:)=a(1,:)
b =
1 2 3
4 5 6
>> b(1,:)=a(2,:)
b =
4 5 6
4 5 6
>> b(:,2)=[]
b =
4 6
4 6
>> a(3,:)=0
a =
1 2 3
4 5 6
0 0 0
>> b(3,1)=20
b =
4 6
4 6
20 0
As you noted in the last example, we can insert one element in matrix B, and
MATLAB automatically resizes the matrix.
Logical indexing arise from logical relations, resulting in a logical array, with ele-
ments 0 or 1.
>> a
a =
1 2 3
4 5 6
0 0 0
1.14 M-files, scripts and functions 13
>> a>2
ans =
0 0 1
1 1 1
0 0 0
Then we can use such array as a mask to modify the original matrix, as shown
next.
>> a(ans)=20
a =
1 2 20
20 20 20
0 0 0
This will be very useful in finite element calculations, particularly when imposing
boundary conditions.
A M-file is a plain text file with MATLAB commands, saved with extension .m.
The M-files can be scripts of functions. By using the editor of MATLAB we can
insert comments or statements and then save or compile the m-file. Note that
the percent sign % represents a comment. No statement after this sign will be
executed. Comments are quite useful for documenting the file.
M-files are useful when the number of statements is large, or when you want to
execute it at a later stage, or frequently, or even to run it in background.
A simple example of a script is given below.
% program 1
% programmer: Antonio ferreira
% date: 2008.05.30
% purpose : show how M-files are built
a=rand(3,4);
b=sin(a);
Functions act like subroutines in fortran where a particular set of tasks is
performed. A typical function is given below, where in the first line we should
name the function and give the input parameters (m,n,p) in parenthesis and the
output parameters (a,b,c) in square parenthesis.
function [a,b,c] = antonio(m,n,p)
14 1 Short introduction to MATLAB
a = hilb(m);
b= magic(n);
c= eye(m,p);
We then call this function as
>> [a,b,c]=antonio(2,3,4)
producing
>> [a,b,c]=antonio(2,3,4)
a =
1.0000 0.5000
0.5000 0.3333
b =
8 1 6
3 5 7
4 9 2
c =
1 0 0 0
0 1 0 0
It is possible to use only some output parameters.
>> [a,b]=antonio(2,3,4)
a =
1.0000 0.5000
0.5000 0.3333
b =
8 1 6
3 5 7
4 9 2
1.15 Graphics
1.15.1 2D plots
Using the command plot we can produce simple 2D plots in a figure, using two
vectors with x and y coordinates. A simple example
x = -4:.01:4; y = sin(x); plot(x,y)
producing the plot of figure 1.1.
1.15 Graphics 15
−1
−4 −3 −2 −1 0 1 2 3 4
We can insert a title, legends, modify axes etc., as shown in table 1.7.
By using hold on we can produce several plots in the same figure. We can also
modify colors of curves or points, as in
>> x=0:.01:2*pi; y1=sin(x); y2=sin(2*x); y3=sin(4*x);
>> plot(x,y1,’--’,x,y2,’:’,x,y3,’+’)
producing the plot of figure 1.2.
1.15.2 3D plots
As for 2D plots, we can produce 3D plots with plot3 using x, y, and z vectors.
For example
t=.01:.01:20*pi; x=cos(t); y=sin(t); z=t.^3; plot3(x,y,z)
produces the plot illustrated in figure 1.3.
The next statements produce the graphic illustrated in figure 1.4.
16 1 Short introduction to MATLAB
1.5
0.5
0
1
0.5 1
0 0.5
0
−0.5 −0.5
−1 −1
>> [xx,yy]=meshgrid(x,x);
>> z=exp(-xx.^2-yy.^2);
>> surf(xx,yy,z,gradient(z))
0.8
0.6
0.4
0.2
0
2
1 2
1
0
0
−1 −1
−2 −2
>> a=rand(3)
a =
0.8909 0.1386 0.8407
0.9593 0.1493 0.2543
0.5472 0.2575 0.8143
>> b=rand(3,1)
b =
0.2435
0.9293
0.3500
The solution vector X can be easily evaluated by using the backslash command,
>> x=a\b
x =
0.7837
2.9335
-1.0246
Consider two matrices (for example a stiffness matrix and a mass matrix), for
which we wish to calculate the generalized eigenproblem.
>> a=rand(4)
a =
0.1966 0.3517 0.9172 0.3804
0.2511 0.8308 0.2858 0.5678
0.6160 0.5853 0.7572 0.0759
0.4733 0.5497 0.7537 0.0540
>> b=rand(4)
18 1 Short introduction to MATLAB
b =
0.5308 0.5688 0.1622 0.1656
0.7792 0.4694 0.7943 0.6020
0.9340 0.0119 0.3112 0.2630
0.1299 0.3371 0.5285 0.6541
>> [v,d]=eig(a,b)
v =
0.1886 -0.0955 1.0000 -0.9100
0.0180 1.0000 -0.5159 -0.4044
-1.0000 -0.2492 -0.2340 0.0394
0.9522 -0.8833 0.6731 -1.0000
d =
-4.8305 0 0 0
0 -0.6993 0 0
0 0 0.1822 0
0 0 0 0.7628
The MATLAB function eig can be applied to the generalized eigenproblem, pro-
ducing matrix V, each column containing an eigenvector, and matrix D, containing
the eigenvalues at its diagonal. If the matrices are the stiffness and the mass ma-
trices then the eigenvectors will be the modes of vibration and the eigenvalues will
be the square roots of the natural frequencies of the system.