INC151 Matlab Lec1

30
 Lecture 1: Introduction and Script M File  Assoc. Prof . Pak orn Kaewtr akulpong, Ph.D . INC, KMUTT

Transcript of INC151 Matlab Lec1

Page 1: INC151 Matlab Lec1

8/2/2019 INC151 Matlab Lec1

http://slidepdf.com/reader/full/inc151-matlab-lec1 1/30

 Lecture 1: Introduction and Script M File Assoc. Prof. Pakorn Kaewtrakulpong, Ph.D.

INC, KMUTT

Page 2: INC151 Matlab Lec1

8/2/2019 INC151 Matlab Lec1

http://slidepdf.com/reader/full/inc151-matlab-lec1 2/30

MATLAB

Introduction Strengths

 Weaknesses

 Variety  Working Environment

Basic Features

Managing the MATLAB workspace Script M Files

2

Page 3: INC151 Matlab Lec1

8/2/2019 INC151 Matlab Lec1

http://slidepdf.com/reader/full/inc151-matlab-lec1 3/30

Introduction

MATrix LABoratory  numerical computing environment

a scientific calculator

, ,and applied mathematics.

a powerful programming language, excellent graphics, and a wide range of expert knowledge.

Simulink graphical simulation environment

by Mathworks, Inc. (http://www.mathworks.com/)

3

Page 4: INC151 Matlab Lec1

8/2/2019 INC151 Matlab Lec1

http://slidepdf.com/reader/full/inc151-matlab-lec1 4/30

Strengths

easy to learn great for testing ideas

state of the art algorithms

  large community  easy visualization

symbolic maths

4

Page 5: INC151 Matlab Lec1

8/2/2019 INC151 Matlab Lec1

http://slidepdf.com/reader/full/inc151-matlab-lec1 5/30

Weaknesses

not very good at hardware interfacing interpreted language

not so good for programming with GUI (Graphic User

nter ace

5

Page 6: INC151 Matlab Lec1

8/2/2019 INC151 Matlab Lec1

http://slidepdf.com/reader/full/inc151-matlab-lec1 6/30

Variety

OS MS Windows

Unix or Linux

Toolboxes/Blocksets ToolboxesMATLAB

Blocksets Simulink

ac platform

x86 (or 32 bit) hardware interfacing

faster

x64 large memory model

6

Page 7: INC151 Matlab Lec1

8/2/2019 INC151 Matlab Lec1

http://slidepdf.com/reader/full/inc151-matlab-lec1 7/30

Working Environment

  workspace

command history 

command window 

 

7

Page 8: INC151 Matlab Lec1

8/2/2019 INC151 Matlab Lec1

http://slidepdf.com/reader/full/inc151-matlab-lec1 8/30

Help help <topic>

e.g. help,help plot

doc <to ic>

e.g. doc, docplot

Help menu or

toolbutton

8

Page 9: INC151 Matlab Lec1

8/2/2019 INC151 Matlab Lec1

http://slidepdf.com/reader/full/inc151-matlab-lec1 9/30

Variable Editor

9

Page 10: INC151 Matlab Lec1

8/2/2019 INC151 Matlab Lec1

http://slidepdf.com/reader/full/inc151-matlab-lec1 10/30

Profiler

10

Page 11: INC151 Matlab Lec1

8/2/2019 INC151 Matlab Lec1

http://slidepdf.com/reader/full/inc151-matlab-lec1 11/30

File and Directory Comparisons

11

Page 12: INC151 Matlab Lec1

8/2/2019 INC151 Matlab Lec1

http://slidepdf.com/reader/full/inc151-matlab-lec1 12/30

Basic Features

Scientific calculator In command window, >> is the prompt.

operators and functions

 var a es symbols

Script

12

Page 13: INC151 Matlab Lec1

8/2/2019 INC151 Matlab Lec1

http://slidepdf.com/reader/full/inc151-matlab-lec1 13/30

Simple Math Precedence

1. parenthesis

2. operationsI. ^ ower

II. *, / or \ (invert division)III. +, -

3. left→ right

e.g.» 4*25+6*22+2*99

» 3^2-5-6/3+2

» 3^2-5-6/(3+2)

13

Page 14: INC151 Matlab Lec1

8/2/2019 INC151 Matlab Lec1

http://slidepdf.com/reader/full/inc151-matlab-lec1 14/30

Variables In MATLAB, variables are not necessarily declared but are

created on a need-to-use basis by its memory manager. Naming convention

case sensitive le itimate characters

letters underscore numbers

begin with letters up to 63 characters (namelengthmax)

e.g.» a=1;b=2;c=3;d=a+b+c» 1_AB=1 % error»  A=1,a=2

14

Page 15: INC151 Matlab Lec1

8/2/2019 INC151 Matlab Lec1

http://slidepdf.com/reader/full/inc151-matlab-lec1 15/30

Special Variables

ans eps

pi

 

Inf, inf  NaN, nan

i,j

(i2 = -1)

15

Page 16: INC151 Matlab Lec1

8/2/2019 INC151 Matlab Lec1

http://slidepdf.com/reader/full/inc151-matlab-lec1 16/30

Basic Data Types

double (defaults), single uint8, uint16, uint32, uint64, int8, int16, int32, int64

char

logical

structure, cell

16

Page 17: INC151 Matlab Lec1

8/2/2019 INC151 Matlab Lec1

http://slidepdf.com/reader/full/inc151-matlab-lec1 17/30

Elementary Functions

17

Page 18: INC151 Matlab Lec1

8/2/2019 INC151 Matlab Lec1

http://slidepdf.com/reader/full/inc151-matlab-lec1 18/30

Elementary Functions (contd.)

18

Page 19: INC151 Matlab Lec1

8/2/2019 INC151 Matlab Lec1

http://slidepdf.com/reader/full/inc151-matlab-lec1 19/30

Examples: Elementary Functions

e.g.» x=sqrt(2)/2

»  y=asin(x)

»  y_ eg=y 1 0 p

»  y=rem(23,4)

» x=-2.6,y1=fix(x),y2=floor(x),y3=ceil(x),y4=round(x)

19

Page 20: INC151 Matlab Lec1

8/2/2019 INC151 Matlab Lec1

http://slidepdf.com/reader/full/inc151-matlab-lec1 20/30

Complex Numbers

a+bi (cartesian form) where a and b are real numbers,and i is the imaginary unit.

e.g.

» c1=1-2 ,c2=1-2

» c3=6+sin(.5)i % error

» c3=6+sin(.5)*i

» c4=(c1+c2)/c3

» mag_c1=abs(c1),angle_c1=angle(c1),deg_c1=angle_c1*180/pi

» real_c1=real(c1),imag_c1=imag(c1)

20

Page 21: INC151 Matlab Lec1

8/2/2019 INC151 Matlab Lec1

http://slidepdf.com/reader/full/inc151-matlab-lec1 21/30

Other MATLAB symbols

. . . contiuation , comma statement separator

; semicolon statement separator

% line comment <Ctl+C> aborting execution

21

Page 22: INC151 Matlab Lec1

8/2/2019 INC151 Matlab Lec1

http://slidepdf.com/reader/full/inc151-matlab-lec1 22/30

Command Window Recalling previous commands

arrow keys

a combination of the leading letter and an up arrow key.

 

u mentary cursor contro clc clear the command window 

22

Page 23: INC151 Matlab Lec1

8/2/2019 INC151 Matlab Lec1

http://slidepdf.com/reader/full/inc151-matlab-lec1 23/30

Display Formats

23

Page 24: INC151 Matlab Lec1

8/2/2019 INC151 Matlab Lec1

http://slidepdf.com/reader/full/inc151-matlab-lec1 24/30

Display Format Settings

24

Page 25: INC151 Matlab Lec1

8/2/2019 INC151 Matlab Lec1

http://slidepdf.com/reader/full/inc151-matlab-lec1 25/30

The MATLAB Search Path

25

Page 26: INC151 Matlab Lec1

8/2/2019 INC151 Matlab Lec1

http://slidepdf.com/reader/full/inc151-matlab-lec1 26/30

Managing the MATLAB Workspace clear

clear all variables in the workspace Load and save data

.MAT file save

saves all variables in MATLAB.MAT (MATLAB binary format).

save <MAT-file> must not be the same name as any variable in the workspace; otherwise

that variable will be saved in MATLAB.MAT. <MAT-file> file extensionneed not be put explicitly.

save <MAT-file> <variable 1> <variable 2> ... <variable n> saves only selected variables.

 Alternatively, use the menu File/Import data.. or Save workspace as..

26

Page 27: INC151 Matlab Lec1

8/2/2019 INC151 Matlab Lec1

http://slidepdf.com/reader/full/inc151-matlab-lec1 27/30

Example: Managing the MATLABWorkspace

e.g.

» clear

» a=1;b=2;c=3

» save

» save myvars a b

» clear

» load myvars

» clear

» load

27

Page 28: INC151 Matlab Lec1

8/2/2019 INC151 Matlab Lec1

http://slidepdf.com/reader/full/inc151-matlab-lec1 28/30

Script M Files Commands Usually Used in Script Files or M-Files

echo ontells MATLAB to display or echo commands to thecommand window as they are read and evaluated

 

echo toggles the echo state

disp(<var>)displays results without identifying variable names(display the values only)

input prompts user for input

return returns control to the executing script m-file or base

28

Page 29: INC151 Matlab Lec1

8/2/2019 INC151 Matlab Lec1

http://slidepdf.com/reader/full/inc151-matlab-lec1 29/30

Script M Files (contd.)

29

Page 30: INC151 Matlab Lec1

8/2/2019 INC151 Matlab Lec1

http://slidepdf.com/reader/full/inc151-matlab-lec1 30/30

Homework #1

2. write a script that asks for inputs  x and n, and calculate the

1. write a line of commands to find the result of 

. .

30

3. find each range of 

double (defaults), singleuint8, uint16, uint32, uint64, int8, int16, int32, int64

Hints: realmin, realmax, intmin, intmax