matlab for diploma students(1)

Post on 07-Apr-2017

389 Views

Category:

Education

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

managementIt 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

MATLABWINDOWS

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 precisi

on

By default, MAT LAB stores all numeric valuesas 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.In addition, the format of the numbers can be controlled.

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 timesEach 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 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:

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

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

Breaking out of loops 2

Breaking out of loops 3

Continuing of loops 1

Errors in Matlab Syntax

ErrorsRuntime 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 bugRun the code again

Step 3Setting up a break point

Step 4 Go toMenu – 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.

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.

Matrix Methods for solving Linear algebraic Equation

Cramer 's Method

Left division

Right division

top related