technical computing 3
TRANSCRIPT
-
8/14/2019 Technical Computing 3
1/21
TECHNICAL COMPUTING
Qasim Habib
-
8/14/2019 Technical Computing 3
2/21
Todays Content
1. Script Files and Editor/Debugger
2. Programming in MATLAB
3. Relational operators
-
8/14/2019 Technical Computing 3
3/21
Script Files andEditor/Debugger
MATLAB operates in two ways
1.Interactive mode
2.Script files
-
8/14/2019 Technical Computing 3
4/21
Script Files
Script file contains MATLAB commands, sorunning 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 thecommand window prompt.
Script file variables are global variables.
-
8/14/2019 Technical Computing 3
5/21
Creating & using a ScriptFile
Following lines show a simple script file
% Program Example.m
% This program computes the sine of
% the square root and displays theresult.
x = sqrt ( [ 5:2:13 ] )
y = sin (x)
-
8/14/2019 Technical Computing 3
6/21
-
8/14/2019 Technical Computing 3
7/21
Effective use of Script Files
Used to avoid the need to retypecommonly used procedures.
Names of script files should followthe MATLAB naming conventions.
Script file variables are global ascompared to function file variableswhich are local.
-
8/14/2019 Technical Computing 3
8/21
Debugging Script Files
Debugging a program is the processof finding and removing the bugs,or errors, in a program.
Two type of errors are encountered
1.Syntax errors
2.Runtime errors
-
8/14/2019 Technical Computing 3
9/21
Programming Style
Suggested structure for a script file is
1.Comment Section
2.Input Section3.Calculation Section
4.Output Section
-
8/14/2019 Technical Computing 3
10/21
Controlling input and output
-
8/14/2019 Technical Computing 3
11/21
Example of menu command
Look the use of menu command inthe following example
-
8/14/2019 Technical Computing 3
12/21
Example of fprintf command
Look the use of fprintf command inthe following example
-
8/14/2019 Technical Computing 3
13/21
Task 5
Solve T1.4-2
Study this Topic (optional)
1.5 The MATLAB Help System
-
8/14/2019 Technical Computing 3
14/21
Programming in MATLAB
Programming is performed inMATLAB using script files (m-files).
MATLAB often uses followingcapabilities in programming
1.Relational operators
2.Conditional statements3.Loops
-
8/14/2019 Technical Computing 3
15/21
Relational operators
Relational Operators are used to makecomparisons.
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 assignmentoperator in MATLAB.
-
8/14/2019 Technical Computing 3
16/21
-
8/14/2019 Technical Computing 3
17/21
Some rules
Comparison is performed onelement-by-element basis
Arrays must be of the samedimension
The result of a comparison usingrelational operator is a logical value,which is either 0 (for false) or 1 (fortrue).
-
8/14/2019 Technical Computing 3
18/21
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
-
8/14/2019 Technical Computing 3
19/21
The find Function
The function find (x) computes an arraycontaining the indices of the nonzeroelements of the numeric array x.
Example>> x = [-2 0 4]
>> y = find(x)
y =1 3
-
8/14/2019 Technical Computing 3
20/21
Task 6
Example E1.6-1
T1.6-1 & T1.6-2
-
8/14/2019 Technical Computing 3
21/21
Reference
This lecture is from pg 29-46 of your main MATLABbook
INTRODUCTION TO MATLAB 7
FOR ENGINEERS
by
WILLIAM J. PALM III