An Overview of Matlab: Computational Techniques

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Computational Techniques

The Default MATLAB Desktop

MATLAB for Electrical Engineers

An Overview of
MATLAB®

1-1 Dr S Z Sayed Hassen


Dr S Z Sayed Hassen

Scalar arithmetic operations


Entering Commands and Expressions

• MATLAB retains your previous keystrokes.


• Use the up-arrow key to scroll back through the
commands.
• Press the key once to see the previous entry, and
so on.
• Use the down-arrow key to scroll forward. Edit a
line using the left- and right-arrow keys the
Backspace key, and the Delete key.
• Press the Enter key to execute the command.

Dr S Z Sayed Hassen Dr S Z Sayed Hassen


1-2 1-3

Order of precedence
An Example Session,

>> 8/10
ans =
0.8000
>> 5*ans
ans =
4
>> r=8/10
r=
0.8000
>> r
r=
0.8000
>> s=20*r
s=
16 Dr S Z Sayed Hassen Dr S Z Sayed Hassen
1-4 1-5

Dr S Z Sayed Hassen 1
Computational Techniques

Examples of Precedence,
Examples of Precedence,
>> 8 + 3*5
>> 3*4^2 + 5
ans =
ans =
23
53
>> 8 + (3*5)
>>(3*4)^2 + 5
ans =
ans =
23
149
>>(8 + 3)*5
>>27^(1/3) + 32^(0.2)
ans =
ans =
55
5
>>4^2128/4*2
>>27^(1/3) + 32^0.2
ans =
ans =
0
5
>>4^212 8/(4*2)
>>27^1/3 + 32^0.2
ans =
ans =
3
11

Dr S Z Sayed Hassen Dr S Z Sayed Hassen


1-6 1-7

Commands for managing the work session Special variables and constants

Dr S Z Sayed Hassen Dr S Z Sayed Hassen


1-8 1-9

Complex Number Operations Numeric display formats.

• The number c1 = 1 – 2i is entered as follows:


c1 = 12i.
• An asterisk is not needed between i or j and
a number, although it is required with a
variable, such as c2 = 5 i*c1.
• Be careful. The expressions
y = 7/2*i
The Desktop Menus and Toolbar.
and
x = 7/2i
give two different results:
y = (7/2)i = 3.5i
and
x = 7/(2i) = –3.5i.

Dr S Z Sayed Hassen Dr S Z Sayed Hassen


1-10 1-11

Dr S Z Sayed Hassen 2
Computational Techniques

Arrays Array Index

• The numbers 0, 0.1, 0.2, …, 10 can be assigned to the >>u(7)


variable u by typing u = 0:0.1:10. ans =
• To compute w = 5 sin u for u = 0, 0.1, 0.2, …, 10, the 0.6000
session is; >>w(7)
ans =
>>u = 0:0.1:10; 2.8232
>>w = 5*sin(u);
• Use the length function to determine how
• The single line, w = 5*sin(u), computed the formula many values are in an array.
w = 5 sin u 101 times.
>>m = length(w)
m=
101

Dr S Z Sayed Hassen Dr S Z Sayed Hassen


1-12 1-13

Some commonly used mathematical functions


Polynomial Roots

To find the roots of x3 – 7x2 + 40x – 34 = 0, the session


is

>>a = [1,-7,40,-34];
>>roots(a)
ans =
3.0000 + 5.000i
3.0000 - 5.000i
1.0000

The roots are x = 1 and x = 3 ± 5i.

Dr S Z Sayed Hassen Dr S Z Sayed Hassen


1-14 1-15

System, directory, and file commands


When you type problem1,
1. MATLAB first checks to see if problem1 is
a variable and if so, displays its value.
2. If not, MATLAB then checks to see if
problem1 is one of its own commands, and
executes it if it is.
3. If not, MATLAB then looks in the current
directory for a file named problem1.m and
executes problem1 if it finds it.
4. If not, MATLAB then searches the
directories in its search path, in order,
for problem1.m and then executes it if
found.

Dr S Z Sayed Hassen Dr S Z Sayed Hassen


1-16 1-17

Dr S Z Sayed Hassen 3
Computational Techniques

A graphics window showing a plot. Some MATLAB plotting commands

Dr S Z Sayed Hassen Dr S Z Sayed Hassen


1-18 1-19

Linear Algebraic Equations You can perform operations in MATLAB in two


ways:
6x + 12y + 4z = 70
7x – 2y + 3z = 5 1. In the interactive mode, in which all
2x + 8y – 9z = 64 commands are entered directly in the
Command window, or
>>A = [6,12,4;7,-2,3;2,8,-9];
2. By running a MATLAB program stored in
>>B = [70;5;64];
script file. This type of file contains
>>Solution = A\B
MATLAB commands, so running it is
Solution =
equivalent to typing all the commands—
3
one at a time—at the Command window
5
prompt. You can run the file by typing its
-2
name at the Command window prompt.
The solution is x = 3, y = 5, and z = –2.

Dr S Z Sayed Hassen Dr S Z Sayed Hassen


1-20 1-21

The MATLAB Command window with the Editor/Debugger


COMMENTS open.
The comment symbol may be put anywhere in the
line. MATLAB ignores everything to the right of the
% symbol. For example,

>>% This is a comment.


>>x = 2+3 % So is this.
x=
5

Note that the portion of the line before the % sign is


executed to compute x.

Dr S Z Sayed Hassen Dr S Z Sayed Hassen


1-22 1-23

Dr S Z Sayed Hassen 4
Computational Techniques

Keep in mind when using script files: Debugging Script Files

1. The name of a script file must begin with Program errors usually fall into one of the
a letter, and may include digits and the following categories.
underscore character, up to 63 1. Syntax errors such as omitting a parenthesis
characters. or comma, or spelling a command name
2. Do not give a script file the same name incorrectly. MATLAB usually detects the
as a variable. more obvious errors and displays a message
3. Do not give a script file the same name describing the error and its location.
as a MATLAB command or function. You 2. Errors due to an incorrect mathematical
can check to see if a command, function procedure, called runtime errors. Their
or file name already exists by using the occurrence often depends on the particular
exist command. input data. A common example is division by
Dr S Z Sayed Hassen
zero. Dr S Z Sayed Hassen
1-24 1-25

To locate program errors, try the following: Programming Style

1. Test your program with a simple version of 1. Comments section


the problem which can be checked by hand. a. The name of the program and any key
2. Display any intermediate calculations by words in the first line.
removing semicolons at the end of b. The date created, and the creators' names
statements. in the second line.
3. Use the debugging features of the c. The definitions of the variable names for
Editor/Debugger. every input and output variable. Include
definitions of variables used in the calculations
and units of measurement for all input and all
output variables!
d. The name of every user-defined function
Dr S Z Sayed Hassen
called by the program.Dr S Z Sayed Hassen
1-26 1-27

Some Input/output commands


Programming Style (continued)

2. Input section Include input data


and/or the input functions and
comments for documentation.

3. Calculation section

4. Output section This section might


contain functions for displaying the
output on the screen.

1-28
Dr S Z Sayed Hassen Dr S Z Sayed Hassen
1-29

Dr S Z Sayed Hassen 5
Computational Techniques

Example of a Script File


Example of a Script File (continued)
Problem:
% Program falling_speed.m:
The speed v of a falling object dropped with no initial % Plots speed of a falling object.
velocity is given as a function of time t by v = gt. % Created on March 1, 2009
%
Plot v as a function of t for 0 < t < tfinal, where tfinal is the % Input Variable:
final time entered by the user. % tfinal = final time (in seconds)
%
% Output Variables:
% t = array of times at which speed is % computed
(in seconds)
% v = array of speeds (meters/second)
%

1-30 Dr S Z Sayed Hassen Dr S Z Sayed Hassen


1-31

Example of a Script File (continued)


Example of a Script File (continued)

% Parameter Value: % Calculation section:


g = 9.81; % Acceleration in SI units dt = tfinal/500;
% % Create an array of 501 time values.
% Input section: t = 0:dt:tfinal;
tfinal = input(’Enter final time in seconds:’); % Compute speed values.
% v = g*t;
%
% Output section:
Plot(t,v),xlabel(’t (s)’),ylabel(’v m/s)’)

1-32 Dr S Z Sayed Hassen Dr S Z Sayed Hassen


1-33

Getting Help From MATLAB: The MATLAB Help Browser


The Function Browser after plot has been selected

1-34 Dr S Z Sayed Hassen Dr S Z Sayed Hassen


1-35

Dr S Z Sayed Hassen 6
Computational Techniques

The Help Navigator MATLAB Help Functions,

• help funcname: Displays in the Command


window a description of the specified function
funcname.
• lookfor topic: Looks for the string topic in the first
comment line (the H1 line) of the HELP text of all
M-files found on MATLABPATH (including
private directories), and displays the H1 line for
all files in which a match occurs.
• doc funcname: Opens the Help Browser to the
reference page for the specified function
funcname, providing a description, additional
remarks, and examples.

1-36 1-37
Dr S Z Sayed Hassen Dr S Z Sayed Hassen

Steps in engineering problem solving Steps for developing a computer solution

Dr S Z Sayed Hassen Dr S Z Sayed Hassen


1-38 1-39

Dr S Z Sayed Hassen 7

You might also like