lecture01-matlab v2

31
Learner’s Guide to MATLAB® Chapter 1 : Getting Started

description

lecture 1 for matlab

Transcript of lecture01-matlab v2

Page 1: lecture01-matlab v2

Learner’s Guide to MATLAB®

Chapter 1 : Getting Started

Page 2: lecture01-matlab v2

Chapter Outline

• Starting MATLAB• Familiarization with MATLAB Desktop• Using MATLAB as a Calculator• Help features in MATLAB• Elementary Math Built-in Functions• Working with Variables

Page 3: lecture01-matlab v2

Starting MATLAB

Page 4: lecture01-matlab v2

MATLAB Desktop

Current Directory

Workspace

Command History

Command Window

Page 5: lecture01-matlab v2

Using MATLAB as a Calculator

>> 5 + 12/5ans =

7.4>> 27^(1/3) + 32^0.2ans =

5>> sin(pi/3) ans = 0.8660>> exp(sin(pi/3)) ans = 2.3774

Page 6: lecture01-matlab v2

Order of Precedence

Matrix Operators Array operators() parentheses

' comp. transpose .' array transpose

power . array power

* multiplication .* array mult.

/ division ./ array division

\ left division

+ addition

- subtraction

(In order of precedence)

Page 7: lecture01-matlab v2

Elementary Math Built-in Functions

How do we know the syntax for using the following functions?

Trigonometric – COS, ACOS, EXP, SIN, ASINExponential – LOG, EXP, SQRTComplex – ABS, ANGLE, CONJDiscrete Maths – FACTOR, PRIMES, GCD

>> doc elfun

- MATLAB has a very large library of built-in functions

Page 8: lecture01-matlab v2

Help Features with MATLAB

You can use the MATLAB help features to understand the syntax in using some of the MATLAB functions if you already know the function name

Type any of the following options at the MATLAB Command Line:

• help function_name• helpwin function_name• doc function_name• Example:

help plot; doc plot;

Page 9: lecture01-matlab v2

Accessing the Help Browser Via the Startup Menu

Page 10: lecture01-matlab v2

Creating Variables in MATLAB

>> x = 3;>> y = 4i;>> z = (x+y)*(x-y)z = 25

>> t = 0.1t = 0.1000>> x = sin(3*t + pi/2)x = 0.9553

Variable name Assign operator Value

Page 11: lecture01-matlab v2

Type of MATLAB Arrays• Scalars ~ just single element arrays

a=[1]

• Row Vectors ~ separated by a blank character or a commab=[1 2 3 4] or b=[1,2,3,4]

• Column vectors ~ separated by semicolons or transpose a row vector to column vector by apostrophe

c=[1;2;3;4] or c=[1 2 3 4]’

• MATRIX ~ semicolon ‘;’ to start a new lined=[1,2,3,4 ; 5,6,7,8]

Page 12: lecture01-matlab v2

Strings as Variables- A string is an array of characters and can include letters, digits, other

symbols and spaces- Created using single quote delimiter (')

>> str = 'Hi there,'str =Hi there, >> str2 = 'Isn''t MATLAB great?'str2 =Isn't MATLAB great?

1x9 vectorstr = H i t h e r e ,

- Each character is a separate matrix element (16 bits of memory per character)

- Indexing same as for numeric arrays

Page 13: lecture01-matlab v2

MATLAB Workspace Browser and Array Editor

• The MATLAB Workspace Browser allows you to access to the data or variables created in the current MATLAB session.

• By selecting the Workspace tab or use the functions who and whos.

• The MATLAB Array Editor is use to view and edit a visual representation of variables in the workspace.

How do I locate my variables created in

MATLAB?

Page 14: lecture01-matlab v2

What is Elementary Math Built-in Functions

Trigonometric – COS, ACOS, EXP, SIN, ASINExponential – LOG, EXP, SQRTComplex – ABS, ANGLE, CONJDiscrete Maths – FACTOR, PRIMES, GCD

How do I use or call Basic MATLAB

functions?

Page 15: lecture01-matlab v2

Sample Problem 1 : Heat Transfer

An object with an initial temperature of T0 that is placed at time t=0 inside a chamber that has a constant temperature of TS, will experience a temperature change according to the equation:

T = TS + (T0 – TS) e-kt

where T is the temperature of the object at time t, and k is a constant. A soda can at a temperature of 120°F (was left in the car) is placed inside a refrigerator where the temperature is 38°F. Determine, to the nearest degree, the temperature of the can after three hours. Assume k=0.45. First, define all the variables and then calculate the temperature using one MATLAB command

Page 16: lecture01-matlab v2

Commands for Managing Variables

MATLAB Function UsageCD/PWD, LS/DIR

WHAT Displays files in a directory (grouped by type)

! Invoke DOS commands from MATLAB

CLEAR Remove function or variable from memory

CLEAR X Y Z Remove only variables X,Y & Z from memory

WHO, WHOS

SIZE

Navigating directories

List workspace variables

Returns the size of a matrix (rows and columns)

Page 17: lecture01-matlab v2

Introduction – (1)• So… what is MATLAB and what’s it all about?

• MATLAB: MATrix LABoratory• Created in 1970 by a dude named Cleve Moler• Was (and still is) used extensively at Stanford University and the University of New Mexico• Why? To make calculating the following things a lot

easier!• Matrix Theory• Linear Algebra• Numerical Analysis, etc..

17

Page 18: lecture01-matlab v2

Introduction – (2)• MATLAB is selected as a numerical analysis tool over languages like C and Java because:• Very EASY programming language• Powerful graphics capabilities• Very sleek and interactive interface• Great for general scientific and engineering computation

18

Page 19: lecture01-matlab v2

Variables and Basic Commands – (1)• One GREAT thing about MATLAB:

• MATLAB is a programming language that is dynamically typed… what does this mean?

• You can declare variables and initialize them without specifying what type they are• MATLAB automatically figures this out for you, or you can choose to

manually override the type• Example:

• C or Java way: int nikhil = 1, double jenny = 2• MATLAB way: nikhil = 1, jenny = 2

19

Page 20: lecture01-matlab v2

Variables and Basic Commands – (2)• When you want to assign something to a variable, use the = sign

• When you assign something to a variable, MATLAB initializes & automatically declares it

• Guidelines for variable names:• All must be single words, no spaces• Must begin with a letter, numbers or the underscore

character ( _ )• Variable names are case sensitive

• i.e nikhil is NOT the same as Nikhil• i.e muffin is NOT the same as mUFfin

• Names can be up to 19 characters in length

20

Page 21: lecture01-matlab v2

Variables and Basic Commands – (3)• Some valid variable names:

• voltage• valueOfR1• Ron_and_Mauro• _Alan2007_

• Some invalid variable names (why are these invalid?):• 123• value of R1• 3v• X#*()$#$!!!

21

Page 22: lecture01-matlab v2

Variables and Basic Commands – (4)• Left panel: Current Directory / Workspace

• A) Shows you directory structure to access working directory (more on this later)

• B) Shows you all of the variables that have been created and can be accessed

• Right: Command Prompt• Enter commands and variable declarations here• Commands without a semicolon ( ; ) echo your

command to screen• Commands with a semicolon suppress that output

22

Page 23: lecture01-matlab v2

Variables and Basic Commands – (5)• Can enter commands either:

• One at a time: The end of each command, press ENTER (carriage return)

• Multiple commands in one line:• Suppress echoing: Use semicolons to separate each command

on the line• Enable echoing: Use commas ( , ) to separate each command

on the line• Typing in a variable by itself and pressing ENTER will redisplay the variable

• Entering a value, pressing ENTER, and not assigning it to anything, the value will be automatically assigned to a variable called ans (answer)

23

Page 24: lecture01-matlab v2

Variables and Basic Commands – (6)

24

Page 25: lecture01-matlab v2

Variables and Basic Commands – (7)• who command: Shows you all of the variables created by

you• You can also check the workspace as well

• clear command: Clears all of the variables shown in the workspace, and you start from scratch

• clc command: Flushes the command prompt• Variables will still be in the workspace, but it clears the command

prompt screen

25

Page 26: lecture01-matlab v2

Variables and Basic Commands – (8)

26

Page 27: lecture01-matlab v2

Variables and Basic Commands – (9)• Can also declare complex numbers too:

• Can add, subtract, multiply and divide• You can use i or j to declare complex numbers• Of course… you can also add, subtract, multiply, divide

and raise to a power normal numbers

27

Page 28: lecture01-matlab v2

Variables and Basic Commands – (10)

28

Page 29: lecture01-matlab v2

Variables and Basic Commands – (11)

• Command History window: Used to keep track of the commands you ran recently

• You can alsodouble click on any of the commandsto re-run them again

• You can also pressthe up & down keysto cycle throughthe commands aswell in the commandprompt

29

Page 30: lecture01-matlab v2

Format output

Page 31: lecture01-matlab v2