Académique Documents
Professionnel Documents
Culture Documents
Introduction to MATLAB
Matrices and matrix arithmetic.
Control structures
Program development using M-files.
Matlab Graphics
Practical applications
Introduction to MATLAB
Matlab means MATrix LABoratory program which was developed to solve linear equations
and eigenvalue problems. MATLAB offers a language for expressing problems and their
solutions both mathematically and visually. MATLAB is used in numeric computation,
algorithm development, data analysis, signal processing, engineering graphics and scientific
visualization.
Defining the variables
The general format for defining the variables is
x = 98
Valid
y_hc = 6 Table = 6576
Invalid
_y = 5
8sum = 767
2/4 = 0.5 -- right division
2\4 = 4/2 = 2 -- left division
Arithmetic Operations
Matrix Arithmetic
Division
C=A\B
C=
0.8608
0.5823
0.3165
1.3165 1.5823
0.9494 0.7468
0.6899 0.9494
Cube of a Matrix
A^3
ans =
39
-68
38
-68
319
-402
38
-402
1104
Cube Root of a Matrix
A^1/3
ans =
1.0000 -0.3333
0
-0.3333 2.0000 -0.6667
0
-0.6667 3.3333
Matrix Operations
Inverse matrix
To find inverse matrix for matrix(A)
>> inv(A)
ans =
0.3544 0.0633 0.0127
0.0633 0.1899 0.0380
0.0127 0.0380 0.1076
Transpose Matrix
A'
ans =
3 -1 0
-1 6 -2
0 -2 10
Determinant
>> b=[1 2 3;4 5 6;4 8 7];
>> c=det(b)
c=
15
Control Structures
If-else statement
If-else statement
a=3; b=3.7;
if a<b,
c=7;
else
c=5;
end;
c
c=
7
While Statement
While Statement
EDU i=0;
EDU while(i<5),
k=3*i,
i=i+1
end
k= 0
i= 1
k= 3
i= 2
k= 6
i= 3
k= 9
i= 4
k = 12
For Loop
For Loop
for i=2:2:10
i
end
i= 2
i= 4
i= 6
i= 8
i = 10
Matlab Graphics
Bar Chart
x=[1,2,3,5,6,8,10];
y=[22,33,44,37,56,38,45];
bar(x,y)
stem(x,y)
data = [ 2.6,4,5.6;
4.5,6,7;
3.7,5.9,9.8;
4.6,6.9,7.8];
plot3(data(:,1),data(:,2),data(:,3))
grid
>> xlabel('x'),ylabel('y'),zlabel('z')
2.
3.
4.
First step is to set the path of the working directory for the Matlab. For that, first
save the filename.m in a particular folder. Then set the path to that particular
folder.
First click on the set path menu on the command window
Then click on the browse menu and set the path towards to the folder in which
the m-file program has been saved.
Then click on path and add the folder path.
Functions in Matlab
Arrays in Matlab
Integration
Integration
x=0:3*pi;
y=sqrt(4*cos(2*x).^2 + sin(x).^2 + 1);
% Integrate y with respect to x
trapz(x,y)
ans =
16.6199
Double Integration
xmin = pi;
xmax = 2*pi;
ymin = 0;
ymax = pi;
result = dblquad(@integrnd,xmin,xmax,ymin,ymax)
Save as dou.m
Differentiation
syms x y
y= sin(x); dy = diff(y,'x')
dy =
cos(x)
y= (sin(x))^2
diff(y)
y=
sin(x)^2
ans =
2*sin(x)*cos(x)
Second Derivative
syms x y
y= (sin(x))^2+cos(x)
diff(y,x,2)
y=
sin(x)^2+cos(x)
ans =
2*cos(x)^2-2*sin(x)^2-cos(x)
syms x y z
y= (sin(z))^2+cos(x)
diff(y,'x')
y=
sin(z)^2+cos(x)
ans =
-sin(x)
References:
[1] Engineering Programming C, MATLAB, JAVA by Mark Austin and David Chancogne, John Wiley &
sons Inc, ISBN 0-471-00116-3
[2] Computer Applications Lab manual, Mechanical Engineering Dept