Sie sind auf Seite 1von 21

TECHNICAL COMPUTING

Qasim Habib
Today’s Content

1. Script Files and Editor/Debugger

2. Programming in MATLAB

3. Relational operators
Script Files and
Editor/Debugger
MATLAB operates in two ways
1.Interactive mode
2.Script files
Script Files
• Script file contains MATLAB commands, so
running it is equal to typing all the commands –
one at a time – at the command window prompt.
• You can run the file by typing its name at the
command window prompt.
• Script file variables are global variables.
Creating & using a Script
File
• Following lines show a simple script file
% Program Example.m
% This program computes the sine of
% the square root and displays the
result.
x = sqrt ( [ 5:2:13 ] )
y = sin (x)
Effective use of Script Files
• Used to avoid the need to retype
commonly used procedures.
• Names of script files should follow
the MATLAB naming conventions.
• Script file variables are global as
compared to function file variables
which are local.
Debugging Script Files
• Debugging a program is the process
of finding and removing the “bugs”,
or errors, in a program.
• Two type of errors are encountered
1.Syntax errors
2.Runtime errors
Programming Style
Suggested structure for a script file is
1.Comment Section
2.Input Section
3.Calculation Section
4.Output Section
Controlling input and output
Example of menu command
• Look the use of menu command in
the following example
Example of fprintf command
• Look the use of fprintf command in
the following example
Task 5
• Solve T1.4-2
• Study this Topic (optional)
1.5 The MATLAB Help System
Programming in MATLAB
• Programming is performed in
MATLAB using script files (m-files).
• MATLAB often uses following
capabilities in programming
1.Relational operators
2.Conditional statements
3.Loops
Relational operators
• Relational Operators are used to make
comparisons.
• MATLAB has six relational operators to
make comparisons between arrays.
• Note in the next slide that “equal to”
operator consist of two equal signs ==,
because = sign denotes ‘assignment
operator’ in MATLAB.
Some rules
• Comparison is performed on
element-by-element basis
• Arrays must be of the same
dimension
• The result of a comparison using
relational operator is a logical value,
which is either 0 (for false) or 1 (for
true).
Examples
>> x = [6,3,9] ; y = [14,2,9];
>> z = (x < y)
z =
1 0 0
>> z = (x ~= y)
z =
1 1 0
>> z = x (x < y)
z =
6
The find Function
• The function find (x) computes an array
containing the indices of the nonzero
elements of the numeric array x.
• Example
>> x = [-2 0 4]
>> y = find(x)
y =
1 3
Task 6
• Example E1.6-1
• T1.6-1 & T1.6-2
Reference
This lecture is from pg 29-46 of your main MATLAB
book

INTRODUCTION TO MATLAB 7
FOR ENGINEERS
by
WILLIAM J. PALM III

Das könnte Ihnen auch gefallen