matlab for diploma students(1)

61
MATLAB PROGRAMMING An introduction

Upload: retheesh-raj

Post on 07-Apr-2017

393 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Matlab for diploma students(1)

MATLAB PROGRAMMING

An introduction

Page 2: Matlab for diploma students(1)

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

Page 3: Matlab for diploma students(1)

There is no need for memory

managementIt is platform-independent

A new program can be developed easily using the

predefined functions

There is extensive graphics support

Advantages of Matlab

Page 4: Matlab for diploma students(1)

The MATLAB System

Development environment

Function Library

Application Program Interface (API)

Graphics system

Page 5: Matlab for diploma students(1)

SUBWINDOWS IN MATLAB DESKTOP

COMMAND WINDOW

CURRENT FOLDER

COMMAND HISTORY

WORKSPACE

MATLABWINDOWS

Page 6: Matlab for diploma students(1)

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

Page 7: Matlab for diploma students(1)

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

Page 8: Matlab for diploma students(1)

Floating point data type

Double

precision

Single precisi

on

By default, MAT LAB stores all numeric valuesas double-precision floating point numbers.

Page 9: Matlab for diploma students(1)

Arithmetic operators in matlab

Page 10: Matlab for diploma students(1)

Matlab relational operators

Page 11: Matlab for diploma students(1)

Built in constants

Page 12: Matlab for diploma students(1)

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.

Page 13: Matlab for diploma students(1)

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.

Page 14: Matlab for diploma students(1)

How to create a script file

Page 15: Matlab for diploma students(1)

Matlab editor window

Page 16: Matlab for diploma students(1)

View of editor window

Page 17: Matlab for diploma students(1)

• 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

Page 18: Matlab for diploma students(1)
Page 19: Matlab for diploma students(1)

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

Page 20: Matlab for diploma students(1)
Page 21: Matlab for diploma students(1)

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.In addition, the format of the numbers can be controlled.

The fprintf command

Page 22: Matlab for diploma students(1)
Page 23: Matlab for diploma students(1)

Different forms of if construct

Three

Page 24: Matlab for diploma students(1)
Page 25: Matlab for diploma students(1)

Loops in

Matlab

For-end loop

While-end loop

Page 26: Matlab for diploma students(1)

In for-end loops the execution of a command, or a group of commands, is repeated a predetermined number of timesEach round of execution is called a pass.

Structure of a for-end loop

Page 27: Matlab for diploma students(1)

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

Page 28: Matlab for diploma students(1)

Example of for- end loop

Result of example

Example of while - end loop

Result of example

Page 29: Matlab for diploma students(1)

NESTED for LOOPSA for loop can be nested within another for loop.

for k = 1 : 3 for n = 1 : 5 . commands . endend

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 = 1k = 1 n = 2 k = 2 n = 2 k = 3 n = 2k = 1 n = 3 k = 2 n = 3 k = 3 n = 3k = 1 n = 4 k = 2 n = 4 k = 3 n = 4k = 1 n = 5 k = 2 n = 5 k = 3 n = 5

Overall the commands will be executed 15 times with the values of:

Page 30: Matlab for diploma students(1)

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 endenddisp('The matrix is:')disp(matrix)

Nestedloop

>> Example2Enter the number of rows 4Enter the number of columns 2The matrix is: 1 7 7 1 7 7 7 7

Page 31: Matlab for diploma students(1)
Page 32: Matlab for diploma students(1)
Page 33: Matlab for diploma students(1)
Page 34: Matlab for diploma students(1)

Breaking out of loops 1Example:A = 6 B = 15count = 1while A > 0 & B < 10

A = A + 1B = B + 2count = count + 1if count > 100

breakend

end• Break out of the loop after 100 repetitions if the while

condition has not been met

Page 35: Matlab for diploma students(1)

Breaking out of loops 2

Page 36: Matlab for diploma students(1)

Breaking out of loops 3

Page 37: Matlab for diploma students(1)

Continuing of loops 1

Page 38: Matlab for diploma students(1)

Errors in Matlab Syntax

ErrorsRuntime Errors

Page 39: Matlab for diploma students(1)

Syntax errors These errors mainly occur as a result of the misspelling of variable or function names or from missing quotes or parentheses.

Page 40: Matlab for diploma students(1)

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

Page 41: Matlab for diploma students(1)

Debugging M-files

Debugging a

function file

Debugging a

script

Page 42: Matlab for diploma students(1)

Debugging a script file

Debugging step 1 run this code (it is a program to display absolute values in specified range)

Page 43: Matlab for diploma students(1)

Debugging Step2 fix the index bugRun the code again

Page 44: Matlab for diploma students(1)

Step 3Setting up a break point

Page 45: Matlab for diploma students(1)

Step 4 Go toMenu – debug - absolute.m

Page 46: Matlab for diploma students(1)
Page 47: Matlab for diploma students(1)
Page 48: Matlab for diploma students(1)

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.

Page 49: Matlab for diploma students(1)

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.

Page 50: Matlab for diploma students(1)

Move the mouse and place the cursor directly over the variable x. A yellow box pops up and tells you the values of x.

Page 51: Matlab for diploma students(1)

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.

Page 52: Matlab for diploma students(1)

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 “<”.

Page 53: Matlab for diploma students(1)

Fix this bug by changing the character “>” to “<”. Then go to the Menu – Debug - Exit Debug Mode. This terminates the debugger in Matlab.

Page 54: Matlab for diploma students(1)

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.

Page 55: Matlab for diploma students(1)

Profi

ler

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.

Page 56: Matlab for diploma students(1)

Matrix Methods for solving Linear algebraic Equation

Cramer 's Method

Page 57: Matlab for diploma students(1)

Left division

Page 58: Matlab for diploma students(1)

Right division

Page 59: Matlab for diploma students(1)
Page 60: Matlab for diploma students(1)
Page 61: Matlab for diploma students(1)