Vous êtes sur la page 1sur 31

Lecture 1: Introduction and Script M File

Assoc. Prof. Pakorn Kaewtrakulpong, Ph.D.


INC, KMUTT

MATLAB
 Introduction
 Strengths
 Weaknesses
 Variety
 Working Environment
 Basic Features
 Managing the MATLAB workspace
 Script M Files

Introduction
 MATrix LABoratory
 numerical computing environment



a scientific calculator
a software package for computation in engineering, science,
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

Strengths
 easy to learn
 great for testing ideas
 state of the art algorithms
 large community
 easy visualization
 symbolic maths

Weaknesses
 not very good at hardware interfacing
 interpreted language
 not so good for programming with GUI (Graphic User

Interface)

Variety
 OS
 MS Windows
 Unix or Linux
 MacOS
 platform
 x86 (or 32 bit)



 Toolboxes/Blocksets
 Toolboxes  MATLAB
 Blocksets  Simulink

hardware interfacing
faster

 x64
 large memory model
6

Working Environment
current directory

workspace
command window

command history

Help
 help <topic>
 e.g. help,
help plot
 doc <topic>
 e.g. doc, doc
plot
 Help menu or

toolbutton

Variable Editor

Profiler

10

File and Directory Comparisons

11

Basic Features
 Scientific calculator
 In command window, >> is the prompt.
 operators and functions
 variables
 symbols
 Script

12

Simple Math
 Precedence
1. parenthesis
2. operations
I.
II.
III.

3.

^ (power)
*, / or \ (invert division)
+, -

left right

 e.g.
4*25+6*22+2*99
3^2-5-6/3+2
3^2-5-6/(3+2)
13

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
 legitimate characters




letters
underscore
numbers

 begin with letters


 up to 63 characters

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

Special Variables
 ans
 eps
 pi
 Inf, inf
 NaN, nan
 i,j
 (i2 = -1)

15

Basic Data Types


 double (defaults), single
 uint8, uint16, uint32, uint64, int8, int16, int32, int64
 char
 logical
 structure, cell

16

Ranges

17

Elementary Functions

18

Elementary Functions (contd.)

19

Examples: Elementary Functions


 e.g.
x=sqrt(2)/2
y=asin(x)
y_deg=y*180/pi
y=rem(23,4)
x=-2.6,y1=fix(x),y2=floor(x),y3=ceil(x),y4=round(x)

20

Complex Numbers
 a+bi (cartesian form) where a and b are real numbers,

and i is the imaginary unit.


 e.g.
c1=1-2i,c2=1-2j
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)
21

Other MATLAB symbols


 . . . contiuation
,
;
%
 <Ctl+C>

comma statement separator


semicolon statement separator
line comment
aborting execution

22

Command Window
 Recalling previous commands
 arrow keys
 a combination of the leading letter and an up arrow key.
 Rudimentary cursor control
 clc
clear the command window

23

Display Formats

24

Display Format Settings

25

The MATLAB Search Path

26

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> le extension
need 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..

27

Example: Managing the MATLAB


Workspace
 e.g.
clear
a=1;b=2;c=3
save
save myvars a b
clear
load myvars
clear
load
28

Script M Files
 Commands Usually Used in Script Files or M-Files
tells MATLAB to display or echo commands to the
command window as they are read and evaluated
echo off
not to display
echo
toggles the echo state
displays results without identifying variable names
disp(<var>)
(display the values only)
input
prompts user for input
echo on

return

returns control to the executing script m-file or base

29

Script M Files (contd.)

30

Homework #1
1.

write a line of commands to find the result of

2. write a script that asks for inputs x and n, and calculate the
result. Find the result for each of input combinations.

3. find each range of


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

31

Vous aimerez peut-être aussi