Fundamental of MATLAB

33
Introduction to MATLAB

Transcript of Fundamental of MATLAB

Introduction to MATLAB

Introduction to MATLABMATLAB stands for MATrix LABoratoryDeveloped by MathWorks Inc.Allows to perform mathematical computations, generate plots, write programs and create graphical user interface.Add- on software modules – toolboxes ; perform more specialized computation.Uses high- level programming language aIncludes control flow statement, data structures, input/output and object- oriented programming features.

What will you learn?

MATLAB environmentMATLAB fundamentalArrays and data structureScript fileMATLAB programmingGraphics

The MATLAB DesktopConsist oft three basic windows

Command windows

Command History

Lauch Pad and wokspace

MATLAB environmentFile and Edit Menu

New – open a dialog box to create a new M- file, Figure, Model or GUIOpen – open a dialog box to select a file for editing.Import data – open a dialog box to import data.Save workspace As.. – open a dialog box to save a fileSet path – open a dialog box to search path.Preferences.. – opens a dialog box to set preferences for items in MATLAB desktop

MATLAB environmentPath Browser

MATLAB file operations use current directory and search path as reference point.File that want to run – must be located in the current directory (MATLAB search path.)

MATLAB environment

MATLAB environmentCommand Windows

Use to interact with MATLAB (enter variables, expressions, and commands)

ExampleEnter the following commands at the MATLAB prompt>> a=1>> b=2.0>> z=a+b

MATLAB environmentWorkspace Browser

Allow the user to view and manage the variables that had been.Variables - can be edit or delete in the workspace using the Workspace Browser.To clear workspace variables –Edit – clear workspace>>clear

MATLAB environmentSaving workspace variable

When quit MATLAB – the variable in MATLAB workspace will be clearedSave the variable in the current workspace and load them later when to reuse the variables.MATLAB save variables names, sizes and values in binary called aMAT-file. ( .mat extensions)

MATLAB environmentCommand History

Display most recently command run in the command windows

MATLAB environmentEditor/Debugger window

Allows user to create, edit and debug program easily.Open Editor/Debugger window from MATLAB Desktop

File – New – M-File>> edit

MATLAB fundamentalWill learn about

Special characters and keysNumber formatsArithmetic operationsDefining variablesCharacter stringsInput and output statementsMATLAB functionsVector and matrices

MATLAB fundamentalSpecial characters and keys

Information you typed will be printed on screen, (at end of line)

Information you typed will not be echoed on screen; (at end of line)

Statement continue on next line… (at end of line)

Comment statementIgnores anything to the right of this sign

%

DescriptionsCharacter

MATLAB fundamentalNumber formats

Use conventional decimal notations with an optional decimal point and leading plus or minus

Exp : 1 -2 +3.0 0.004 56.98Scientific notations uses e to indicate a multiplication by power of 10

Exp: 1.0e03 2.03e3Imaginary number

1i -3.1415j

MATLAB fundamentalNumber formats

Use conventional decimal notations with an optional decimal point and leading plus or minus

Exp : 1 -2 +3.0 0.004 56.98Scientific notations uses e to indicate a multiplication by power of 10

Exp: 1.0e03 2.03e3Imaginary number

1i -3.1415j

MATLAB fundamentalFormat command

3.14Fixed dollars and cenFormat bank

3.1416e+005 digit floating pointFormat short e

3.1234567899656315 digit scaled fixedFormat long

3.12455 digitFormat short

exampleDescriptionCommand

MATLAB fundamentalArithmetic operations

Performs calculation just like calculator.Brackets can be used to alter the precedence.

a/bDivision : a/b/

a^bExponential: ab^

a*bMultiplication :ab*

a – bSubtraction : a-b-

a + bAddition :a+b+

MATLABOperationSymbol

MATLAB fundamentalArithmetic operations

Evaluated from left to rightFollow order of precedence as follow

Evaluated from left to rightAddition and subtraction

Fourth

Evaluated from left to rightMultiplication and division

Third

Evaluated from left to rightExponentiationSecond

Start with innermost pairParenthesesFirst

MATLAB fundamentalDefining variables

Do not require a type definition or dimension statement.When new variables appears, MATLAB automatically creates it.Defined using the = sign, called the assignment operators>> x=2>>x=x+3

MATLAB fundamentalBuilt- in variable names

MATLAB has pre-defined variables nameMust not use the built-in variable names in MATLAB variables

Imaginary numberi,j

Smallest usable positive real numberrealmin

InfinityInf

The number πPi

Temporary variablesans

DescriptionVariables names

MATLAB fundamentalCharacter string

enclosed with single quotes.Contain alphabetic letters, numeric and symbols

>> ‘hai’ans =

haitext = ‘Selamat Datang’ans =

Selamat Datang

MATLAB fundamentalInput and output statements

Able to accepts from other userSuppressing output

Used semicolon ‘;’ to suppress resultExample >> a=6;

Output statementdisp(x)fprintf(‘The value of m is %g \n’, m)

Input statement>>x=input(‘Input your name’,’s’)>>k=input(‘Enter the value of k \n’)

MATLAB fundamentalMatlab FunctionsMost MATLAB commands are built in functions1. Elementary functions2. Special Math Functions

MATLAB fundamentalMatlab FunctionsElementary functions

>> help elfunc>>lookfor keyword

Examples:log - Natural logarithm.

log10 - Common (base 10) logarithm.log2 - Base 2 logarithm and dissect floating point number.pow2 - Base 2 power and scale floating point number.sqrt - Square root.nextpow2 - Next higher power of 2.

MATLAB fundamentalMatlab FunctionsElementary functions

Complex.abs - Absolute value.angle - Phase angle.complex - Construct complex data from real and imaginary

parts.conj - Complex conjugate.imag - Complex imaginary part.real - Complex real part.unwrap - Unwrap phase angle.isreal - True for real array.cplxpair - Sort numbers into complex conjugate pairs.

MATLAB fundamentalMatlab FunctionsSpecial Math functions

>>help specfun>>lookfor keyword

Examples:Number theoretic functions.

factor - Prime factors.isprime - True for prime numbers.primes - Generate list of prime numbers.gcd - Greatest common divisor.lcm - Least common multiple.rat - Rational approximation.rats - Rational output.

MATLAB fundamentalUser Defined FunctionsUser created using specific format – M- filesSaved with extension .m

Later topics

MATLAB fundamentalMatrices and VectorsMatrix is the basic building blocks of MATLAB

A matrix is a set of numbers arranged in a rectangular grid of rows and columnsExample: 3 x 3 Matrix

A = -1 2 40 5 82 6 -3

MATLAB fundamentalMatrices and VectorsA vector is a set of numbers arranged

all in one row( row vector) c= [ 2 4 0 –8]

All in in one column (column vector)d= 1

5-5

BRIEF LIST OF MATLAB COMMANDS

Some Basic Commands (Note command syntax is case-sensitive!) matlab - loads the program matlab into

your workspace. quit - quits matlab, returning you to the

operating system. exit - same as quit.

BRIEF LIST OF MATLAB COMMANDSwho - lists all of the variables in your

matlab workspace. whos - list the variables and describes

their matrix size. clear - deletes all matrices from active

workspace. clear x - deletes the matrix x from active

workspace. ... the ellipsis defining a line continuation is

three successive periods.

BRIEF LIST OF MATLAB COMMANDSsave saves all the matrices defined in the current

session into the file, matlab.mat. load - loads contents of matlab.mat into current

workspace. save filename - saves the contents of workspace into

filename.matsave filename x y z - saves the matrices x, y

and z into the file titled filename.mat. load filename - loads the contents of filename into

current workspace; the file can be a binary (.mat) file or an ASCII file.