Tuesday, 15 May 2012

On 00:11 by Unknown in    1 comment
Basic to MATLAB

Matlab is a commercial "Matrix Laboratory" package which operates as an interactive programming environment. It is available for PC's, Macintosh and UNIX systems. Matlab is well adapted to numerical experiments. Programming becomes more easy through Matlab. We can write Programs as a Script (m-files). Program and Script files (m-files) always have file names ending with ".m". Almost every data object is assumed to be an array. Also note that Matlab is a Case sensitive language, it means that 'A' and 'a' are two different    variables.
The beauty of Matlab is that it is more Programming easy. There is allot of built in functions and some toolboxes to easy work. Graphical output (figure) is available to supplement numerical results. We can also Online help is available from the Matlab prompt by typing help.
Now lets start some basic functions and m-files
>> help
help gives us information about the built in function there description. for exp
>> help imwrite()

Matrix: matrix have rows and columns. we can specify rows and columns of any dimentions.

>> A=[1 2 3 4 , 4 3 2 1]
Here are 2 rows and 4 columns. A Scalar is 1*1 matrix in Matlab. A=1. A Vector of n elements can be represent by n*1 matrix. there is a row vector n*1 and a column vector 1*n.

 >> colvec=[2, 5, 3, 1, 6]     % colvec is column vector with 5*1
>> rowvec=[2 5 3 1 6]         % rowvec is a row vector with 1*5

Colon operator: The colon operator ' : ' is understood by Matlab to perform special and useful operations.
for exp if two integer numbers are separated by a colon, Matlab will generate all of the integers between these two integers.

>>a = 1:8             % generate a sequence through colon operator
>>a = [ 1 2 3 4 5 6 7 8 ]    % this method also generate a seq in a matrix form. both are same

If three numbers, integer or non-integer, are separated by two colons, the middle number is interpreted to be a ”step" and the first and third are interpreted to be "limits”:

>>b = 0.0 : .5 : 5.0

it will generates the row vector b = [ 0.0 .5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0]
Colon Operator also used to create a vector from a matrix. lets a

>> B=[1 2 3; 3 4 7, 7 9 3]     % B is 3*3 matrix, now see next line
>>rowB=B(1 , :)                    % it will generate a row vector like 

rowB=[1 2 3]

>> colB=B(: , 1)                   % it will generate a column vector like

colB=[1
           3
           7]

Some Basic Commands of Matlab

>>pwd             % prints working directory
>>demo           % demonstrates what is possible in Matlab
>>who             % lists all of the variables in your matlab workspace
>>whos            % list the variables and describes their matrix size
>>clear             % rases variables and functions from memory
>>clear x          %erases the matrix 'x' from your workspace
>>close            % by itself, closes the current figure window
>>figure           % creates an empty figure window
>>hold on        % holds the current plot and all axis properties so that
>>subsequent   % graphing commands add to the existing graph
>>hold off         % sets the next plot property of the current axes to "replace"

>>save             % saves all the matrices defined in the current session into the file, matlab.mat, located in the                             current working directory
>>load                                 % loads contents of matlab.mat into current workspace
>>save filename x y z            % saves the matrices x, y and z into the file titled filename.mat
>>save filename x y z /ascii   % save the matrices x, y and z into the file titled filename.dat
>> load filename                   % loads the contents of filename into current workspace; the file can be a binary (.mat)
>>load filename.dat              % loads the contents of filename.dat into the variable filename

>> grid                                % creates a grid on the graphics plot
>>title('text')                        % places a title at top of graphics plot
>>xlabel('text')                     % writes 'text' beneath the x-axis of a plot
>>ylabel('text')                     % writes 'text' beside the y axis of a plot
>>text(x,y,'text')                  % writes 'text' at the location (x,y)
>> text(x,y,'text','sc')            % writes 'text' at point x,y assuming lower left corner is (0,0) and upper right corner is (1,1)
>> axis([xmin xmax ymin ymax])  %sets scaling for the x- and y-axes on the current plot


Matrix Operation
>> A=[1 2 3; 2 3 4; 7 8 1]
>> B=[3 5 1; 2 7 0; 8 3 6]
>> A+B;                    % is an addition Operation
>> A-B;                     % is an subtraction Operation
>> A*B;                    % is an Multiplication Operation
>> A/B;                     % is an Division Operation
>> A'                         % is a Transpose Matrix of A
>> A^2                     % is a Power Operation. here Power is Square
>> C = A .* B           % is point by point Multiplication used when dimension mismatched

>> A(1:3 , 2)             % is the first to the third elements of the second column of A.


>> C = zeros(1,3)      % generate Matrix C of all zeros like C=[0 0 0]
>> C = ones(1,4)       % generates Matrix C of all Ones like C=[1 1 1 1]
>> C = rand(3,5)       % uniformly distributed random elements
>> C = randn(2,5)     % normally distributed random elements
>> B = [A A-2; B*2 C/4]     % Construction of Matrix Z from Small matrices.


Functions:
MATLAB provides a large number of standard elementary mathematical functions, including abs, sqrt, exp,  sin, cos, magic etc.
Mathematical Functions
>> help elfun                   % this will give list of all Elementary Maths Functions
Advanced Mathematical and Matrix Functions
>>help specfun
>> help elmat
Data Analysis Functions
>>help datafun

Programming with Matlab
Matlab Programming codes are save into m-files. To start a new program code, go to files, select new files, and then select text editor. a new window will be opened. now write your program in this file and save it as M-file. M-files are of two types. script and functions. functions are like methods they can accept arguments and return outputs, but the function name should be same as the M-file name. the script is same as normal program contains a bunch of code.

Flow Controls
Matlab is a Programming language which support all flow controls types

  • If statement
  • Switch statement
  • For loops
  • While loops
  • Continue statement
  • Break statement
All these flow control and some other important graphs will be discussed in the next Post "Matlab Tutorial Part2". 









1 comment: