35
MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Email: [email protected] Dept of Mechanical Engineering LSU

MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Email:[email protected]@lsu.edu Dept of Mechanical Engineering LSU

Embed Size (px)

Citation preview

Page 1: MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Email:rmadaz1@lsu.edurmadaz1@lsu.edu Dept of Mechanical Engineering LSU

MATLAB Tutorials

Session IIntroduction to MATLAB

Rajeev MadazhyEmail: [email protected]

Dept of Mechanical Engineering

LSU

Page 2: MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Email:rmadaz1@lsu.edurmadaz1@lsu.edu Dept of Mechanical Engineering LSU

Tutorial Outline

Session 1: Introduction to MATLAB

Session 2: Programming in MATLAB

Session 3: Plotting in MATLAB

Session 4: Mathematical Applications using MATLAB

Session 5: Mathematical Applications using MATLAB (cont.)

Session 6: Engineering Applications using MATLAB

Session 7: Open for specific problems

Department of Mechanical Engineering, LSU

Page 3: MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Email:rmadaz1@lsu.edurmadaz1@lsu.edu Dept of Mechanical Engineering LSU

Session Outline

Overview of MATLAB Using the built-in functions Constants and Variables Basic Arithmetic Operations Matrices and Computation Solving simple equations

Department of Mechanical Engineering, LSU

Page 4: MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Email:rmadaz1@lsu.edurmadaz1@lsu.edu Dept of Mechanical Engineering LSU

What is MATLAB?

MATLAB is basically a computing tool for engineers and mathematicians.

Features include:

Interactive programming environment

Graphing facilities

Built-in functions

Toolboxes designed for specialized field of applications

Department of Mechanical Engineering, LSU

Page 5: MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Email:rmadaz1@lsu.edurmadaz1@lsu.edu Dept of Mechanical Engineering LSU

Background info about MATLAB

MATLAB is an acronym for Matrix Laboratory

Cleve Moler was the original creator of MATLAB

MATLAB is now a product of Math Works Inc. co-

founded by Cleve Moler and Jack Little.

First version of it appeared as early as 1981

Initially written for matrix computations, it contained

only 3000 lines of source code written in FORTRAN

Department of Mechanical Engineering, LSU

Page 6: MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Email:rmadaz1@lsu.edurmadaz1@lsu.edu Dept of Mechanical Engineering LSU

What can MATLAB do for you?

Produce effective solutions

Compact the time invested in analysis and development

Reduce project costs

Quickly test and compare multiple alternatives

Modeling and simulation

……………………………………………….ad infinitum

Department of Mechanical Engineering, LSU

Page 7: MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Email:rmadaz1@lsu.edurmadaz1@lsu.edu Dept of Mechanical Engineering LSU

Getting Started…….

Prerequisites:

Basic knowledge of linear algebra

Some computer experience (helpful but not necessary)

Curiosity!!!!!

Department of Mechanical Engineering, LSU

Page 8: MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Email:rmadaz1@lsu.edurmadaz1@lsu.edu Dept of Mechanical Engineering LSU

Basic Arithmetic Operations

Addition : a+b

Subtraction : a- b

Multiplication : a*b

Division : a/b

Power : a^b

Note the symbols being used for the operators.

Department of Mechanical Engineering, LSU

Page 9: MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Email:rmadaz1@lsu.edurmadaz1@lsu.edu Dept of Mechanical Engineering LSU

In MATLAB….

Department of Mechanical Engineering, LSU

Page 10: MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Email:rmadaz1@lsu.edurmadaz1@lsu.edu Dept of Mechanical Engineering LSU

Naming Constants and Variables

The value of is stored in MATLAB as pi

The contents of the variable ans change with every answer

Variable names can contain upto 31 characters

Every variable must start off with a letter

When you write the semicolon ‘;’ at the end of the statement

the computer will not display the result after you press the

return key.

MATLAB is case sensitive. A & a are identified different

Department of Mechanical Engineering, LSU

Page 11: MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Email:rmadaz1@lsu.edurmadaz1@lsu.edu Dept of Mechanical Engineering LSU

In MATLAB….

Department of Mechanical Engineering, LSU

Page 12: MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Email:rmadaz1@lsu.edurmadaz1@lsu.edu Dept of Mechanical Engineering LSU

Built-in math functions

abs(x) absolute value of x

Sin(x) sine of x (where x is in radians)

asin(x) arcsine of x

exp(x) exponential of x

log(x) natural logarithm of x

log10(x) base 10 logarithm of x

sqrt(x) square root of x

imag(x) imaginary part of complex number x

real(x) real part of the complex number xDepartment of Mechanical Engineering, LSU

Page 13: MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Email:rmadaz1@lsu.edurmadaz1@lsu.edu Dept of Mechanical Engineering LSU

In MATLAB…..

To get more information about other functions type at command

prompt: >>help elfun

Department of Mechanical Engineering, LSU

Page 14: MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Email:rmadaz1@lsu.edurmadaz1@lsu.edu Dept of Mechanical Engineering LSU

Vectors and matrices

An ordered collection of numbers separated by commas or

spaces can be defined in MATLAB as an array or vector

Example: A=[1 2 3 4 5] is an array of 1 row and 5 columns

A matrix is defined as an array of n n elements

The elements of an array are identified by their index

The number of elements of an array can be retrieved using

the length function

The addition and subtraction are defined only for arrays of

the same length

Department of Mechanical Engineering, LSU

Page 15: MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Email:rmadaz1@lsu.edurmadaz1@lsu.edu Dept of Mechanical Engineering LSU

In MATLAB….

Department of Mechanical Engineering, LSU

Page 16: MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Email:rmadaz1@lsu.edurmadaz1@lsu.edu Dept of Mechanical Engineering LSU

Vectors and matrices (cont..)

Array of equally spaced elements can be entered by stating

the first element, the increment and the last element

When the increment is 1 it can be omitted

Department of Mechanical Engineering, LSU

Page 17: MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Email:rmadaz1@lsu.edurmadaz1@lsu.edu Dept of Mechanical Engineering LSU

Vectors and matrices (cont..)

Array multiplication in MATLAB is indicated by “.*”

For example : A.*B is the product of two arrays

Array division in MATLAB is indicated by “./”

For example : A./B is the quotient of two arrays

We can also raise the elements of the array to the same

power by using the operator “.^”

Many built-in functions can be applied to the array by

simply using the array name as the argument

Department of Mechanical Engineering, LSU

Page 18: MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Email:rmadaz1@lsu.edurmadaz1@lsu.edu Dept of Mechanical Engineering LSU

In MATLAB….

Department of Mechanical Engineering, LSU

Page 19: MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Email:rmadaz1@lsu.edurmadaz1@lsu.edu Dept of Mechanical Engineering LSU

Simple plots using arrays

Department of Mechanical Engineering, LSU

Page 20: MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Email:rmadaz1@lsu.edurmadaz1@lsu.edu Dept of Mechanical Engineering LSU

Plot result…..

Department of Mechanical Engineering, LSU

Page 21: MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Email:rmadaz1@lsu.edurmadaz1@lsu.edu Dept of Mechanical Engineering LSU

Department of Mechanical Engineering, LSU

Row Vectors and Column Vectors

One dimensional arrays can be considered as row vectors

Column vectors are generated by entering an array of

numbers separated by semicolons “;”

Row vectors and column vectors can be converted into each

other by transposition operation “ ‘ “

Page 22: MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Email:rmadaz1@lsu.edurmadaz1@lsu.edu Dept of Mechanical Engineering LSU

Department of Mechanical Engineering, LSU

In MATLAB….

Page 23: MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Email:rmadaz1@lsu.edurmadaz1@lsu.edu Dept of Mechanical Engineering LSU

Department of Mechanical Engineering, LSU

Defining a matrix

Matrices are defined as two dimensional arrays

Different rows are separated by semi colons or type

each row separately and press enter

Any element of a matrix is identified by two indices:

the first indicates the row and the second indicates

the column

Page 24: MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Email:rmadaz1@lsu.edurmadaz1@lsu.edu Dept of Mechanical Engineering LSU

Department of Mechanical Engineering, LSU

In MATLAB….

Page 25: MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Email:rmadaz1@lsu.edurmadaz1@lsu.edu Dept of Mechanical Engineering LSU

Department of Mechanical Engineering, LSU

Cont…

The number of rows and columns in a matrix is retrieved by

size function

We can create a new matrix by putting two matrices besides

each other

Page 26: MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Email:rmadaz1@lsu.edurmadaz1@lsu.edu Dept of Mechanical Engineering LSU

Department of Mechanical Engineering, LSU

Cont…

We can also extract a sub matrix out of a matrix

Page 27: MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Email:rmadaz1@lsu.edurmadaz1@lsu.edu Dept of Mechanical Engineering LSU

Cont…

Department of Mechanical Engineering, LSU

We can generate a matrix with the same number of rows and

columns as A, with all the elements equal to one, using the

command ones

Similarly using the command zeroes we can get all the

elements of the matrix as zeroes

Page 28: MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Email:rmadaz1@lsu.edurmadaz1@lsu.edu Dept of Mechanical Engineering LSU

Department of Mechanical Engineering, LSU

Polynomials….

Polynomial is expressed in MATLAB as an array of

coefficients, A=[1 2 -4]

The equation is solved by using the function roots(A)

The coefficients of the polynomial can be retrieved from its roots

through the function poly()

The value of a polynomial at point x is obtained by function

polyval(A,x) where A is the array, the coefficients of the

polynomial are stored

422 xx

Page 29: MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Email:rmadaz1@lsu.edurmadaz1@lsu.edu Dept of Mechanical Engineering LSU

Department of Mechanical Engineering, LSU

In MATLAB….

Page 30: MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Email:rmadaz1@lsu.edurmadaz1@lsu.edu Dept of Mechanical Engineering LSU

Department of Mechanical Engineering, LSU

Complex Numbers

Page 31: MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Email:rmadaz1@lsu.edurmadaz1@lsu.edu Dept of Mechanical Engineering LSU

Department of Mechanical Engineering, LSU

Solving systems of Equations…

x1+ 2x2 + x3 = 4

2x1+ 3x2- x3 = 9

4x1- x2 +2x3= 0

214

132

121

A

6

9

4

B

BAX 1BXA

Page 32: MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Email:rmadaz1@lsu.edurmadaz1@lsu.edu Dept of Mechanical Engineering LSU

Department of Mechanical Engineering, LSU

In MATLAB….

Page 33: MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Email:rmadaz1@lsu.edurmadaz1@lsu.edu Dept of Mechanical Engineering LSU

Department of Mechanical Engineering, LSU

Looking ahead….

There are several books written on MATLAB where a good

Description about the functions is given.

Introduction to MATLAB for Engineers and Scientists

Delores M. Etter & David C. Kentucky

A guide to MATLAB: for Beginners and experienced users

Brian R. Hunt, Ronald L. Lipsman & JM Rosenberg

Page 34: MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Email:rmadaz1@lsu.edurmadaz1@lsu.edu Dept of Mechanical Engineering LSU

Department of Mechanical Engineering, LSU

Next Session….

Programming in MATLAB :

How to use M Files

Programming functions

Control Structures

Displaying output in different formats

Page 35: MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Email:rmadaz1@lsu.edurmadaz1@lsu.edu Dept of Mechanical Engineering LSU

Thank You

Department of Mechanical Engineering, LSU