MATLAB
PROGRAMMING
An
introduction
Salient Features of MATLAB
MATLAB is a
powerful language
for technical
computing
The name MATLAB
stands for MATrix
LABoratory, because
its basic data
element is a matrix
(array).
It integrates
computation,
visualization, and
programming in an
easy-to-use
environment i.e.
Integrated
development
environment (IDE).
It has a very
extensive library of
predefined programs
or functions
designed to help
engineers and
scientists to solve
their problems in a
faster and less
painful way
There is no need for
memory management
It is platform-independent
A new program can be
developed easily using the
predefined functions
There is extensive graphics
support
Advantages of
Matlab
The MATLAB System
Development environment
Function Library
Application Program Interface (API)
Graphics system
SUBWINDOWS IN MATLAB DESKTOP
COMMAND WINDOW
CURRENT FOLDER
COMMAND HISTORY
WORKSPACE
MATLAB
WINDOWS
SNAPSHOT OF MATLAB DESKTOP
Command History window,
which displays all entries
made in the command window
during each session.
This window shows files and
directory available in the current
working directory of MATLAB
It is the window where we type commands, execute
programs, and launch other windows
This lists variables that you have either
entered or computed in your MATLAB
session
Matlab Basic numeric
Data types
Unsigned integer
(4 different classes)
Uint 8 Uint 16 Uint 32 Uint 64
Signed integer
(4 different classes)
Int 8 Int 16 Int 32 Int 64
Floating
point
data type
Double
precision
Single
precision
By default,
MAT LAB
stores all
numeric
values
as double-
precision
floating
point
numbers.
Arithmetic operators in matlab
Matlab relational operators
Built in constants
Abort In order to abort a command in MATLAB, hold down the
control key and press c to generate a local abort with MATLAB.
The Semicolon (;) If a semicolon (;) is typed at the end of a
command, the output of the command is not displayed.
Typing % When per cent symbol (%) is typed in the
beginning of a line, the line is designated as a comment.
When the enter key is pressed, the line is not executed.
SCRIPT FILES
A script file is a sequence
of MATLAB commands,
also called a program.
When a script file runs
(is executed), MATLAB
executes the commands
in the order.
When a script file has a
command that generates
an output, the output is
displayed in the
Command Window.
Using a script file is
convenient because it can
be edited and executed
many times.
Script files can be typed
and edited in any text
editor and then pasted
into the MATLAB editor.
Script files are also called
M-files because the
extension .m is used
when they are saved.
How to create a script
file
Matlab editor window
View of editor window
• In some cases variable is defined in the script file, and when the file
is executed, the user is prompted to assign a value to the variable in
the Command Window.
• This is done by using the input command for creating the variable.
• The form of input command that defines the characters that are
entered as a string
The input command
The disp command
• The disp command is used to display the
elements of a variable without displaying the
name of the variable, and to display text.
• Every time the disp command is executed, the
display it generates appears in a new line.
• The format of disp command is
The fprintf command can be used to display output
(text and data) on the screen.
With this command (unlike with the disp command)
the output can be formatted. For example, text and
numerical values of variables can be intermixed and
displayed in the same line.
The fprintf command
Different forms of
if construct
Three
Loops in
Matlab
For-end
loop
While-end
loop
In for-end loops the execution of a
command, or a group of commands, is
repeated a predetermined number of times
Each round of execution is called a pass.
Structure of a for-end loop
while-end loops are
used in situations when
looping is needed but
the number of passes is
not known in advance.
looping process carry
on until a specified
condition is satisfied.
Structure of while –end
loop
Example of for- end
loop
Result of example
Example of while - end
loop
Result of example
NESTED for LOOPS
A for loop can be nested within another for loop.
for k = 1 : 3
for n = 1 : 5
.
commands
.
end
end
Every time k is increased by 1 the
nested loop loops five times with
the value of n ranging from 1
through 5.
k = 1 n = 1 k = 2 n = 1 k = 3 n = 1
k = 1 n = 2 k = 2 n = 2 k = 3 n = 2
k = 1 n = 3 k = 2 n = 3 k = 3 n = 3
k = 1 n = 4 k = 2 n = 4 k = 3 n = 4
k = 1 n = 5 k = 2 n = 5 k = 3 n = 5
Overall the commands will be
executed 15 times with the values
of:
matrix = 0;
n = input('Enter the number of rows ');
m = input('Enter the number of columns ');
for i = 1:n
for j = 1:m
if i == j
matrix(i,j) = 1;
else
matrix(i,j) = 7;
end
end
end
disp('The matrix is:')
disp(matrix)
Nested
loop
>> Example2
Enter the number of rows 4
Enter the number of
columns 2
The matrix is:
1 7
7 1
7 7
7 7
Breaking out of loops 1
Example:
A = 6 B = 15
count = 1
while A > 0 & B < 10
A = A + 1
B = B + 2
count = count + 1
if count > 100
break
end
end
• Break out of the loop after 100 repetitions if the while
condition has not been met
Breaking out of loops 2
Breaking out of loops 3
Continuing of loops 1
Errors in Matlab
Syntax Errors
Runtime
Errors
Syntax errors
These errors mainly occur as a result of the
misspelling of variable or function names or from
missing quotes or parentheses.
Runtime errors are found by Matlab
during the execution of a program.
They are generally more difficult to fix
than simple syntax errors.
The capability to fix runtime errors is
something that improves with
experience only.
Run time
Errors
Debugging M-files
Debugging a
function file
Debugging a
script
Debugging a script file
Debugging step 1
run this code
(it is a program to display absolute values in
specified range)
Debugging Step2
fix the index bug
Run the code again
Step 3
Setting up a
break point
Step 4 Go to
Menu – debug -
absolute.m
To direct Matlab to run the command in line 1, click on the Step icon
Matlab runs the first line and then proceeds to line 2.
An alternative way to step through the program is by pressing the F10 key on the keyboard. Press
F10 to make Matlab run line 2 of the code.
Move the mouse and place the cursor directly over the variable x. A yellow box pops up and tells you the
values of x.
Press F10 to run line 3. Using the mouse, highlight the expression (x(k) > 0). Place the mouse cursor over
the highlighted area and right-click. Choose Copy from the Context Menu.
Press F10. The code steps to line 7. It should have entered the if statement and gone to line 5 to
change the sign of the first element, but it did not. Instead it went to line 7. By noting this, we can
discover that there is a programming error in the condition for the if statement. The greater than
character “>” should be changed to the less than character “<”.
Fix this bug by changing the character “>” to “<”. Then go to the Menu – Debug - Exit Debug Mode.
This terminates the debugger in Matlab.
The bug is now fixed. Remove the Breakpoint. This can be carried out by using the mouse. Left-click in
the space between the code and the line number 1 (pointed at by the arrow in the figure) to toggle the
Breakpoint to off. The red dot disappears.
Profiler
Let a Matlab program consists of a M-file that
may call a number of different Matlab
functions.
To find out the time that is required to execute
the script file and all the functions that it calls
,Matlab contains a tool that provides us with
this information ,called the Profiler.
Use this information to optimize our code and
make it run faster.
Matrix Methods for solving Linear
algebraic Equation
Cramer 's Method
Left
division
Rightdivision
Matlab for diploma students(1)
Matlab for diploma students(1)
Matlab for diploma students(1)

Matlab for diploma students(1)

  • 1.
  • 2.
    Salient Features ofMATLAB MATLAB is a powerful language for technical computing The name MATLAB stands for MATrix LABoratory, because its basic data element is a matrix (array). It integrates computation, visualization, and programming in an easy-to-use environment i.e. Integrated development environment (IDE). It has a very extensive library of predefined programs or functions designed to help engineers and scientists to solve their problems in a faster and less painful way
  • 3.
    There is noneed for memory management It is platform-independent A new program can be developed easily using the predefined functions There is extensive graphics support Advantages of Matlab
  • 4.
    The MATLAB System Developmentenvironment Function Library Application Program Interface (API) Graphics system
  • 5.
    SUBWINDOWS IN MATLABDESKTOP COMMAND WINDOW CURRENT FOLDER COMMAND HISTORY WORKSPACE MATLAB WINDOWS
  • 6.
    SNAPSHOT OF MATLABDESKTOP Command History window, which displays all entries made in the command window during each session. This window shows files and directory available in the current working directory of MATLAB It is the window where we type commands, execute programs, and launch other windows This lists variables that you have either entered or computed in your MATLAB session
  • 7.
    Matlab Basic numeric Datatypes Unsigned integer (4 different classes) Uint 8 Uint 16 Uint 32 Uint 64 Signed integer (4 different classes) Int 8 Int 16 Int 32 Int 64
  • 8.
    Floating point data type Double precision Single precision By default, MATLAB stores all numeric values as double- precision floating point numbers.
  • 9.
  • 10.
  • 11.
  • 12.
    Abort In orderto abort a command in MATLAB, hold down the control key and press c to generate a local abort with MATLAB. The Semicolon (;) If a semicolon (;) is typed at the end of a command, the output of the command is not displayed. Typing % When per cent symbol (%) is typed in the beginning of a line, the line is designated as a comment. When the enter key is pressed, the line is not executed.
  • 13.
    SCRIPT FILES A scriptfile is a sequence of MATLAB commands, also called a program. When a script file runs (is executed), MATLAB executes the commands in the order. When a script file has a command that generates an output, the output is displayed in the Command Window. Using a script file is convenient because it can be edited and executed many times. Script files can be typed and edited in any text editor and then pasted into the MATLAB editor. Script files are also called M-files because the extension .m is used when they are saved.
  • 14.
    How to createa script file
  • 15.
  • 16.
  • 17.
    • In somecases variable is defined in the script file, and when the file is executed, the user is prompted to assign a value to the variable in the Command Window. • This is done by using the input command for creating the variable. • The form of input command that defines the characters that are entered as a string The input command
  • 19.
    The disp command •The disp command is used to display the elements of a variable without displaying the name of the variable, and to display text. • Every time the disp command is executed, the display it generates appears in a new line. • The format of disp command is
  • 21.
    The fprintf commandcan be used to display output (text and data) on the screen. With this command (unlike with the disp command) the output can be formatted. For example, text and numerical values of variables can be intermixed and displayed in the same line. The fprintf command
  • 23.
    Different forms of ifconstruct Three
  • 25.
  • 26.
    In for-end loopsthe execution of a command, or a group of commands, is repeated a predetermined number of times Each round of execution is called a pass. Structure of a for-end loop
  • 27.
    while-end loops are usedin situations when looping is needed but the number of passes is not known in advance. looping process carry on until a specified condition is satisfied. Structure of while –end loop
  • 28.
    Example of for-end loop Result of example Example of while - end loop Result of example
  • 29.
    NESTED for LOOPS Afor loop can be nested within another for loop. for k = 1 : 3 for n = 1 : 5 . commands . end end Every time k is increased by 1 the nested loop loops five times with the value of n ranging from 1 through 5. k = 1 n = 1 k = 2 n = 1 k = 3 n = 1 k = 1 n = 2 k = 2 n = 2 k = 3 n = 2 k = 1 n = 3 k = 2 n = 3 k = 3 n = 3 k = 1 n = 4 k = 2 n = 4 k = 3 n = 4 k = 1 n = 5 k = 2 n = 5 k = 3 n = 5 Overall the commands will be executed 15 times with the values of:
  • 30.
    matrix = 0; n= input('Enter the number of rows '); m = input('Enter the number of columns '); for i = 1:n for j = 1:m if i == j matrix(i,j) = 1; else matrix(i,j) = 7; end end end disp('The matrix is:') disp(matrix) Nested loop >> Example2 Enter the number of rows 4 Enter the number of columns 2 The matrix is: 1 7 7 1 7 7 7 7
  • 34.
    Breaking out ofloops 1 Example: A = 6 B = 15 count = 1 while A > 0 & B < 10 A = A + 1 B = B + 2 count = count + 1 if count > 100 break end end • Break out of the loop after 100 repetitions if the while condition has not been met
  • 35.
  • 36.
  • 37.
  • 38.
    Errors in Matlab SyntaxErrors Runtime Errors
  • 39.
    Syntax errors These errorsmainly occur as a result of the misspelling of variable or function names or from missing quotes or parentheses.
  • 40.
    Runtime errors arefound by Matlab during the execution of a program. They are generally more difficult to fix than simple syntax errors. The capability to fix runtime errors is something that improves with experience only. Run time Errors
  • 41.
  • 42.
    Debugging a scriptfile Debugging step 1 run this code (it is a program to display absolute values in specified range)
  • 43.
    Debugging Step2 fix theindex bug Run the code again
  • 44.
    Step 3 Setting upa break point
  • 45.
    Step 4 Goto Menu – debug - absolute.m
  • 48.
    To direct Matlabto run the command in line 1, click on the Step icon Matlab runs the first line and then proceeds to line 2.
  • 49.
    An alternative wayto step through the program is by pressing the F10 key on the keyboard. Press F10 to make Matlab run line 2 of the code.
  • 50.
    Move the mouseand place the cursor directly over the variable x. A yellow box pops up and tells you the values of x.
  • 51.
    Press F10 torun line 3. Using the mouse, highlight the expression (x(k) > 0). Place the mouse cursor over the highlighted area and right-click. Choose Copy from the Context Menu.
  • 52.
    Press F10. Thecode steps to line 7. It should have entered the if statement and gone to line 5 to change the sign of the first element, but it did not. Instead it went to line 7. By noting this, we can discover that there is a programming error in the condition for the if statement. The greater than character “>” should be changed to the less than character “<”.
  • 53.
    Fix this bugby changing the character “>” to “<”. Then go to the Menu – Debug - Exit Debug Mode. This terminates the debugger in Matlab.
  • 54.
    The bug isnow fixed. Remove the Breakpoint. This can be carried out by using the mouse. Left-click in the space between the code and the line number 1 (pointed at by the arrow in the figure) to toggle the Breakpoint to off. The red dot disappears.
  • 55.
    Profiler Let a Matlabprogram consists of a M-file that may call a number of different Matlab functions. To find out the time that is required to execute the script file and all the functions that it calls ,Matlab contains a tool that provides us with this information ,called the Profiler. Use this information to optimize our code and make it run faster.
  • 56.
    Matrix Methods forsolving Linear algebraic Equation Cramer 's Method
  • 57.
  • 58.