53
Do you speak MATLAB!

Introduction to MATLAB

Embed Size (px)

DESCRIPTION

Please Download and view it. Presentation on MATLAB

Citation preview

Page 1: Introduction to MATLAB

Do you speak MATLAB!

Page 2: Introduction to MATLAB

MathWorks®

MATLAB®

R2012a (7.14)

Page 3: Introduction to MATLAB

MATLAB

Introducing MATLAB – Language of technical computing 2

Is an integrated technical computing environment that combines numeric computation, advanced graphics and visualization, and a high-level programming language.

The MathWorks, Inc. High-level language of technical computing

Development environment for engineers, scientists

Interactive tools for design, problem solving

Mathematical function libraries

Graphics and data visualization tools

Custom GUIs

External Interfaces: C, C++, Fortran, Java, COM, Excel, .NET

Page 4: Introduction to MATLAB

MATLAB product family

Toolboxes: Collections of specialized Functions

Graphics

2D Graphics3D GraphicsColor & LightingAnimationImage, Audio & Video

Computations

Linear AlgebraData AnalysisPolynomialsInterpolationsSolutions of ODE

External Interfaces

CC++JAVA.netMicorosoft Excel

Parallel ComputingMath, Statistics, and OptimizationControl System Design and AnalysisSignal Processing and CommunicationsImage Processing and Computer VisionTest and Measurement

Built-in Functions

User-written Functions

MATLAB

Computational FinanceComputational Biology

Code Generation and VerificationApplication Deployment

Database Connectivity and Reporting

MATLAB Programming Language

Parallel Computing ToolboxMATLAB Distributed Computing Server

Parallel Computing Symbolic Math ToolboxPartial Differential Equation ToolboxStatistics ToolboxCurve Fitting Toolbox

Optimization ToolboxGlobal Optimization ToolboxNeural Network ToolboxModel-Based Calibration Toolbox

Math, Statistics, and OptimizationControl System ToolboxSystem Identification ToolboxFuzzy Logic Toolbox

Robust Control ToolboxModel Predictive Control ToolboxAerospace Toolbox

Control System Design and Analysis Signal Processing ToolboxDSP System ToolboxCommunications System ToolboxWavelet Toolbox

Fixed-Point ToolboxRF ToolboxPhased Array System Toolbox

Signal Processing and CommunicationsImage Processing ToolboxComputer Vision System ToolboxImage Acquisition ToolboxMapping Toolbox

Image Processing and Computer Vision

Data Acquisition ToolboxInstrument Control ToolboxImage Acquisition ToolboxOPC ToolboxVehicle Network ToolboxTest and Measurement

Financial ToolboxEconometrics Toolbox

Datafeed ToolboxDatabase Toolbox

Computational FinanceSpreadsheet Link EX (for Microsoft Excel)Fixed-Income Toolbox

Financial Derivatives Toolbox

Bioinformatics ToolboxSimBiology

Computational BiologyMATLAB CoderHDL Coder

HDL VerifierFilter Design HDL Coder

Code Generation and Verification

MATLAB CompilerMATLAB Builder NE (for Microsoft .NET Framework)

MATLAB Builder JA (for Java language)MATLAB Builder EX (for Microsoft Excel)

Spreadsheet Link EX (for Microsoft Excel)

Application DeploymentDatabase ToolboxMATLAB Report Generator

Database Connectivity and Reporting

Introducing MATLAB – Language of technical computing 3

Page 5: Introduction to MATLAB

Agenda

Introducing MATLAB – Language of technical computingMinimal MATLAB

4

Page 6: Introduction to MATLAB

Agenda

Introducing MATLAB – Language of technical computingMinimal MATLAB

MATLAB DesktopComputing with MATLAB

5

Array – creation & working

Plotting – creating & visualizingAutomating commands – creating, saving & executing script file

Conditional control statements

Loop control statements

Functions – creating & executing a function file

Page 7: Introduction to MATLAB

Agenda

Introducing MATLAB – Language of technical computingMinimal MATLAB

MATLAB DesktopComputing with MATLABArray – creation & working

Plotting – creating & visualizingAutomating commands – creating, saving & executing script file

Functions – creating & executing a function file

6

Conditional control statements

Loop control statements

Page 8: Introduction to MATLAB

The main window characterized by the MATLAB command prompt (>>)

Command Window

This is where you write, edit, create, and save your own script or programs in files called m-files.

Editor Window

This is where all your files from the current directory are listed.

Current Directory

All commands typed on MATLAB prompt in the command window get recorded, even across multiple session in this window.

Command History

This subwindow lists all variables that you have generated so far along with there type and size.

Workspace

MATLAB - Desktop

Minimal MATLAB 7

Page 9: Introduction to MATLAB

MATLAB - Desktop

The main window characterized by the MATLAB command prompt (>>)

Help Window

Minimal MATLAB 8

Page 10: Introduction to MATLAB

Agenda

Introducing MATLAB – Language of technical computingMinimal MATLAB

MATLAB DesktopComputing with MATLABArray – creation & working

Plotting – creating & visualizingAutomating commands – creating, saving & executing script file

Functions – creating & executing a function file

9

Conditional control statements

Loop control statements

Page 11: Introduction to MATLAB

Basic Data and VariablesComputing with MATLAB

Class

Variable

SizeValue

Minimal MATLAB 10

Page 12: Introduction to MATLAB

Basic Data and VariablesComputing with MATLAB

Multidimensional Array[full or sparse]

Scalar

logical

ab

char

{ }cell struct

functionhandle (@)numeric

int8, uint8,int16, unit16,int32, unit32,int64, unit64

single double

Minimal MATLAB 11

Page 13: Introduction to MATLAB

Arithmetic Operator & Their Precedence

Computing with MATLAB

Operations Operators Examples

AdditionSubtractionMultiplicationRight DivisionLeft DivisionExponentiation

*/\^

>> >> >> >> >> >> (means )

Precedence Order Operators

1

234

Parentheses ( ). For nested parentheses, the innermostare executed first.Exponentiation, ^Multiplication, *; Division, /,\Addition, +; Subtraction, -

help precedenceMinimal MATLAB 12

Page 14: Introduction to MATLAB

Computing with MATLABMathematical Functions

Trigonometry

sindcosdtandcotdsecdcscd

Degree

sincostancotseccsc

sinhcoshtanhcothsechcsch

asinacosatanacotasecacsc

Basic Hyperbolic Inverse

Radian

Basic

Exponential

exploglog10log2sqrt

Complex

absconjimagrealangle

Matrix

dettracerankinvexpmsqrtm

Discrete Math

factorialgcdlcmprimes

help <type function name>Minimal MATLAB 13

Page 15: Introduction to MATLAB

Predefined Variables and Keywords

ans

pieps

infi,jNaN

A variable that has the value of the last expression that was not assigned to aspecific variableThe number π.The smallest difference between two numbers. Equal to 2^(–52), which isapproximately 2.2204e–016.Used for infinity.Defined as , which is: 0 + 1.0000i.Stands for Not-a-Number. Used when MATLAB cannot determine a validnumeric value. Example: 0/0.

Predefined Variables

break case catch classdef continue else elseif end for function global if otherwise parfor persistent return spmd switch try while

Keywords

iskeyword

help <Predefined Variable Name>

Computing with MATLAB

Minimal MATLAB 14

Page 16: Introduction to MATLAB

Managing Variables

clearclear x y zclear allwhowhos

Clears the workspace, all variables are removed from the memoryClears only variables x, y, and zClears all variables and functions from workspaceLists variables currently in the workspaceLists variables currently in the workspace with their sizes together with information about their bytes and class

Command Outcome

Computing with MATLAB

Minimal MATLAB 15

Page 17: Introduction to MATLAB

Agenda

Introducing MATLAB – Language of technical computingMinimal MATLAB

MATLAB DesktopComputing with MATLABArray – creation & working

Plotting – creating & visualizingAutomating commands – creating, saving & executing script file

Functions – creating & executing a function file

16

Conditional control statements

Loop control statements

Page 18: Introduction to MATLAB

OverviewArray

5

5745 7 4

5 10 566 4 67 78 86

Scalar of 1 X 1 Row Vector of 1 X 3 Column vector of 3 X 1

One Dimensional Matrix of 3 X 3

5 10 56 85 236 4 6 23 27 78 86 53 6

5 14 12 10 566 10 504 4 6 89 0

Two Dimensional Matrix of 3 X 5

5 10 56 85 236 4 6 23 27 78 86 53 6

5 14 12 10 56 6 0

5 14 12 10 56 89 0

Three Dimensional Matrix of 3 X 5

Minimal MATLAB 17

Page 19: Introduction to MATLAB

CreationArray

One Dimensional Array

variable_name = [ type vector elements ]

Minimal MATLAB 18

Two Dimensional Array

variable_name = [1st row elements; 2nd row elements; 3rd row elements; ... ; last row elements]

from a known list of numbers with constant spacing by specifying the first term, the spacing, and the

last term

Known lists of numbers

Page 20: Introduction to MATLAB

Array

Minimal MATLAB 19

Creation

Creating a vector with constant spacing by specifying the first term, the spacing, and the last term:

variable_name = [m:q:n]

or

variable_name = m:q:n

m = first term

q = spacing

n = last term

: (colon operator)Constant spacing

Creating a vector with linear (equal) spacing by specifying the first and last terms, and the number of terms:

variable_name = linspace(xi, xf, n)

xi = first element

xf = Last element

n = number of elements

linspaceConstant spacing

Page 21: Introduction to MATLAB

Array

Minimal MATLAB 20

Array Operators

+ Addition- Subtraction.* Element-by-element multiplication./ Element-by-element division.\ Element-by-element left division.^ Element-by-element power‘ Transpose

Operations Operation

𝐴=1 34 2

𝐵=2 35 7

𝐶=𝐴 .∗𝐵= 2 920 14

𝐶=1×2 3×34×5 2×7

Page 22: Introduction to MATLAB

Data ManipulationArray

Minimal MATLAB 21

Addressing - Extraction (Subset)

Addressing an element(s) individually or in subgroups: To redefine only some of the elements, When specific elements are to be used in calculations, When a subgroup of the elements is used to define a new variable.

Page 23: Introduction to MATLAB

Array

Minimal MATLAB 22

Address vector

>> ve = [35 46 78 23 5 14 81 3 55]

ve =

35 46 78 23 5 14 81 3 551 2 3 4 5 6 7 8 9 position or index of element

Data Manipulation

Page 24: Introduction to MATLAB

Array

Minimal MATLAB 23

Address matrix )

3 11 6 54 7 10 213 9 0 8

MAT =

(1,1) (1,2) (1,3) (1,4)

(2,1) (2,2) (2,3) (2,4)

(3,1) (3,2) (3,3) (3,4)

3 11 6 5

4 7 10 2

13 9 0 8

MAT =

Data Manipulation

Page 25: Introduction to MATLAB

Array

Minimal MATLAB 24

Addressing vector : (colon operator)

Data Manipulation

Addressing matrix : (colon operator)

A(:,n) Refers to the elements in all the rows of column n of the matrix A.

A(n,:) Refers to the elements in all the columns of row n of the matrix A.

A(:,m:n) Refers to the elements in all the rows between columns m and n of the matrix A.

A(m:n,:) Refers to the elements in all the columns between rows m and n of the matrix A.

A(m:n,p:q)Refers to the elements in rows m through n and columns p through q of the matrix A.

Page 26: Introduction to MATLAB

Agenda

Introducing MATLAB – Language of technical computingMinimal MATLAB

MATLAB DesktopComputing with MATLABArray – creation & working

Plotting – creating & visualizingAutomating commands – creating, saving & executing script file

Functions – creating & executing a function file

25

Conditional control statements

Loop control statements

Page 27: Introduction to MATLAB

CreationPlotting

Minimal MATLAB 26

ExampleDamped Oscillation equation: 𝒚=𝒆−𝒂𝒕𝐜𝐨𝐬(𝒃𝒕)

Page 28: Introduction to MATLAB

CreationPlotting

Minimal MATLAB 27

ExampleDamped Oscillation equation: 𝒚=𝒆−𝒂𝒕𝐜𝐨𝐬(𝒃𝒕)

a = 0.1; b = 1;t = 0:0.1:20;y = exp(-a*t).*cos(b*t);plot(t,y)grid on;xlabel('Time in second');ylabel('Response in mm');title('Damped Oscillation');legend('a = 0.1,b = 1)');

1-2-3-4-5-6-7-8-9-

Page 29: Introduction to MATLAB

CreationPlotting

Minimal MATLAB 28

Overview

ylabel xlabel

title

legend

Page 30: Introduction to MATLAB

GraphingPlotting

Minimal MATLAB 29

Plot function plot(x,y)

where the variables x and y are vector and must be of same size i.e the number of elements should be equal.

x=[1 2 3 5 7 7.5 8 10];y=[2 6.5 7 7 5.5 4 6 8];plot(x,y)

Page 31: Introduction to MATLAB

GraphingPlotting

Minimal MATLAB 30

Plot function

plot(x,y,‘line specifiers’,‘PropertyName’,PropertyValue)

Line specifiers are optional and can be used to define the style and color of theline and the type of markers (if markers are desired).

Line specifiers

solid (default)dasheddotteddash-dot

Line Style Specifier

---:-.

redgreenbluecyan

Line color Specifier

rgbc

magentayellowblackwhite

Line color Specifier

mykw

Page 32: Introduction to MATLAB

GraphingPlotting

Minimal MATLAB 31

Plot function

plot(x,y,‘line specifiers’,‘PropertyName’,PropertyValue)

Line specifiers are optional and can be used to define the style and color of theline and the type of markers (if markers are desired).

Line specifiers

plus signcircleasteriskpointcrosstriangle (pointed up)triangle (pointed down)

Marker type Specifier

+o*.

^v

squarediamondfive pointed starsix pointed startriangle (pointed left)triangle (pointed right)

Marker type Specifier

sdph<>

Page 33: Introduction to MATLAB

GraphingPlotting

Minimal MATLAB 32

Plot function

plot(x,y,‘line specifiers’,‘PropertyName’,PropertyValue)

To specify the thickness of the line, the size of the marker, and the colors of the marker’s edge line and fill

Property Name

Page 34: Introduction to MATLAB

Agenda

Introducing MATLAB – Language of technical computingMinimal MATLAB

MATLAB DesktopComputing with MATLABArray – creation & working

Plotting – creating & visualizingAutomating commands – creating, saving & executing script file

Functions – creating & executing a function file

33

Conditional control statements

Loop control statements

Page 35: Introduction to MATLAB

CreationScript

Minimal MATLAB 34

OverviewSequence of MATLAB statements in fileScript

A script file is an external file that contains a sequence of MATLAB statements. Script files have a filename extension of .m.

Page 36: Introduction to MATLAB

BasicsScript

Minimal MATLAB 35

variable_name = input(‘prompt message’)

Accepting input from user

Displaying output to user

disp(name of a variable)

disp(‘text as string’)

Saving script file

Creating script file

Executing script file

Page 37: Introduction to MATLAB

Agenda

Introducing MATLAB – Language of technical computingMinimal MATLAB

MATLAB DesktopComputing with MATLABArray – creation & working

Plotting – creating & visualizingAutomating commands – creating, saving & executing script file

Functions – creating & executing a function file

36

Conditional control statements

Loop control statements

Page 38: Introduction to MATLAB

CreationScript

Minimal MATLAB 37

Conditional StatementConditional expression consisting of relational and/or logical operators.

Operations Operators

Less thanGreater thanLess than or equal toGreater than or equal toEqual toNot Equal to

><=>===~=

Relational Operators

Operations Operators

AndOrNot

&||~

Logical Operators

Page 39: Introduction to MATLAB

CreationScript

Minimal MATLAB 38

Example

𝑔 (𝑡 )=¿{0 , 𝑡<−2  

−4−2𝑡 ,−2<𝑡<0  −4−3 𝑡 ,0<𝑡<4  16−2𝑡 ,4<𝑡<8  

0 , 𝑡>8  }Plot

g1g2g3

t = linspace(-5,10);g1 = -4 - 2*t;g2 = -4 + 3*t;g3 = 16 - 2*t;g = g1.*(-2<t & t<=0)... + g2.*(0<t & t<=4)... + g3.*(4<t & t<=8);plot(t,g)

1-2-3-4-5-6-7-

g1

g2

g3

Page 40: Introduction to MATLAB

if/elseif/else StatementScript

Minimal MATLAB 39

ifstatement

StatementGroup 1

False

True

End

StatementGroup 2

if expression statementselseif expression statementselse statementsend

Page 41: Introduction to MATLAB

CreationScript

Minimal MATLAB 40

Switch Statement

False

True

Flowchart

matchcase, k

caseend

False

case statement, k

True

switch switch_expression case case_expression statements case case_expression statements : otherwise statementsend

Page 42: Introduction to MATLAB

Agenda

Introducing MATLAB – Language of technical computingMinimal MATLAB

MATLAB DesktopComputing with MATLABArray – creation & working

Plotting – creating & visualizingAutomating commands – creating, saving & executing script file

Functions – creating & executing a function file

41

Conditional control statements

Loop control statements

Page 43: Introduction to MATLAB

Loop control statementsScript

Minimal MATLAB 42

for (Iterative Loop)

while (Conditional Loop)

continue, break

for index = start:increment:endstatements

end

while expression statementsend

Page 44: Introduction to MATLAB

Agenda

Introducing MATLAB – Language of technical computingMinimal MATLAB

MATLAB DesktopComputing with MATLABArray – creation & working

Plotting – creating & visualizingAutomating commands – creating, saving & executing script file

Functions – creating & executing a function file

43

Conditional control statements

Loop control statements

Page 45: Introduction to MATLAB

CreationFunction

Minimal MATLAB 44

Overview

SYSTEM[Mathematical model of Physical Phenomenon]

(function file)

Set of inputs Set of outputs123

m

123

n

[System]Electrical NetworksCommunication SystemControl SystemThermodynamic SystemComputer system, etc

[Input variables]current, voltage,power, energy,temperature, data

[Output variables]current, voltage,

power, energy,temperature,

Processed data

function [out1, out2, ...] = func_name(in1, in2, ...)

declares the function func_name, and its inputs and outputs

Page 46: Introduction to MATLAB

CreationFunction

Minimal MATLAB 45

Example

𝐻=|𝑦𝑥 |= 1

√1+(𝜔𝑅𝐶)2Black BoxX Y

function H = lp(R,C)w = logspace(-2,6);H = 1./sqrt(1 + (w*R*C).^2);semilogx(w,H)xlabel('Angular frquency, w (rad/s)');ylabel('System Response');title('Low-Pass frequency response');

1-2-3-4-5-6-

Source: MATLAB An Introduction with Applications, Amos Gilat, 4e

Page 47: Introduction to MATLAB

CreationFunction

Minimal MATLAB 46

Example

Page 48: Introduction to MATLAB

CreationFunction

Minimal MATLAB 47

Example

Black Box𝑇 𝑇 𝑑𝑇𝑤

𝐵𝑃 𝑅𝐻

𝑅𝐻=100𝑒𝑒𝑠

𝑇 𝑑=243.5 ln(

𝑒6.112

)

17.67− ln ( 𝑒6.112

)

, dew point temperature

, dry-bulb temperature

, wet-bulb temperature

, barometric temperature in millibars

, relative humidity in %

𝑃𝑠=6.112 exp17.67𝑇𝑇 +243.5

𝑃𝑤=6.112 exp17.67𝑇𝑤

𝑇𝑤+243.5

𝑒=𝑒𝑤−𝐵𝑃 (𝑇 −𝑇𝑤)0.00066 (1+0.00115𝑇𝑤)

Source: MATLAB An Introduction with Applications, Amos Gilat, 4e

Page 49: Introduction to MATLAB

CreationFunction

Minimal MATLAB 48

Example

function [Td, RH] = DewptRhum(T,Tw,BP)es = 6.112*exp((17.67*T)/(T + 243.5));ew = 6.112*exp((17.67*Tw)/(Tw + 243.5));e = ew - BP*(T - Tw)*0.00066*(1 + 0.00115*Tw);RH = 100*(e/es);Td = (243.5*log(e/6.112))/(17.67 - log(e/6.112));

1-2-3-4-5-

𝑇=25 °𝐶 ,𝑇𝑤=19°𝐶 ,𝐵𝑃=985𝑚𝑖𝑙𝑙𝑖𝑏𝑎𝑟Calculate

𝑇=36 °𝐶 ,𝑇𝑤=31°𝐶 ,𝐵𝑃=1020𝑚𝑖𝑙𝑙𝑖𝑏𝑎𝑟

Page 50: Introduction to MATLAB

BEFORE LEAVING

Page 51: Introduction to MATLAB

More on MATLAB

1. MATLAB Tutorials2. Demo and Webinars3. Documentations4. MATLAB Central (User Community)

www.mathworks.com

Page 52: Introduction to MATLAB

Reference

1. Getting started with MATLAB, A quick introduction for scientists and engineers, Rudra Pratap, Indian edition

2. MATLAB An Introduction with Applications, Amos Gilat, 4e3. Matlab - A Practical Introduction to Programming and Problem Solving, Stormy

Attaway, 2e4. Essential MATLAB for Engineers and Scientists, Brian D. Hahn & Daniel T.

Valentine5. A Guide to MATLAB for Beginners and Experienced Users, Hunt Lipsman &

Rosenberg

Page 53: Introduction to MATLAB

THANKSWe speak MATLAB!

ASHISH [email protected]/ashishmeet