Expt - 1introduction To MATLAB
Expt - 1introduction To MATLAB
EXERCISE NO. 1
Introduction to MATLAB - 1
I.
Introduction
MATLAB is a high-level computer language for scientific computing and
call.
There is extensive graphics support that allows the results of
[Type text]
The symbol >> is MATLABs prompt for input. The percent sign (%) marks
the beginning of a comment. A semicolon (;) has two functions: it suppresses
printout of intermediate results and separates the rows of a matrix. Without
a terminating semicolon, the result of a command would be displayed. For
example, omission of the last semicolon in the line defining the matrix A
would result in the following.
Type the following into the MATLAB
>> A = [2 1 0; -1 2 2; 0 1 4]
DSP
Page 2
[Type text]
Now create an M-file with MATLAB, type the fucntion below and
save the file with the .m extension. The file name of a saved
function should be identical to the name of the function.
function b = gauss(A,b)
n = length(b);
%-----------------Elimination phase------------for k = 1:n-1
for i = k+1:n
if A(i,k) = 0
lambda = A(i,k)/A(k,k);
A(i,k+1:n) = A(i,k+1:n) - lambda*A(k,k+1:n);
b(i)= b(i) - lambda*b(k);
end
end
end
%--------------Back substitution phase----------for k = n:-1:1
b(k) = (b(k) - A(k,k+1:n)*b(k+1:n))/A(k,k);
end
2.
After saving the M-file, you are now ready to solve problems.
For example if the function for Gauss elimination listed above
is saved as gauss.m, it can be called just like any MATLAB
function:
>> A = [2 1 0; -1 2 2; 0 1 4];
>> b = [1; 2; 3];
>> soln = gauss(A,b)
soln =
0.2500
0.5000
DSP
Page 3
[Type text]
0.6250
Data Types and Variables
Data Types
The most commonly used MATLAB data types, or classes, are double,
char and logical, all of which are considered by MATLAB as arrays. Numerical
objects belong to the class double, which represents double-precision arrays;
a scalar is treated as a 1 1 array. The elements of a char type array are
strings (sequences of characters), whereas a logical type array element may
contain only 1 (true) or 0 (false).
Another important class is function handle, which is unique to MATLAB.
It contains information required to find and execute a function. The name of
a function handle consists of the character @, followed by the name of the
function; e.g., @sin. Function handles are used as input arguments in
function calls. For example, suppose that we have a MATLAB function
plot(func,x1,x2) that plots any user-specified function func from x1 to x2. The
function call to plot sin x from 0 to would be plot(@sin,0,pi).
Additional classes can be defined by the user. The class of an object
can be displayed with the class command.
3.
DSP
Page 4
[Type text]
4.
inf
NaN
i or j
Pi
realmi
n
realm
number
largest usable positive
ax
number
>> 0/0
DSP
Page 5
[Type text]
>> eps
5.
>> A = [ 2 -1 0
-1 2 -1
0 -1 1]
>> A = [2 -1 0; -1 2 -1; 0 -1 1]
>> b = [1 2 3]
>> b = [1; 2; 3]
DSP
Page 6
[Type text]
>> b = [1 2 3]
>> A = [8 1 6; 3 5 7; 4 9 2]
>> A(2,3)
A(:,2)
>> A(2:3,2:3)
DSP
Page 7
[Type text]
6.
>> celldisp(c)
>> c{1}
DSP
Page 8
[Type text]
>> c{1}(2)
7.
>> s4 = s1(1:12)
DSP
Page 9
[Type text]
Observations
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
Conclusion:
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
DSP
Page 10