26 - 3/20/2000ame 150l1 m atlab ®. 26 - 3/20/2000ame 150l2 m atlab ® vs. fortran fortran positives...

34
26 - 3/20/2000 AME 150L 1 AME 150 L MATLAB ®

Upload: isabella-preston

Post on 03-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 26 - 3/20/2000AME 150L1 M ATLAB ®. 26 - 3/20/2000AME 150L2 M ATLAB ® vs. Fortran Fortran Positives –First Compiler –Legacy Codes –Efficient Numerically

26 - 3/20/2000 AME 150L 1

AME 150 L

MATLAB®

Page 2: 26 - 3/20/2000AME 150L1 M ATLAB ®. 26 - 3/20/2000AME 150L2 M ATLAB ® vs. Fortran Fortran Positives –First Compiler –Legacy Codes –Efficient Numerically

26 - 3/20/2000 AME 150L 2

MATLAB® vs. Fortran• Fortran Positives

– First Compiler

– Legacy Codes

– Efficient Numerically

– Portability

• Fortran Negatives– Debugging Hard

– Cumbersome

– Object Orientation pasted-on

• MATLAB® Positives– Interactive mode– Wealth of 3rd party

“notebooks”– Matrix Oriented– Efficient I/O and Graphics

• MATLAB® Negatives– Syntax cumbersome– Command Line oriented

Page 3: 26 - 3/20/2000AME 150L1 M ATLAB ®. 26 - 3/20/2000AME 150L2 M ATLAB ® vs. Fortran Fortran Positives –First Compiler –Legacy Codes –Efficient Numerically

26 - 3/20/2000 AME 150L 3

MATLAB® Usage• For the next 4 lectures

– Slide will introduce topic• Useful in reviewing lecture and practicing

• Can pay attention to on-line demonstration

– Demonstration of usage on-line• Will show details of usage

• Shop proper and improper operation

• First -- Learn to use MATLAB® Help

Page 4: 26 - 3/20/2000AME 150L1 M ATLAB ®. 26 - 3/20/2000AME 150L2 M ATLAB ® vs. Fortran Fortran Positives –First Compiler –Legacy Codes –Efficient Numerically

26 - 3/20/2000 AME 150L 4

Basic Features• Simple Mathematics

– MATLAB® is like a calculator• Type in expression and get answer (ans=)

• Suppress answer with trailing semi-colon ;

• Can define variables (like in Fortran)

– MATLAB® operations• Fortran-like +, -, * and functions

• Exponentiation is ^ (like Basic or Excel)

• 2 Divisions / and \ ( 56/8 same as 8\56 )

Page 5: 26 - 3/20/2000AME 150L1 M ATLAB ®. 26 - 3/20/2000AME 150L2 M ATLAB ® vs. Fortran Fortran Positives –First Compiler –Legacy Codes –Efficient Numerically

26 - 3/20/2000 AME 150L 5

Hierarchy of operations

• Similar to Fortran– Expression evaluated left to right– Exponentiation has highest precedence– Multiplication & Division next & equal– Addition & Subtraction next & equal

• Expressions in Parentheses evaluated first– Start with Innermost & proceed Outward

Page 6: 26 - 3/20/2000AME 150L1 M ATLAB ®. 26 - 3/20/2000AME 150L2 M ATLAB ® vs. Fortran Fortran Positives –First Compiler –Legacy Codes –Efficient Numerically

26 - 3/20/2000 AME 150L 6

Naming Variables

• Variable names are Case sensitive (!!!)

• Names can have up to 31 characters

• Names must start with a letter

• Names can contain letter, numerical digits, and underscore (no special characters or blanks)

• i and j are special (they are ) 1

Page 7: 26 - 3/20/2000AME 150L1 M ATLAB ®. 26 - 3/20/2000AME 150L2 M ATLAB ® vs. Fortran Fortran Positives –First Compiler –Legacy Codes –Efficient Numerically

26 - 3/20/2000 AME 150L 7

Special Variablesi or j - imaginary base

ans - unnamed answer

pi - 3.1416…

eps - machine epsilon(1 + eps)>1 (smallest)

inf - stands for infinity

NaN or nan - not a number

flops - number of floating point operations

nargin - number of function input arguments

nargout - number of function output arguments

realmin - smallest usable positive real number

realmax - largest usable positive real number

Page 8: 26 - 3/20/2000AME 150L1 M ATLAB ®. 26 - 3/20/2000AME 150L2 M ATLAB ® vs. Fortran Fortran Positives –First Compiler –Legacy Codes –Efficient Numerically

26 - 3/20/2000 AME 150L 8

Features of calculations

• Complex numbers are naturalEDU» sqrt(-1)

ans =

0+ 1.0000i

• Pi is predefined in a variableEDU» atan(1)*4-pi

ans =

0

Page 9: 26 - 3/20/2000AME 150L1 M ATLAB ®. 26 - 3/20/2000AME 150L2 M ATLAB ® vs. Fortran Fortran Positives –First Compiler –Legacy Codes –Efficient Numerically

26 - 3/20/2000 AME 150L 9

Cautions

• Special Variables Can Be Redefined!EDU» pi

ans =

3.1416

EDU» pi=5

pi =

5

EDU» atan(1)*4-pi

ans =

-1.8584

Page 10: 26 - 3/20/2000AME 150L1 M ATLAB ®. 26 - 3/20/2000AME 150L2 M ATLAB ® vs. Fortran Fortran Positives –First Compiler –Legacy Codes –Efficient Numerically

26 - 3/20/2000 AME 150L 10

More Features

• Clear - resets special values and any other defined variables

• Convenience often misleadsEDU» -1*(1/3) EDU» -1^(1/4)

ans = ans =

-0.3333 -1

EDU» -1^(1/2) EDU» sqrt(sqrt(-1))

ans = ans =

-1 0.7071+ 0.7071i

Page 11: 26 - 3/20/2000AME 150L1 M ATLAB ®. 26 - 3/20/2000AME 150L2 M ATLAB ® vs. Fortran Fortran Positives –First Compiler –Legacy Codes –Efficient Numerically

26 - 3/20/2000 AME 150L 11

Comments & Punctuation

• Comments start with %

• Lines are continued by three periods ...• Comments cannot be continued (but can use

% on next line to make “new” comment)

• Cannot continue in middle of a variable name

Page 12: 26 - 3/20/2000AME 150L1 M ATLAB ®. 26 - 3/20/2000AME 150L2 M ATLAB ® vs. Fortran Fortran Positives –First Compiler –Legacy Codes –Efficient Numerically

26 - 3/20/2000 AME 150L 12

Complex Numbers

• Complex numbers have a magnitude and angle (argument in mathematics)

M M ei = a + b i

where

2 2 1tan

cos( ) sin( )

bM a b aa M b M

Page 13: 26 - 3/20/2000AME 150L1 M ATLAB ®. 26 - 3/20/2000AME 150L2 M ATLAB ® vs. Fortran Fortran Positives –First Compiler –Legacy Codes –Efficient Numerically

26 - 3/20/2000 AME 150L 13

MATLAB® Complex Helpers

• Real, imag, abs, and anglec1 = EDU» mag_c1=abs(c1)

1.0000+ 2.0000i mag_c1 =

EDU» c2=3*(2-sqrt(-1)*3) 2.2361

c2 = EDU» angle_c1=angle(c1)

6.0000- 9.0000i angle_c1 =

EDU» c3=6+sin(-5)*j 1.1071

c3 = EDU» imag_c1=imag(c1)

6.0000+ 0.9589i imag_c1 =

2

Page 14: 26 - 3/20/2000AME 150L1 M ATLAB ®. 26 - 3/20/2000AME 150L2 M ATLAB ® vs. Fortran Fortran Positives –First Compiler –Legacy Codes –Efficient Numerically

26 - 3/20/2000 AME 150L 14

MATLAB® Mathematical Functions

Appendix A(C: Palm Text)

Page 47,77,116(E: King Text)

Page 123-124(Etter & Kuncicky)

Appendix A(Hanselman & Littlefield)

Appendiz A(Mathworks Reference)

http://www.mathworks.com/support/books/index.php3

Page 15: 26 - 3/20/2000AME 150L1 M ATLAB ®. 26 - 3/20/2000AME 150L2 M ATLAB ® vs. Fortran Fortran Positives –First Compiler –Legacy Codes –Efficient Numerically

26 - 3/20/2000 AME 150L 15

MATLAB® Links

• Pointers to other textshttp://www.mathworks.com/support/books/index.php3

• User Contributed Software (“M-Files”)

http://www.mathworks.com/support/ftp/

• On-line Demos

http://www.mathworks.com/products/demos/

Page 16: 26 - 3/20/2000AME 150L1 M ATLAB ®. 26 - 3/20/2000AME 150L2 M ATLAB ® vs. Fortran Fortran Positives –First Compiler –Legacy Codes –Efficient Numerically

26 - 3/20/2000 AME 150L 16

Command Window

• Command Window Workspacewho - lists active variables

whos - more detailed information

clear - clears the workspace (erases all)

clear list of variables- clears variables in list

help - provides general help

help command - provides help on command

more on - pauses on full screen

Page 17: 26 - 3/20/2000AME 150L1 M ATLAB ®. 26 - 3/20/2000AME 150L2 M ATLAB ® vs. Fortran Fortran Positives –First Compiler –Legacy Codes –Efficient Numerically

26 - 3/20/2000 AME 150L 17

Number Display Formatformat short 3.1416 5 digits

format long 3.14159265358979 15 digits

format short e 3.146e+00 + exponent

format long e 3.14159265358979e+00

format short g 3.1416 better of shorts

format long g 3.14159265358979

format hex 400921fb5442d18 hexadecimal

format rat 355/113 rational approximation

format bank 3.14 2 decimal digits

format compact suppresses extra line feeds

Page 18: 26 - 3/20/2000AME 150L1 M ATLAB ®. 26 - 3/20/2000AME 150L2 M ATLAB ® vs. Fortran Fortran Positives –First Compiler –Legacy Codes –Efficient Numerically

26 - 3/20/2000 AME 150L 18

Command Window Control

clc Clear command window

home cursor to upper left corner

more on Pages the command window

computer tells what computer you are running on

version Short version information

ver long version information

quit Stop using Matlab

Page 19: 26 - 3/20/2000AME 150L1 M ATLAB ®. 26 - 3/20/2000AME 150L2 M ATLAB ® vs. Fortran Fortran Positives –First Compiler –Legacy Codes –Efficient Numerically

26 - 3/20/2000 AME 150L 19

Script M-files

• Can create scripts of commands

• Called “m-files” because extension is .m

Page 20: 26 - 3/20/2000AME 150L1 M ATLAB ®. 26 - 3/20/2000AME 150L2 M ATLAB ® vs. Fortran Fortran Positives –First Compiler –Legacy Codes –Efficient Numerically

26 - 3/20/2000 AME 150L 20

Special M-File Commands

disp(variable) display results - no variable names

echo control echoing of script commands

input prompt user for input

keyboard Give temporary control to keyboard

pause pause until any key pressed

pause n pause n seconds, then continue

waitforbuttonpress

Page 21: 26 - 3/20/2000AME 150L1 M ATLAB ®. 26 - 3/20/2000AME 150L2 M ATLAB ® vs. Fortran Fortran Positives –First Compiler –Legacy Codes –Efficient Numerically

26 - 3/20/2000 AME 150L 21

MATLAB® Session Control

• A file called startup.m (in your search path) will execute every time you start MATLAB®

• Suggestions for a startup file– Keep a diary (copy) of day’s session (you can

edit it to remove irrelevant material)– set more on

Page 22: 26 - 3/20/2000AME 150L1 M ATLAB ®. 26 - 3/20/2000AME 150L2 M ATLAB ® vs. Fortran Fortran Positives –First Compiler –Legacy Codes –Efficient Numerically

26 - 3/20/2000 AME 150L 22

Sample Startup file

% This is the startup.m initialization file

more on %pause on a full screen

dfile=strcat(date,'.txt'); %make date string

diary(dfile); %Create a diary file %named with today's

date

disp(strcat(‘Diary created:',datestr(now,0)))

Page 23: 26 - 3/20/2000AME 150L1 M ATLAB ®. 26 - 3/20/2000AME 150L2 M ATLAB ® vs. Fortran Fortran Positives –First Compiler –Legacy Codes –Efficient Numerically

26 - 3/20/2000 AME 150L 23

File & Directory Management

• Save, load & deleting variablessave stores all variables in matlab.mat

save list stores variables in list

load loads all variables from matlab.mat

load list loads just variables in list

These commands are for initializing workspace

Page 24: 26 - 3/20/2000AME 150L1 M ATLAB ®. 26 - 3/20/2000AME 150L2 M ATLAB ® vs. Fortran Fortran Positives –First Compiler –Legacy Codes –Efficient Numerically

26 - 3/20/2000 AME 150L 24

File Management Details

save(fname , ['var1','var2',…])

saves variables var1 and var2 into file stored in string fname

delete('data.mat')

deletes named file

Lots of low-level file I/O - we will not use in AME150

Page 25: 26 - 3/20/2000AME 150L1 M ATLAB ®. 26 - 3/20/2000AME 150L2 M ATLAB ® vs. Fortran Fortran Positives –First Compiler –Legacy Codes –Efficient Numerically

26 - 3/20/2000 AME 150L 25

Exist Command Details EXIST('A') returns:

0 if A does not exist

1 if A is a variable in the workspace

2 if A is a file [usually M-file] on MATLAB's search path

3 if A is a MEX-file on MATLAB's search path

4 if A is a MDL-file on MATLAB's search path

5 if A is a built-in MATLAB function

6 if A is a P-file on MATLAB's search path

7 if A is a directory

Page 26: 26 - 3/20/2000AME 150L1 M ATLAB ®. 26 - 3/20/2000AME 150L2 M ATLAB ® vs. Fortran Fortran Positives –First Compiler –Legacy Codes –Efficient Numerically

26 - 3/20/2000 AME 150L 26

Embedded OS Commandscd or pwd Show or change present working directory

p=cd Return present working directory

dir or ls Display files in current directory

d=dir return current working directory

p=matlabroot return dir path to Matlab

type cow Type cow.m in Command window

what Type organized listing of Matlab files

Page 27: 26 - 3/20/2000AME 150L1 M ATLAB ®. 26 - 3/20/2000AME 150L2 M ATLAB ® vs. Fortran Fortran Positives –First Compiler –Legacy Codes –Efficient Numerically

26 - 3/20/2000 AME 150L 27

MATLAB® Special Features

• Vectors and Arrays (and array operations)

• Plotting

• Linear Algebra

• Polynomials (and their roots)

• Solutions of Systems of Equations

• Problem Solving

Page 28: 26 - 3/20/2000AME 150L1 M ATLAB ®. 26 - 3/20/2000AME 150L2 M ATLAB ® vs. Fortran Fortran Positives –First Compiler –Legacy Codes –Efficient Numerically

26 - 3/20/2000 AME 150L 28

MATLAB® Arrays

• MATLAB® has many features to work with arrays of numbers (vectors & matrices)

• Definition:x=[ list of elements separated by , or blanks ]

each element may be an arbitrary expression

x = [0 0.1*pi,.2*pi … 0.9*pi pi]

y=sin(x) produces an array result

Page 29: 26 - 3/20/2000AME 150L1 M ATLAB ®. 26 - 3/20/2000AME 150L2 M ATLAB ® vs. Fortran Fortran Positives –First Compiler –Legacy Codes –Efficient Numerically

26 - 3/20/2000 AME 150L 29

MATLAB® Arrays (2)

• Arrays have the following properties– 1-D Arrays can be rows or columns

(use ; to signal end of row)– 2-D Arrays have rows and columns– MATLAB® can handle higher numbers of

dimensions - extra dimensions are “pages”

Page 30: 26 - 3/20/2000AME 150L1 M ATLAB ®. 26 - 3/20/2000AME 150L2 M ATLAB ® vs. Fortran Fortran Positives –First Compiler –Legacy Codes –Efficient Numerically

26 - 3/20/2000 AME 150L 30

MATLAB® Arrays (3)

• An Array is referred to by its name (same as naming variables)

• An element of an array is indicated by an index (enclosed in parentheses)V = [1 2 3 4 5]

V(3) is 3

V(:3) is [1 2 3]

V(2:) is [2 3 4 5]

Page 31: 26 - 3/20/2000AME 150L1 M ATLAB ®. 26 - 3/20/2000AME 150L2 M ATLAB ® vs. Fortran Fortran Positives –First Compiler –Legacy Codes –Efficient Numerically

26 - 3/20/2000 AME 150L 31

The Colon “:” operator• The Colon is used to create equi-spaced lists

x=[1:4] is [1 2 3 4]N1:N2 starts at N1 and increments by 1 to N2

x=[1:2:9] is [1 3 5 7 9]N1:N2:N3 starts at N1, increments by N2 until reach N3

Angle=[180:-1:0] generates a list of 181 angles from 180 to 0 (in reverse order)

Radians=pi/180*Angle (same but in radians)

Page 32: 26 - 3/20/2000AME 150L1 M ATLAB ®. 26 - 3/20/2000AME 150L2 M ATLAB ® vs. Fortran Fortran Positives –First Compiler –Legacy Codes –Efficient Numerically

26 - 3/20/2000 AME 150L 32

Scalars and Vectors

• Scalars can be added, subtracted, multiplied or divided into vectors (element by element)

» x=[1 3 5 7 9];y=3*x

y =

3 9 15 21 27

» z=x+7

z =

8 10 12 14 16

Page 33: 26 - 3/20/2000AME 150L1 M ATLAB ®. 26 - 3/20/2000AME 150L2 M ATLAB ® vs. Fortran Fortran Positives –First Compiler –Legacy Codes –Efficient Numerically

26 - 3/20/2000 AME 150L 33

The Dot “. “ Operator

• Some operators don’t make sense» x^2

??? Error using ==> ^

Matrix must be square.

» x.^2

ans =

1 9 25 49 81

Page 34: 26 - 3/20/2000AME 150L1 M ATLAB ®. 26 - 3/20/2000AME 150L2 M ATLAB ® vs. Fortran Fortran Positives –First Compiler –Legacy Codes –Efficient Numerically

26 - 3/20/2000 AME 150L 34

The Dot “. “ Operator (continued)

• The Dot before an operator signals that you want top perform the same operation on every element in an array

x = 1 3 5 7 9

y = 3 9 15 21 27

» x.*y

ans =

3 27 75 147 243