Module 01
Module 01
SO
SOLVING
G WITH MATLAB
A A
AND MAPLE
MODULE 1
STARTING MATLAB
2017
2. Introduction
Matlab (Matrix laboratory) is an interactive software system for
numerical computations and graphics.
IIn addition,
ddi i i has
it h a variety
i off graphical
hi l capabilities,
bili i and
d can be
b
extended through programs written in its own programming
language.
language
2
MATLAB
Matlab is designed to solve problems numerically, that is, in finite-
precision arithmetic.
arithmetic Therefore it produces approximate rather than
exact solutions, and should not be confused with a symbolic
computation
p system
y ((SCS)) such as Mathematica or Maple.
p
3. MATLAB Windows
Default Windows Other Windows
Command Window Help Window
C
Current Di
Directory Window
Wi d Edi Window
Editor Wi d
Workspace Window Figure Window
Command History Window
3
MATLAB
4
MATLAB
4. Working in the Command Window
Clickk in
Cli i the
th Command
C d Window
Wi d t make
to k it active.
ti Wh a window
When i d
becomes active, its title bar darkens. It is also likely that your cursor
will change from outline form to solid or from light to dark,
dark or it may
simply appear. Now you can begin entering commands.
• Several Commands can be types in the same line.
line This is done by
typing a comma between the commands. When the Enter key is
pressed the commands are executed in order from left to right.
• It is not possible to go back to a previous line in the command
window, make a correction and re
re-execute
execute the command.
• A previously typed command can be recalled to the command
prompt with the up-arrow key (). () When the command is
displayed at the command prompt, it can be modified if needed
and executed. The down-arrow keyy (()) can be used to move down
the previously typed commands.
5
MATLAB
• To enter a statement that is too long to be typed in one line, use
three periods, … , followed by Enter.
• You can suppress output to the screen by adding a semicolon (;)
after the statement.
statement
• When the symbol % is typed in the beginning of a line, the line is
designated as a comment.
comment
clc Command
The clc command clears the Command Window. You can also place
cursor on the Command window and press the right button of the
mouse.
Aborting
To interrupt a running program press simultaneously the Ctrl-c keys.
6
MATLAB
5. Saving and Reloading Work
To save yyour current workspace
p select Save Workspace
p as… from
the File menu. Chose a name for your file, e.g. filename.mat and
next click on Save. Remember that the file you just created must be
l
located
d in
i MATLAB's
MATLAB' searchh path.h
To load contents of the file named filename into MATLAB's
workspace
k type load
l d filename
fil i the
in h Command
C d Window.
Wi d Y can
You
also select open from the File menu.
To save the
h workk performed
f d during
d i session,
i you can use command
d
diary ‘filename.txt’.
8
MATLAB
7. Display Formats
FORMAT Set S t output
t t format.
f t FORMAT with ith no inputs
i t sets t the
th
output format to the default appropriate for the class of the variable.
For float variables,
variables the default is FORMAT SHORT.
SHORT
9
MATLAB
FORMAT LONG G Best of fixed or floating point format
with
ith 15 digits
di it for
f double
d bl and
d 7 digits
di it for
f
single.
FORMAT SHORT E
Engineering
i i fformatt that
th t has
h att least
l t 5
ENG digits and a power that is a multiple of
three
FORMAT LONG Engineering format that has exactly 16
ENG significant digits and a power that is a
multiple of three.
FORMAT HEX Hexadecimal format.
format
FORMAT + The symbols +, - and blank are printed
for positive,
positive negative and zero elements.
elements
Imaginary parts are ignored.
FORMAT BANK Fixed format for dollars and cents.
cents
10
MATLAB
FORMAT RAT Approximation by ratio of small integers.
N b with
Numbers ith a large
l numerator
t or large
l
denominator are replaced by *.
FORMAT S
Suppresses extra
t line-feeds.
li f d
COMPACT
FORMAT LOOSE P t the
Puts th extra
t line-feeds
li f d back
b k in.
i
11
MATLAB
asin, acos, atan: inverse trigonometric functions
asind acosd,
asind, acosd atand: inverse trigonometric functions for degree
asinh, acosh, atanh: inverse hyperbolic functions
exp, log, log10: exponential functions
real,, imag,
g, conj:
j real,, imaginary
g y pparts of a complex
p number,, and the
complex conjugate number
abs, angle: absolute value of a complex number,
number and the phase angle
of a complex number
complex(x,y):
complex(x y): Generates a complex number with a real component
x and an imaginary component y.
round: rounding to the nearest integer
floor: rounding to the smallest integer
ceil: rounding to the largest integer
12
MATLAB
fix: rounding to the smallest integer if positive and to the largest
integer if negative
sign: 1 if positive, 0 if zero, and –1, if negative
mod rem: remainders upon division
mod,
factorial: factorial of an integer number
factor: factorize an integer number into prime numbers
gcd(x y) : Finds the greatest common denominator of x and y.
gcd(x,y) y
lcm(x,y) : Finds the least common multiple of x and y.
13
MATLAB
9. Variables and Assignments
A variable is a name made of a letter or a combination of several
letters (and digits) that is assigned a numerical value. Once a
variable is assigned
g a numerical value,, it an be used in mathematical
expressions, in functions, and in any MATLAB statements and
commands.
Variable names can contain letters, digits and the underscore
character.
Variable names must begin with a letter.
MATLAB is case sensitive; it distinguishes between uppercase and
lowercase letters. For example AA, Aa, aA and aa are four different
variables.
Avoid using the names of a built-in-function for a variable.
You can check to see if a variable name is allowed by using the
isvarname command. 14
MATLAB
MATLAB reserves a list of keywords for use by the program, which
you cannot assign as variable names.
names The iskeyword command
causes MATLAB to list these reserved names.
>> iskeyword
ans =
'break'
'case'
case
'catch'
'classdef'
' ti '
'continue'
'else'
'elseif'
'end'
'for'
'function‘
……..
……….. 15
MATLAB
10. Predefined Variables
A number of frequently used variables are already defined when
MATLAB is started.
ans A variable that has the value of the last expression that
was not assigned to a specific variables.
pi
p The number .
eps The smallest difference between two numbers (2-52).
inf Used for infinity.
infinity
i, j Defined as (-1).
NaN Not a number
date returns the current date in a “string format.”
clock Returns a six-member array [year month day hour
minute second].
Fix(clock) result in a format that is easier to read 16
MATLAB
11. Managing Variables
clear Removes all variables from the memory.
clear x, y, z Removes only variables x, y, and z from the
memory.
who Displays a list of the variables currently in the
memory.
whos Displays a list of the variables currently in the
memory and their size together with information
about their bytes and class.
E
Example
l
>> syms x y >> sin(pi)
>> (x+y)*(x-2*y) >> sin(sym(pi))
>> expand(ans) >> sym('1/3')+sym('5/8')
>> factor(ans) >> factor(ans)
p y ((
>> simplify ((x^3-y^3)/(x-y))
y ) ( y)) >> simplify(((sin(x))^2+(cos(x))^2))
p y((( ( )) ( ( )) ))
19
MATLAB
MATLAB has a more robust command, called simple, that
sometimes does a better job than simplify.
simplify
>> solve('x^2-3*x-5')
fzero
You can numerically find the solutions shown on the graph with
fzero, which looks for a zero of a given function near a specified
value of x.
>> fzero('exp(-2*x)-sin(x)',0.5)
e o( e p( ) s ( ) ,0.5)
21
MATLAB
16. User Defined Function
Y can use inline
You i li tot define
d fi your own functions.
f ti
>> f=inline('x^3+2*x^2-5*x+6','x')
>> f(2)
22
MATLAB