Sunday, 2 November 2014

Beginners tutorial MATLAB 

Its a simulation tool. Very useful for ECE EEE ME 

MATLAB is abbreviation for MATrix LABoratory.

MATLAB is an integrated development environment and computer language that enables users to perform computationally intensive tasks.  The software is used for numerical computation tasks, visualization, and mathematical programming.
For more details, see http://www.mathworks.com/products/matlab.

Latest release of MATLAB is R2014a.

As the name emphasis, MATLAB considers every variable/value in terms of matrices.
MatLab vs c: MatLab is not a programming language, but it is a scripting language. In simple English, you use English-like commands in the MatLab program; whereas corresponding c program will run in the background.

So, MatLab program is slower than c program; but is more flexible and lightweight than the former. 


The latest MATLAB constitutes FOUR platforms, within its environment.
1. M-file/Script file platform
2. Simulink
3. GUIDE- Graphical User Interface IDE(Integrated Development  Envinirment)
4. Matlab APPLICATIONS


Note: All these four are not independent, but dependent on each other. It is recommended to start with M-file/script-file programming for a beginner.


Cost of MatLab: MatLab is a proprietary software, but it is worthy-enough to pay for it. Even there is free student version of MatLab, which is available on demand from the mathworks website.
A humble request is to buy the MatLab products, and don't be a part of pirating the software(which is done by many torrent-hosting/file-sharing  websites).
Platform support: MatLab is available in Windows, Unix  and Macintosh platforms. The Unix flavor is used for linux too.
Alternatives to MatLab: In contract, there are free alternatives for MatLab, such as Octave, Scilab,etc. Scilab is available in windows and linux platforms; whereas Octave is available only for Linux platform.

Matlab programs are backward compatible.

MATLAB is a proprietary software, patented by MathWorks Inc.
Though it is commercial, the free student version can be downloaded from MathWorks website, on registration.

Windows in MATLAB:

By default, you will see the default window layout.
Else, to get the default layout of windows in MATLAB, go to File->Desktop->Desktop Layout->Default.

1. Command Window: It is heart of MATLAB, where the response of all inputted commands will the displayed, expect the graphical responses.

2. Command History: It stores all the commands executed in the command window, including the date and time of their execution.
It doesn't store commands executed through script-files.


3. Current Folder (Current working Directory): Indicates the current folder to which matlab is associated. All user-defined functions and other files(images, audio files, video files..) must be in the current folder. Also, the current folder association can also be changed.

4. Workspace: It stores all the variables, their data, data types, and some more information about the variables used in current computations. The variables can be cleared. Closing Matlab, automatically results in clearing of workspace.

5. Editor Window: All the M-files/script files must be written in this editor. Optionally, notepad can also be used as matlab editor, where the file should be saved with .m extension to make an M-file. But, Editor Window has some advanced options like direct execution, publishing, ...

6. Figure Window: All the graphical responses will be displayed in this window. By default, it saves with .fig extension; but there are options to save as conventional file formats like .jpg. .png,...

 File Types in MATLAB:

1) .m files -> These are the Matlab script files. user-defined functions are also saved as .m files.
2) .fig files -> The default file type to save the images/figures. The GUIDE files are also saved with .fig extension.
3) .mdl files -> The Simulink Model files are saved with .mdl extension. mdl files are text files.
4) .slx files -> New file format to save the simulink models, release from R2012a. SLX is a compressed file that contains model information in XML format.
 Note: It is optional to the user to choose between .mdl and .slx extensions.

5).mat files-> It is similar to the .dat file, used to store data.


Apart from these proprietary file formats, Matlab supports few file formats to import and export data.
 
How to open Matlab
    Method1: click on matlab shortcut in start menu or on desktop
    Method2: for windows os ::::Press windows+R--->type cmd--->type "matlab"
             for linux os:::::::go to terminal/Konsole--->type "matlab"

Note: It is recommended not to do any action, atleast-for-a-vial, while opening MATLAB because Matlab consumes more RAM while invoking.

2. Basic commands
 
   ver
   version
   license/hostid
   computer/computer('arch')
   ispc/isunix/ismac
   matlabpath/path/setpath
   dir/ls---files in current working directory
   delete filename.m
   mkdir/rmdir
   diary
   cd/pwd----present working directory
   diary('filename'),diary on ....diary off

Working 
Type the following following commands in the Command Window, and observe(verify) their corresponding results  12

ans =     12

% commenting operator in matlab
%12

a=23

a =

    23

a=23; %; is a output suppressor operator

who

Your variables are:

a    ans

whos
  Name      Size            Bytes  Class     Attributes

  a         1x1                 8  double            
  ans       1x1                 8  double            


who---- This command lists the variables in workspace.

whos----This command details the variables in workspace.


12+23

ans =

    35

12-23

ans =

   -11

mul=12*23

mul =

   276

div=12/23;
; is the output suppressing operator

who

Your variables are:

a    ans  div  mul

div

div =

    0.5217

1/2

ans =

    0.5000

2/1

ans =

     2

1/2

ans =

    0.5000

1\2

ans =

     2

%   \ is the inverted division operator a\b=b/a
2/3

ans =

    0.6667

2\3

ans =

    1.5000

3/2

ans =

    1.5000

% exponential representation

1e3

ans =

        1000
% e and E holds fine

1E3

ans =

        1000
% But, matlab is case-sensitive

a=exp(2)

a =

    7.3891

A=exp(1)

A =

    2.7183
who
a   A

% lookfor ----This command is used for command search
% Syntax: lookfor keywork_to_search

% we know the relation between exponential and logarithm

q=exp(12)

q =

   1.6275e+05

log(q)

ans =

    12

% = assignment operator
% == equivalence operator, tests equality between variables on both sides
% == results 1, if both are equal; 0, if they are not equal
1==2

ans =

     0

1==1

ans =

     1

a=exp(12),b=log(a)

a =

   1.6275e+05


b =

    12

log(exp(12))

ans =

    12
exp(log(12))

ans =

    12

% log ---command to compute natural logarithm
% log10---command to compute base 10 logarithm
exp(log10(12))

ans =

    2.9423

log10(exp(12))==log(exp(12))

ans =

     0

% both are not equal

log10(10^12)==10^(log10(12))
ans=
 
     1
%both are equal

%trigonometric computations
sin(90)

ans =

    0.8940

pi

ans =

    3.1416

22/7

ans =

    3.1429

sin(pi/2)

ans =

     1

sin(pi/2)==sin(90)

ans =

     0

sin(0)

ans =

     0

asin(sin(pi/2))

ans =

    1.5708

asin(sin(90))

ans =

    1.1062

sin(5)

ans =

   -0.9589

% Matlab’s trig functions are permanently set to radians mode

% The up-arrow key will display previous commands.
% And when you back up to a previous command that you like, hit Enter and it will execute.
% Or you can edit it when you get to it (use , , and Del), then execute it. Try doing this
% Now to re-execute the commands you have already typed.
diary off

clear all %to clear the workspace. Execute and observe the workspace window

close all % closes safely all the windows opened, except main window and the editor window

clc%clears the command window. Execute it and observe the command window

who
% As "clear all" command clears all the variables; so "who" can't display any variable.

% who lists active variables

% whos----lists active variables and their sizes

% what-------lists .m files available in the current directory


% Numerical Accuracy in MATLAB
225/331

ans =

    0.6798

format long e
225/331

ans =

     6.797583081570997e-01

format short% (the default format)
225/331

ans =

    0.6798

format long
225/331

ans =

   0.679758308157100

format short e
%e stands for exponential notation
225/331

ans =

   6.7976e-01

format bank
225/331

ans =

    0.67

format short% (the default format)

225/331

ans =

    0.6798


pi

ans =

    3.1416

who

Your variables are:

ans

pi=2%can be done

pi =

     2

who

Your variables are:

ans  pi 

pi

pi =

     2

% please don't do so. "pi" was assigned to 3.1416, by default
pi=3.1416

pi =

    3.1416

clear all
pi

ans =

    3.1416

power(2,5)
ans =

    32

power(10,2)

ans =

   100

pow2(2)

ans =

     4

%pow2(2) means 2^2
%X = pow2(Y) for each element of Y is 2 raised to the power Y
%  X = pow2(F,E) for each element of the real array F and a integer
%    array E computes X = F .* (2 .^ E).

X = pow2(Y) for each element of Y is 2 raised to the power Y

sqrt(25)

ans =

     5

% sqrt---to calculate the squarate


% working with complex numbers
a=2+2j

a =

   2.0000 + 2.0000i

b=3+5i

b =

   3.0000 + 5.0000i

% either "i" or "j" can be used to represent the imaginary number
diary off
2j

ans =

        0 + 2.0000i

2i

ans =

        0 + 2.0000i

%but, the notation "i2" or "j2" is forbidden
i2
{ Undefined function or variable 'i2'.
}
j2
{ Undefined function or variable 'j2'.
}
% abs----to calculate the absolute value of a complex number
who

Your variables are:

a    ans  b  

w=3;
abs(w)

ans =

     3
% abs(real_number)=real_number

% abs(imaginary_number)=real_number
abs(3j)

ans =

     3

abs(3.33j)

ans =

    3.3300

a

a =

   2.0000 + 2.0000i

abs(a)

ans =

    2.8284

%abs(m+nj)=sqrt(power(m,2)+power(n,2))
sqrt(power(2,2)+power(2,2))

ans =

    2.8284

z1=1+2i

z1 =

   1.0000 + 2.0000i

%% or you can multiply by i, like this
z1=1+2*i

z1 =

   1.0000 + 2.0000i

z2=2-3i

z2 =

   2.0000 - 3.0000i

% add and subtract
addition=z1+z2

addition =

   3.0000 - 1.0000i

subtraction=z1-z2

subtraction =

  -1.0000 + 5.0000i

% multiply and divide
multiply=z1*z2

multiply =

   8.0000 + 1.0000i

division=z1/z2

division =

  -0.3077 + 0.5385i

d1=z1\z2

d1 =

  -0.8000 - 1.4000i

z=3+4i

z =

   3.0000 + 4.0000i

real(z)

ans =

     3

imag(z)

ans =

     4

conj(z)

ans =

   3.0000 - 4.0000i

abs(z)

ans =

     5

angle(z)

ans =

    0.9273

diary off
%angle(m+nj)=atan(b/a)
z

z =

   3.0000 + 4.0000i

atan(4/3)==angle(3+4j)

ans =

     1

%Here, 1 means both are equal
%Euler’s famous formula e
%exp(xi)= cos x + i sin x
exp(i*pi/4)

ans =

   0.7071 + 0.7071i

%% Housekeeping Functions
% ceil(x)---the nearest integer to x looking toward +
% close 3--- closes figure window 3
% fix(x)---- the nearest integer to x looking toward zero
% fliplr(A)------ flip a matrix A, left for right
% flipud(A)-----flip a matrix A, up for down
% floor(x)------- the nearest integer to x looking toward -
% length(a)------the number of elements in a vector
% mod(x,y)-----the integer remainder of x/y; see online help if x or y are negative
% rem(x,y)------the integer remainder of x/y; see online help if x or y are negative
% rot90(A)------rotate a matrix A by 90
% round(x)------the nearest integer to x
% sign(x)--------the sign of x and returns 0 if x=0
% size(c)---------the dimensions of a matrix

No comments:

Post a Comment