# Workshop on MATLAB : Fundamental and Applications

Posted

ØMATLAB = MATrix LABoratory

It is a Numerical Computing environment and Fourth Generation Programming Language developed by “MathWorks” organization.

It allows matrix manipulations, plotting of functions and data, implementation of algorithms, creation of user interfaces and interfacing with programs written in other languages, including  Fortran , C, C++ and Java.

MATLAB is widely used in Academic and Research Institutions as well as Industrial Enterprises.

MATLAB Basics Elements:

• Array: A collection of data values organized into rows and columns, and known by a single name.

Arrays:

• The fundamental unit of data in MATLAB
• Scalars are also treated as arrays by MATLAB (1 row and 1 column).
• Row and column indices of an array start from 1.
• Arrays can be classified as vectors and matrices.
• Vector: Array with one dimension
• Matrix: Array with more than one dimension
• Size of an array is specified by the number of rows and the number of columns, with the number of rows mentioned first (For example: n x m array).

Total number of elements in an array is the product of the number of rows and the number of columns.

• a vector x = [1 2 5 1]

x =

1   2   5   1

• a matrix x = [1 2 3; 5 1 4; 3 2 -1]

x =

1     2     3

5     1     4

3     2    -1

• transpose y = x.’      y =

1

2

5

1

Arithmetic Operators:

variable_name = expression;

–  subtraction

*  multiplication

/  division

^  power

‘  complex conjugate transpose

.*  element-by-element mul

./  element-by-element div

.^  element-by-element power

.‘  transpose

Relational & Logical Operators:

==  equal

~=  not equal

<  less than

<=  less than or equal

>  greater than

>=  greater than or equal

&  AND

|  OR

~  NOT

pi  3.14159265…

j  imaginary unit,

i  same as j

Variables:

• A region of memory containing an array, which is known by a user-specified name.
• Contents can be used or modified at any time.
• Variable names must begin with a letter, followed by any combination of letters, numbers and the underscore (_) character. Only the first 31 characters are significant.
• The MATLAB language is Case Sensitive. NAME, name and Name are all different variables.

Note: Give meaningful (descriptive and easy-to-remember) names for the variables. Never define a variable with the same name as a MATLAB function or command.

Initializing Variables in Assignment Statements

An assignment statement has the general form

var = expression

Examples:

>> var = 40 * i;    >> a2 = [0 1+8];

>> var2 = var / 5;    >> b2 = [a2(2) 7 a];

>> array = [1 2 3 4];  >> c2(2,3) = 5;

>> x = 1;  y = 2;     >> d2 = [1 2];

>> a = [3.4];  >> d2(4) = 4;

>> b = [1.0 2.0 3.0 4.0];

>> c = [1.0; 2.0; 3.0];

>> d = [1, 2, 3; 4, 5, 6];    ‘;’ semicolon suppresses the

>> e = [1, 2, 3    automatic echoing of values but

4, 5, 6];    it slows down the execution.

Initializing of Expressions:

first: increment: last

• Colon operator: a shortcut notation used to initialize arrays with thousands of elements

>> x = 1 : 2 : 10;

>> angles = (0.01 : 0.01 : 1) * pi;

• Transpose operator: (′) swaps the rows and columns of an array

>> f  = [1:4]′;

>> g = 1:4;

>> h = [ g′  g′ ];

Initializing with Keyboard Input:

• The input function displays a prompt string in the Command Window and then waits for the user to respond.

my_val = input( ‘Enter an input value: ’ );

in1 = input( ‘Enter data: ’ );

in2 = input( ‘Enter data: ’ ,`s`);

Special Values:

• pi: p value up to 15 significant digits
• i, j: sqrt(-1)
• Inf: infinity (such as division by 0)
• NaN: Not-a-Number (division of zero by zero)
• clock: current date and time in the form of a 6-element row vector containing the year, month, day, hour, minute, and second
• date: current date as a string such as 3-Oct-2012
• eps: epsilon is the smallest difference between two numbers
• ans: stores the result of an expression

The Display Function:

disp( array )

>> disp( ‘Hello’ )

Hello

>> disp(5)

5

>> disp( [ ‘Poona ‘ ‘College’ ] )

Poona  College

>> name = ‘Students’;

>> disp( [ ‘Hello ‘ name ] )

Hello Students

The Print function:

fprintf( format, data )

–%d  integer

–%f  floating point format

–%e  exponential format

–%g  either floating point or exponential     format, whichever is shorter

–\n  new line character

–\t  tab character

>> fprintf( ‘Result is %d’, 3 )

Result is 3

>> fprintf( ‘Area of a circle with radius %d is %f’, 3, pi*3^2 )

Area of a circle with radius 3 is 28.274334

>> x = 5;

>> fprintf( ‘x = %3d’, x )

x =    5

>> x = pi;

>> fprintf( ‘x = %0.2f’, x )

x = 3.14

>> fprintf( ‘x = %6.2f’, x )

x =    3.14

>> fprintf( ‘x = %d\ny = %d\n’, 3, 13 )

x = 3

y = 13

Built-in MATLAB Functions:

• result = function_name( input );

–abs, sign

–log, log10, log2

–exp

–sqrt

–sin, cos, tan

–asin, acos, atan

–max, min

–round, floor, ceil, fix

–mod, rem

• help elfun ® help for elementary math functions
• Types of errors in MATLAB programs:
• Syntax errors

–Check spelling and punctuation

• Run-time errors

–Check input data

–Can remove “;” or add “disp” statements

• Logical errors

–Use shorter statements

–Check typos

–Check units

–Ask your teacher, friends, assistants, instructor, …

Flow Control:

• Logic Control:

–IF / ELSEIF / ELSE

–SWITCH / CASE / OTHERWISE

• Iterative Loops:

–FOR

–WHILE

Basic Elements of Matlab’s Desktop:

• Command Windows: Where all commands and programs are run.

Write the command or program name and hit Enter.

• Command History: Shows the last commands run on the Command Windows. A command can be recovered clicking twice
• Current directory: Shows the directory where work will be done.
• Workspace: To see the variables in use and their dimensions (if working with matrices)
• Help (can also be called from within the comand windows)
• Matlab Editor: All Matlab files must end in the .m extension.
• Some Useful MATLAB commands:
• Edit Editor
• Exit Close
• Quit Close
• who List known variables
• whos List known variables plus their size
• help Ex: >> help sqrt Help on using sqrt
• lookfor Ex: >> lookfor sqrt Search for

keyword sqrt in m-files

• what Ex:>> what a: List MATLAB files in a:
• clear Clear all variables from work space
• clear x y Clear variables x and y from work space
• clc Clear the command window
• what List all m-files in current directory
• dir List all files in current directory
• ls Same as dir
• type test Display test.m in command window
• delete test Delete test.m
• cd a: Change directory to a:
• chdir a: Same as cd
• pwd Show current directory
• which test Display current directory path to test.m