Computer Fundamentals-Lab manual-MATLAB
Computer Fundamentals-Lab manual-MATLAB
Intro to MATLAB
C O M P U T E R F U N DA M E N TA L S
INTRO TO MATLAB
2009-ME-383
Kamran Ali Ahmed
MATLAB
MATLAB stands for "MATrix LABoratory" and is a numerical computing environment
and fourth-generation programming language. Developed by The MathWorks, MATLAB
allows matrix manipulations, plotting of functions and data, implementation of algorithms,
creation of user interfaces, and interfacing with programs written in other languages,
including C, C++, and FORTRAN. [1]
VARIABLES
2
Intro to MATLAB
Just like in C++, the variables are created with any name and their value is defined
by any value preceded by an equality sign.
>> a = 3;
>> b = [1 2 3 4];
>> c = [2 3; 4 5];
>> d = [1:4; 7:10];
The above commands declare four variables a, b, c & d. Variable a is constant with
value 3; variable b is row matrix having four columns; variable c is 2-by-2 matrix; and
variable d is 2-by-4 matrix. You can instinctively change the values of any variable. In an
array or a matrix, we can access any element individually also.
>>b (3) = 3;
>>d (2, 2) =8;
The first command accesses the third element of the array b; and the second
command accesses the element at second row and second column of the matrix d.
In order to construct an array, we can use many methods like:
colon notation
linspace command
logspace command
element-by-element entry
TASK 1
MATLAB can perform all the operations on matrices we can think of. In this lab
session, we see how multiplication, addition, transpose, and inverse of a matrix can
be found.
Type the commands:
>> a = [2 3 4; 4 5 6; 6 7 8];
>> b = [12 33 44; 44 23 23; 232 3 2];
Addition: a+b
Subtraction: a-b
Multiplication: a*b
Division: a/b
3
Write all the results of above operation.
Intro to MATLAB
Addition =
14
48
238
36
28
10
48
29
10
Subtraction =
-10 -30
-40 -18
-226
4
-40
-17
6
Multiplication =
1084
1660
2236
147
265
383
165
303
441
Division =
0.0913
0.0905
0.0898
-0.0011 0.0041
0.0880 -0.0041
0.1771 -0.0124
TASK 2
_____________________________________________________________________________________
_____________________________
_____________________________________________________________________________________
_____________________________
_____________________________________________________________________________________
_____________________________
_____________________________________________________________________________________
_____________________________
In order to find the transpose of a matrix, the () operator or the transp function can be used.
If:
a = [2 3 4; 4 5 6; 6 7 8];
>> a'=
4
2
3
4
4
5
6
6
7
8
Intro to MATLAB
TASK 3
Transpose:
>> a'
ans =
Inverse:
5
Intro to MATLAB
ans =
1.0e+016 *
-0.4504
0.9007
-0.4504
0.9007
-1.8014
0.9007
-0.4504
0.9007
-0.4504
PLOTTING
The most interesting part of MATLAB is its plotting capability. We shall learn through
examples. In order to create a plot, the very first step is to define a range of axis.
For example if we want to plot a line between x and y, we need to define range of x which is
the independent variable.
x = [-10:.01:10];
y = 2*x + 4;
plot (x, y);
Write the above commands and note down the output.
6
Intro to MATLAB
TASK 4
Try to plot a sine function, a tangential function and cosine function over the whole period.
Also draw an exponential function.