MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering...

41
MATLAB FOR ENGINEERS CREDITS: 4 UNIT#1 LECTURE#1

Transcript of MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering...

Page 1: MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008. MATLAB is too broad for our purposes

MATLAB FOR ENGINEERS

CREDITS: 4

UNIT#1 LECTURE#1

Page 2: MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008. MATLAB is too broad for our purposes

Unit I Basics: MATLAB environment, Variables, Basic data types, Relational and Logic operators, Conditional statements, Input and Output, Loops and branching.

Unit II Matrices: Creating and Manipulating matrices, Matrix maths and Matrix functions, Colon operator, Linspace, Cross product, Dot product, Logical functions, Logical indexing, 3-dimensional arrays, Cell arrays, Structures, Plotting: 2-D and 3-D plots: Basic plots, subplots, Histograms, Bar graphs, Pie charts.

Unit III M-file scripts: Creating, saving and running an M-file, Creating and running of a function, Function definition line, H1 and help text lines, Function body, Sub-functions, Nested functions, File I/O handling, M-file debugging.

Unit – IV Simulink: Introduction, Block diagram, Functions, Creating and working with models, Defining and managing signals, Running a simulation, analyzing the results.

Unit V Applications: Root finding, Data analysis, Statistical functions, Polynomials, Curve fitting, Interpolation, Ordinary differential equations, Integration and differentiation, Signal processing applications, Circuit analysis applications, Control system applications.

Page 3: MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008. MATLAB is too broad for our purposes

*D Hanselman and B Littlefield

Mastering Matlab 7, Pearson Education.

A Gilat Matlab: An Introduction with Applications, John Wiley and Sons, 2004.

Y Kirani Singh and B B Chaudhari

Matlab Programming, Prentice Hall of India, 2007

*Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008

Page 4: MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008. MATLAB is too broad for our purposes

MATLAB is too broad for our purposes in this course.

The features we are going to require is

MATLAB

Programming Basic

Operations SIMULINK

Mathematical, Logical, Relational Operations on various data types, Various Plots, operation on matrix

Sim Power System

Page 5: MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008. MATLAB is too broad for our purposes

1. Basic Mathematics equivalent to senior secondary level.

2. Basic programming skills

3. Basic Electrical Engineering.

4. And that’s all what is required to start with EEE -278.

Page 6: MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008. MATLAB is too broad for our purposes

MATLAB is a software package for doing numerical

computation. It was originally designed for solving linear

algebra type problems using matrices. It’s name is derived

from MATrix LABoratory

MATLAB has since been expanded and now has built-in

functions for solving problems requiring data analysis, signal

processing, optimization, and several other types of scientific

computations. It also contains functions for 2-D and 3-D

graphics and animation.

Page 7: MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008. MATLAB is too broad for our purposes

MATLAB includes hundreds of functions for:

Data analysis and visualization,

Numeric and symbolic computation,

Engineering and Scientific graphics,

Modeling, simulation, and prototyping,

Eigenvalue, singular value

Page 8: MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008. MATLAB is too broad for our purposes

By Default we have three windows opening up during launch

1. Command Window

2. Workspace

3. Command History

In addition to above we have other windows also like

Figure window

Editor window

Current directory window

Page 9: MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008. MATLAB is too broad for our purposes

MATLAB

Command

Window

Page 10: MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008. MATLAB is too broad for our purposes

File

Page 11: MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008. MATLAB is too broad for our purposes

Edit

Page 12: MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008. MATLAB is too broad for our purposes

View

Page 13: MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008. MATLAB is too broad for our purposes

View

Page 14: MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008. MATLAB is too broad for our purposes

View

Page 15: MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008. MATLAB is too broad for our purposes

Web

Page 16: MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008. MATLAB is too broad for our purposes

Help

Page 17: MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008. MATLAB is too broad for our purposes
Page 18: MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008. MATLAB is too broad for our purposes

Pressing the up arrow in the command window will bring up the last command entered This saves you time when things go wrong

If you want to bring up a command from some time in the past type the first letter and press the up arrow.

The current working directory should be set to a directory of your own

18

Page 19: MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008. MATLAB is too broad for our purposes

Every programming language has its specific ways of recognizing data types for data handling.

MATLAB has following Data Types

Scalars

Characters

Arrays

Strings

Cell-arrays

Structures

Page 20: MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008. MATLAB is too broad for our purposes

Any Number which can represent a quantity or measure is known as scalar

Includes integers, complex numbers, floating point numbers

Example

4, 3+5i, -23.45 etc

Page 21: MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008. MATLAB is too broad for our purposes

Any single alphanumeric symbol enclosed in a single quote is a character constant.

Example

‘2’, ‘e’, ‘A’ etc

5 and ‘5’ are different data

Page 22: MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008. MATLAB is too broad for our purposes

Any two or more alphanumeric symbols enclosed in a single quote is a string data.

A String is an array of character.

Example

‘IBEMA’, ‘7743789’ etc.

‘IBEMA’==[‘I’, ’B’, ‘E’, ‘M’, ‘A’]

Page 23: MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008. MATLAB is too broad for our purposes

A MATLAB matrix is a rectangular array of numbers

Scalars and vectors are regarded as special cases of matrices

MATLAB allows you to work with a whole array at a time

Page 24: MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008. MATLAB is too broad for our purposes

You can create matrices (arrays) of any size using a combination of the methods for creating vectors

List the numbers using ‘,’ to separate each column and then ‘;’ to define a new row

24

Page 25: MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008. MATLAB is too broad for our purposes

You can also use built in functions to create a matrix >> A = zeros(2, 4)

creates a matrix called A with 2 rows and 4 columns containing the value 0

>> A = zeros(5) or >> A = zeros(5, 5) creates a matrix called A with 5 rows and 5

columns You can also use: >> ones(rows, columns) >> rand(rows, columns)

Note: MATLAB always refers to the first value as the number of Rows then the second as the number of Columns

25

Page 26: MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008. MATLAB is too broad for our purposes

The matrix indices begin from 1 (not 0 (as in C))

The matrix indices must be positive integer

Given:

A(-2), A(0) Error: ??? Subscript indices must either be real positive integers or logicals.

A(4,2) Error: ??? Index exceeds matrix dimensions.

Page 27: MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008. MATLAB is too broad for our purposes

A vector is a list of numbers Use square brackets [] to contain the numbers

To create a row vector use ‘,’ to separate the content

27

Page 28: MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008. MATLAB is too broad for our purposes

To create a column vector use ‘;’ to separate the content

28

Page 29: MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008. MATLAB is too broad for our purposes

A row vector can be converted into a column vector by using the transpose operator ‘

29

Page 30: MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008. MATLAB is too broad for our purposes

Create vector with equally spaced intervals >> x=0:0.5:pi

x =

0 0.5000 1.0000 1.5000 2.0000 2.5000 3.0000

Create vector with n equally spaced intervals >> x=linspace(0, pi, 7)

x =

0 0.5236 1.0472 1.5708 2.0944 2.6180 3.1416

Equal spaced intervals in logarithm space

>> x=logspace(1,2,7)

x =

10.0000 14.6780 21.5443 … 68.1292 100.0000

Note: MATLAB uses pi to represent , uses i or j to represent imaginary unit

Page 31: MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008. MATLAB is too broad for our purposes

A cell-array is a special type of array, where the elements can be of different data types.

They are distinguished from the simple arrays by using braces, instead of square brackets for enclosing the elements.

Example

{‘AHMED’, 34, ‘RING ROAD’}

A Cell array can contain other cell-arrays

Page 32: MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008. MATLAB is too broad for our purposes

A Structure is also a different data type to store various related data types using meaningful field names.

They are written in name-value combination using the word struct.

Example

Struct(‘field1’, data1, ‘field2’, data2, ‘field3’, data3)

Page 33: MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008. MATLAB is too broad for our purposes

Handling data directly in computation is inconvienient for varoius reasons so data are assigned names called variables Variables Names 1.Can be any string of upper and lower case letters along with numbers and underscores but it must begin with a letter

2.Should not be keywords or program name as IF, WHILE, ELSE, END, SUM, etc.

3.Names are case sensitive

Value This is the data that is associated to the variable; The data is accessed by using the name. Variables have the type of the last thing assigned to them Re-assignment is done silently – there are no warnings if you overwrite a variable with something of a different type.

33

Page 34: MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008. MATLAB is too broad for our purposes

Valid

Mean3, avg1, first_value etc

Invalid

1stvalue, second-value, dec.hex etc

Page 35: MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008. MATLAB is too broad for our purposes

No need for types. i.e.,

All variables are created with double precision unless specified and they are matrices.

After these statements, the variables are 1x1 matrices with double precision

int a; double b;

float c;

Example: >>x=5;

>>x1=2;

Page 36: MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008. MATLAB is too broad for our purposes

Singletons To assign a value to a variable use the equal symbol ‘=‘ >> A = 32

To find out the value of a variable simply type the name in

36

Page 37: MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008. MATLAB is too broad for our purposes

To make another variable equal to one already entered >> B = A

The new variable is not updated as you change the original value

Note: using ; suppresses output

37

Page 38: MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008. MATLAB is too broad for our purposes

The value of two variables can be added

together, and the result displayed… >> A = 10 >> A + A

…or the result can be stored in another

variable >> B = A + A

38

Page 39: MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008. MATLAB is too broad for our purposes

You can use the command “clear all” to delete all the variables present in the workspace

You can also clear specific variables using:

>> clear Variable_Name

39

Page 40: MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008. MATLAB is too broad for our purposes

(+) Addition

(-) Subtraction

(*) Multiplication

(/) Division

(^) Power

(’) Complex conjugate transpose

Page 41: MATLAB FOR ENGINEERS CREDITS: 4 · *Steven T Karris Introduction to Simulink with Engineering Applications, 2nd edition, Orchard Publication, 2008. MATLAB is too broad for our purposes

THANK YOU