introduction to matlab lecture 1 of 4

Post on 16-Jul-2015

92 Views

Category:

Education

9 Downloads

Preview:

Click to see full reader

TRANSCRIPT

This course will:› Introduce the basic functionality of MATLAB

› Demonstrate its utility in scientific research

› Identify interesting concepts and useful techniques in scientific computing

By the end of the course, you should have the skills necessary to apply MATLAB to your research and learn how to extend its capabilities

2

Introduction: What is Matlab, History of Matlab, strengths, weakness

Getting familiar with the interface: Layout, Pull down menus

Creating and manipulating objects: Variables (scalars, vectors, matrices, text strings), Operators (arithmetic, relational, logical) and built-in functions

Flow control: Conditional and iteration blocks

Data transport: Importing into/Exporting from the workspace

Plotting with Matlab

M-files and UserDefined functions

3

Introduction:

› What is Matlab,

› History of Matlab,

› strengths, weakness

Getting familiar with the interface:

› Layout,

› Pull down menus

Creating and manipulating variables

MATLAB is a program for doing numerical

computation.

It was originally designed for solving linear

algebra type problems using matrices.

Its name is derived from MATrix LABoratory

5

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.

6

Application kernel, e.g., matlab-7.7

Computational engine

Built-in low level algorithms (no source code)

Includes a variety of graphical capabilities

Suite with contemporary releases, e.g. R2008b

Programming language

Designed to solve problems numerically

(Particularly good for matrix operations)

Easy to learn and use (Simplier syntax rules than

Fortran or C)

7

Auxiliaries – usually bundled with kernel

› Simulink (dynamic systems modeling)

› Stateflow (event and logic-driven modeling)

Toolboxes - designed for specific applications

› Specialized collections of MATLAB source files

Developed or supported by MathWorks Inc.

Signal Processing, Optimization, Control System, etc.

Downloadable third party freeware

Multiple precision arithmetic, robotics, etc.

Resource: http://www.mathworks.com/matlabcentral/

8

Originally written in Fortran› Author: Cleve Moler, Univ. of New Mexico

Mathworks, Inc. founded to further develop it.› Incorporated in 1984

› Core is written in C

› Graphical interface is written in Java

› Runs on many platforms Unix – Solaris, SGI, AIX, Digital Unix

Linux

Windows

Mac OS

9

MATLAB is relatively easy to learn

MATLAB code is optimized to be relatively quick when performing matrix operations

MATLAB may behave like a calculator or as a programming language

MATLAB is interpreted, errors are easier to fix

Although primarily procedural, MATLAB does have some object-oriented elements

10

MATLAB is NOT a general purpose

programming language

MATLAB is an interpreted language (making it

for the most part slower than a compiled

language such as C++)

MATLAB is designed for scientific computation

and is not suitable for some things (such as

parsing text)

11

Command window: Type your instructions here

and press ENTER to execute them.

12

The MATLAB environment is command oriented somewhat like UNIX.

A prompt (>>) appears on the screen and a MATLAB statement can be entered. When the <ENTER> key is pressed, the statement is executed, and another prompt appears.

If a statement is terminated with a semicolon ( ; ), no results will be displayed. Otherwise results will appear before the next prompt.

13

Example

14

» a=5;

» b=a/2

b =

2.5000

»

Command history: a list of instructions executed by MATLAB is shown here.

Workspace: shows a list of variables created by MATLAB. As you can see, the value of ‘A’ is shown.

15

16

17

18

19

20

21

22

23

Help› Launch Matlab Help Window

Help > Product Help > MATLAB

› See Demos Help > Demos

Or Type help at the Matlab prompt or help followed by a function name for help on a specific function

Online:› Online documentation for Matlab at the MathWorks

website

http://www.mathworks.com/access/helpdesk/help/techdoc/matlab.html

› There are also numerous tutorials online that are easily found with a web search.

24

25

26

27

“%” is the neglect sign for Matlab (equaivalentof “//” in C). Anything after it on the same line is neglected by Matlab compiler.

Sometimes slowing down the execution is done deliberately for observation purposes. You can use the command “pause” for this purpose

28

>> clear % clears all objects in

workspace

>> clear x y % clears values of

objects x and y

>> clc % clears command

window scroll buffer

>> which <filename> % finds first occurrence

in search path

>> who % lists all objects in the

workspace

>> <command> ; % semicolon →

% execute, no display

in command window

29

>> <control> c % stops program execution

>> <control> q % stops execution and exits Matlab

>> <begin command text > … % three dots → continue

to next line

>> whos % List known variables plus

their size

>> help sqrt % Help on using sqrt

>> lookfor sqrt % Search for keyword sqrt

in on MATLABPATH.

>> what ('directory') % List MATLAB files in

directory

30

>> dir %List all files in current directory

>> ls % Same as dir

>> type test %Display the content of test.m

in command window

>> delete test % Delete test.m

>> cd a: % Change directory to a:

>> chdir a: % Same as cd

>> pwd % Show current directory

>> which test % Display directory path to

‘closest’ test.m

31

Top navigation bar with pull-down menus File

Edit

Debug

Desktop

Window

Help

Toolbar with icons

Current Directory path identified

32

File› New (create new m-file, figure, variable)

› Import Data

› Set Path (folders, directories to search)

› Preferences > Command Window

(number format, scroll buffer size)

Edit› Find, Delete, Copy, Paste, etc.

33

Desktop

› Choose layout for display of windows

Original on launch Desktop > Save Layout (if you like it) with a name

Modify original if desired Desktop > Save Layout (if you like it) with a name

Default layout (new in R2008b) You can always start over from this configuration

Desktop > Desktop Layout > Default

34

Numerical values

› Sequence of base 10 digits Binary, hex representations are character strings

› Period as radix point for floating point numbers

› Lower case e for power of 10 exponent

Example: >> x = 123.456e-07

→ x = 0.0000123456

[ not (123.456)*(2.71828,,,)-7 ]35

Complex numbers

› i and j initially set to the square root of -1

Imaginary part has i or j appended

(1+1i) is a single complex number object

(1+1*i) is a sum of one and current value of i

36

Matrices

› Rectangular arrays of elements

Indexed by row, then by column

Example: >> A = [ a(1,1) a(1,2); a(2,1) a(2,2)]

37

Boolean logicals (Bernoulli variables)› Truth Values

1 for true; 0 for false

› Primary logic symbols & logical AND == logical equal | logical inclusive OR ~ logical NOT

› Compound logic symbols < Less Than <= Less Than or Equal > Greater Than >= Greater Than or Equal == Equal To ~= Not Equal To

38

MATLAB also supports some logical

functions.

› any(x) returns 1 if any element of x is nonzero

› all(x) returns 1 if all elements of x are nonzero

› isnan(x) returns 1 at each NaN in x

› isinf(x) returns 1 at each infinity in x

› finite(x) returns 1 at each finite value in x

39

Separators and delimiters

[ ] square brackets -- vector and matrix delimiters

{ } curly brackets -- cell delimiter

( ) parentheses * Grouping in compound expressions

* Vector and matrix element indices

* Function argument

: colon -- index range separator ; semicolon -- matrix row separator

<space> matrix column separator

, comma -- matrix index, function argument separator, row vector element separator

' ' single quotes -- demarcation of character strings

40

Data Types:

41

Have not to be previously declared type in a variable name and type in its value.

MATLAB will decide on the data type automatically, so you don’t have to declare its data type.

Variable names can contain up to 63 characters

Variable names must start with a letter followed by letters, digits, and underscores.

Variable names are case sensitive

42

Example of illegal variable names:

43

Matlab Special Variables

ans Default variable name for results

pi Value of π

eps Smallest incremental number

inf Infinity

NaN Not a number e.g. 0/0

realmin The smallest usable positive real number

realmax The largest usable positive real number

44

Assignment = a = b (assign b to a)

Addition + a + b

Subtraction - a -b

Multiplication * or.* a*b or a.*b

Division / or ./ a/b or a./b

Power ^ or .^ a^b or a.^b

45

Type the following expressions into MATLAB at the command window, and observe the results:

1. >> for = 5

2. >> else =6

3. >> cos = 3;

>> cos(0)

4. >> A = [1,2,3];>> sum(A)

>> sum = 7;

>> sum(A)

Type the following expressions into MATLAB at the command window, and observe the results:

5. >> 5>2

6. >> 5<4

7. >> 1.5<=1.5

8. >> 2>=pi

9. >> 1.8==1.801

10. >> 1.8~=2

11. >> 1.8==1.80000000000000000000000001 (see what happen)

top related