matlab_课件
Disadvantages of MATLAB
Can be slow Expensive
6
MATLAB Environment
MATLAB array MATLAB Desktop
Command Window Figure Windows Edit/Debug Window Command History Window Launch Pad Workspace Browser and Array Editor Help Browser Current Directory Browser
21
The MATLAB Search Path
Programming Pitfalls
Never use a variable with the same name as a MATLAB function or command Never create an M-file with the same name as a MATLAB function or command “File/Set Path”, editpath, pathtool path command
23
Homework
Quiz 1.1 Exercises
1.1, 1.4
24
MATLAB Basics
MATLAB Basics
A program can be input
command by command using the command line (lines starting with ―» ‖ on the MATLAB desktop) as a series of commands using a file (a special file called M-file)
MATLAB is case sensitive: “name”, “Name” and “NAME” are considered different variables Never use a variable with the same name as a MATLAB command Naming convention: use lowercase letters
Command_line oriented way to get help
Type help or help fun1 in the command window
Searches for an exact function name match Searches the quick summary information in each function for a match
zeros()
x = zeros(2) x = 0 0 0 0 z = zeros(2,3) z = 0 0 0 0
y = zeros(1,4) y = 0 0 0
0
0 0
ones(), size(), length()
t = zeros( size(z) ) t = 0 0 0 0 0 0
Use meaningful names for variables MATLAB variable names
must begin with a letter can contain any combination of letters, numbers and underscore (_) must be unique in the first 31 characters
If a command is followed by a semicolon (;), result of the computation is not shown on the command window
26
MATLAB Basics: Variables
Variable is a name given to a reserved location in memory
4
Introduction to MATLAB
MATrix LABoratory
Advantages of MATLAB
Ease of use Platform independence Predefined functions Plotting
matrix = [ 1 2 3; 4 5 6 ] matrix = 1 2 3 4 5 6 matrix = [ 1 2 3; 4 5 ] ??? Error a = [ 5 (2+4) ] a = 5 6
31
MATLAB Basics: Initializing Variables
10
The Command History Window
11
The Launch Pad
12
The Edit/Debug Window
Create new M-files
“File/New/M-file” Clicking the Toolbar icon “File/Open ” Clicking the
MATLAB Programming for Engineers
Texts
S. J. Chapman, MATLAB Programming for Engineers(2nd edition)
Reference
张志涌等,MATLAB教程-基于6.X版本,北
航出版社 许波等,MATLAB工程数学应用,清华大学出 版社
2
Grading
Lab work: 20% HW: 10% Midterm: 10% Final: 60%
Tests
An in-class open-book final
3
Contents
1 Introduction (1.1-1.5) 2 MATLAB Basics (2.1-2.10,2.13,2.14) 3 Top-down Program Design (3.1-3.2) 4 Relational and Logical Operators(3.3,4.3) 5 Branches and Loops (3.4, 4.1,4.2,4.4) 6 Plotting(2.11, 3.5, 6.4, 6.5) 7 User-defined Functions (5.1-5.9) 8 Complex Data and Character Data(6.1-6.3) 9 Input/Output Functions(8.1-8.11) 10 Sparse Arrays, Cell Arrays, and Structures (7.1-7.5)
transpose operator '
u = [ 1:3 ]' u = 1 2 3 v = [ u u ] v = 1 1 2 2 3 3
v = [ u'; u' ] v = 1 2 1 2
3 3
33
MATLAB Basics: Initializing Variables
Initialization using built-in functions
Initialization using shortcut statements
colon operator first:increment:last
x = 1:2:10 x = 1 3 y = 0:0.1:0.5 y = 0 0.1
5
7
9
0.2
0.3
0.4
0.5
32
MATLAB Basics: Initializing Variables
Open an existing one
Toolbar icon
13
The Edit/Debug Window
14
Figure Windows
15
The MATLAB Workspace
>> whos >>var1 >>clear
Deletes all variables
>>clearpe lookfor command
19
A Few Important Commands
Type demo or select “demos” in the Launch Pad clc clf Clear ^c(control-c), abort ! Invoke operating system command diary (diary fileneme,diary off ,diary on)
34
MATLAB Basics: Initializing Variables
Initialization using keyboard input
input()
value = input( 'Enter an input value: ' ) Enter an input value: 1.25 value = 1.2500 name = input( 'What is your name: ', 's' ) What is your name: Selim name = Selim
class_code = 111; number_of_students = 65; name = 'Sichuan Normal University'; radius = 5; area = pi * radius^2;