Command window

Introduction to Matlab
1
Introduction
• Software with numerical, graphical and
programming capabilities.
• Lots of built-in functions.
• Can add toolbox.
Used
• Interactively in Command window, or
• Write program in script or m-files
2
TOPIC 1
Getting Started
Matlab Windows
Variables
Arithmetic Operators
Elementary Mathematic Functions
3
Matlab Default Window
4 Default Windows
Matlab Default Window
5
Matlab Windows
Four default windows
• Command window - executing commands,
running programs, opening other windows,
managing the software. – the main window.
• Current folder window - shows files that are
stored in the current folder.
• Work space window - provides information
about variables that are being used.
• History window - logs commands entered in the
Command window.
6
Controlling Window View (R2010)
• To close unwanted window click x at the top right
corner of the window
• To display on Command window only select Desktop
Layout form the Desktop menu, then Command
Window Only
• To display default view select Desktop Layout form the
Desktop menu, then Default
• A window can be docked and undocked by clicking the
curl up or down arrow at the top right corner of the
window.
7
Controlling Window View (R2013)
8
Controlling Window View (R2013)
9
Other Matlab Windows
• Editor window - creates & debugs script and
function files.
• Figure window - displays graphics.
• Help window - provides help information.
• etc
10
Working in Command Window
• >> is called command prompt
• At >> any command or expression can be entered and
will immediately respond with the result.
• Several commands can be typed in a same line by
separating them with coma (,).
• A command can be continued to the next line by typing
3 periods (…)
• Previously typed command can be recalled to the
command prompt with the up arrow(↑), then modified
if needed. The down arrow (↓) can be used to moved
down the list. Very useful in correcting a long
expression with errors.
11
Working in Command Window - cont’d
>> a=2
a =
2
>> b=5,c=6
b =
5
c =
6
>> d=a+b...
+1
d =
8
>> d=a+b+c
d =
13
12
Working in Command Window - cont’d
• clc command clears the Command window.
• Semicolon (;) is typed at the end of command to
suppress output. If several command are typed in
the same line separated by a semicolon instead of a
coma, the output will not be displayed.
• Percent (%) symbol is typed at the beginning of a
line to designate a comment. Frequently used in
program, no need in Command window.
• A command in Command History window can be
used again in Command window by double-clicking
on the command, or drag the command to the
command window
13
Working in Command Window - cont’d
% ……continue
>> e=d/a
e =
6.5000
>> f=c^a
f =
36
>> g=f/e;
>> % semi colon can also be used to type
>> % several commands commands in a line.
>> % But the output suppressed.
>> h=e-2;j=f*2;
14
Quitting Matlab
• To get out of Matlab either
– type quit or exit at >>, or
– Pick x at top right corner.
15
m-Files and Folder
Creating & Saving Script
Folder
Search Path
Set Path
16
Matlab Program File - Script
• Simple calculation can be done interactively in
Command Window.
• But in many cases many steps are required before
getting final result, and a better way is first to create
a file with a list of commands, save it and then
run(execute) the file.
• This is called computer programs; in MATLAB this file
is called scripts.
• also known as m-files because the extension .m is
used when they are saved.
17
Creating & Saving Script File (R2013)
• To create a script file click new script on the HOME
tab and a new window will appear – the EDITOR.
• Type program statements in the EDITOR. When
finished choose the save down arrow under the
EDITOR tab, and save file.
18
Creating & Saving Script File (R2013)
Current Folder
19
Saving Script File - cont’d
Choose the folder in
which to the save
20
Current Folder
• The current folder is a reference location that
Matlab uses to find files.
• You can always load files and execute scripts and
functions that are in the current folder, even if that
folder is not currently on the Matlab search path.
• Functions in the current folder take precedence over
functions with the same file name that reside
anywhere on the search path.
21
Search Path
• Matlab has a search path that it uses to find scripts /
m-files.
• Matlab’s m-files are organized in folders on your file
system. Many of these folders of m-files are provided
along with Matlab, and users may add others.
• If a user enters a name at the Matlab prompt, the
Matlab interpreter attempts to find the name as
follows:
22
Search Path - cont’d
1) It looks for the name as a variable.
If it is a variable, Matlab displays the current contents of
the variable.
2) It checks to see if the name is an m-file in the current
directory.
If it is, MATLAB executes that function or command.
3) It checks to see if the name is an m-file in any directory
in the search path.
If it is, MATLAB executes that function or command.
Note that Matlab checks for variable names first, so if you
define a variable with the same name as a Matlab function
or command, that function or command becomes
inaccessible. This is a common mistake made by novice
users.
23
Set Path
This how to add a folder
to the search path.
Browse & pick &
select the folder in
Add Folder to Path
box
The
selected
folder
will be
added to
this
search
path list
24
Variables
Assigning
Rules
Predefined variables
Managing variables
25
Variables and Assignment
• Variable is used to store value that frequently change.
• Variable can be created using assignment statement.
Variablename = expression
Assignment operator not an Equal sign
Span equals 5
span = 5
Span gets the value of 5
Variable names
should always
be mnemonic
• Variable created will be shown in Workspace window
• Putting the first value in a variable is called
initialisation (initialising the variable)
• Adding to a variable is known as incrementing
span = span + 1
26
Rules for Identifier
Variable, function and file are example of identifier names.
The rules for identifier names are:
• Must begin with a letter – may be followed by letters,
digits, underscore ( _ ), but not a space.
• Can be up to 63 characters.
• Case sensitive – Span and span are two different
variables.
• Cannot use keywords (e.g. for, else, break, end,
if, while).
To display keywords type iskeyword.
• Names of built-in function (e.g. sqrt, sin, exp) can, but should
not be used as variable names. Once a function is used to define
variable, the function cannot be used.
27
Predefined Variables
Frequently used variables already defined by Matlab.
Some of predefined variables are:
Name
ans
pi
Description
A variable that has the value of the last expression
that was no assigned
The value π
inf
Used for infinity
NaN
Stands for Not-a-Number. Used when Matlab
cannot determine a valid numeric value. e.g. 0/0
Predefined variables can be redefined to have any
other value.
28
Commands for Managing Variables
• Who – shows variables that have been defined in
Command window.
• Whos – as above + more information as in workspace
window (size, bytes, class).
• Clear – clears all variables.
• Clear variablename – clears out a particular variable.
29
Commands for Managing Variables – cont’d
>> r=4;
>> ccircle=2*pi*r
ccircle =
25.1327
>> pi*r^2
ans =
50.2655
>> Hrad=ans/ccircle
Hrad =
2
>> who
Your variables are:
Area
Hrad
ans
r
30
Commands for Managing Variables – cont’d
>> whos
Name
Attributes
Area
Hrad
ans
r
Size
1x1
1x1
1x1
1x1
Bytes
8
8
8
8
Class
double
double
double
double
>> clear
>> who
31
Arithmetic Operators
Symbols
Precedence
Mathematical Functions
32
Arithmetic Operation with Scalars
Scalars are numbers.
Symbols for arithmetic operations are:
Operation
Addition
Subtraction
Multiplication
Right Division
Left Division
Symbol
+
*
/
Example
8 + 2 = 10
8–2=6
8 * 2 = 16
8/2=4
2\8=4
\
Exponentiation
8 ^ 2 = 64
^
• Symbols are as in calculator except left division, \.
For scalar \ is the inverse of /.
• Left division is mostly used in matrix operations. 33
Operation Order of Precedence
Matlab executes calculations according to the order
of precedence as listed below (the same as used in
most calculators)
Precedence Operator
1
Parentheses ( ). For nested (), the inner most () are
executed first.
2
^ exponentiation
3
4
5
- negation
*, / , \ (equal precedence)
+, - (equal precedence)
Expression with two or more operations equal of precedence
are evaluated from the left to right.
34
Example - Operation Order of Precedence
a) 5 + 6 / 3 = 7
b) (7 + 5) / 2 = 6
c) 4 + 5 / 2 + 3 = 9.5
d) 3 ^ 3 / 4 = 6.75
e) 8 ^ (1/3) + 32^0.2 = 4
f) 35.46 + 456 / 4 – 20.53 + 100 / (2^3) …
-12.65 = 128.87
35
Display Formats
Command
Description
Example
Format short
Print 4 decimal places
130/7 = 18.5714
Format long
Print 15 decimal places
130/7= 18.571428571428573
Format short e
Scientific notation with 4
decimal places
130/7 =1.8571e+001
Format long e
Scientific notation with 15
decimal places
1.857142857142857e+001
Format short g
Print 5 digits
18.571
Format long g
Print 15 digits
18.5714285714286
Format bank
Print 2 decimal places
18.57
Format compact
Eliminates empty lines to allow more lines with information
displayed on the screen
Format loose
Add empty lines (opposite to compact)
36
Built-in Functions
• Matlab has a large library of functions.
• To call a function, the name of the function is given
followed by the arguments that are passed to the
function in the parentheses.
e.g
>> sqrt(81)
Function
name
(value 9 would be returned)
argument
>> rem(15,4)
(value 3 would be returned)
37
Elementary Mathematic Functions
• Some of the commonly used mathematical functions is
listed below
Function
Description
Example
sqrt(x)
Square root
sqrt(64)= 8
abs(x)
Absolute value
abs(-12) = 12
round(x)
Round to the nearest integer
round(17/5) = 3
exp(x)
Exponential (ex)
Exp(3)= 20.0855
factorial(x)
Factorial function x!
Factorial(4)= 24
sin(x)
Sine of angle x radian
sin(1)= 0.8415
sind(x)
Sine of angle x degree
sind(30)= 0.5000
atand(x)
Inverse tangent in degree
atand(1) = 45
log(x)
Natural logarithm
log(10) = 2.3026
38
Help to get Functions
• Type help at the prompt in Command window to
show a list of help topics;
>> help
Example of help topics are ;
– matlab\elfun (elementary math functions)
– matlab\ops (operators that can be used in expressions)
• To see a list of functions contain within in a particular
help topic; type help followed by the topic name
e.g. >> help elfun
• To find out what a particular function does and how
to call; type help followed by the function name.
e.g. >> help sin
39
Problem Examples
40
Problem Example 1
For the mathematical identity below, verify that the identity is
correct by calculating the values of the left and right sides of
the equation, substituting x =12o.
4𝑡𝑎𝑛𝑥−4𝑡𝑎𝑛3 𝑥
tan 4x =
1−6𝑡𝑎𝑛2 𝑥+𝑡𝑎𝑛4 𝑥
>> x=12
x =
12
>> lhs=tand(4*x)
lhs =
1.1106
tan(x),
if x in radian
>> rhs=(4*tand(x)-4*(tand(x)^3))...
/(1-6*(tand(x))^2+(tand(x))^4)
rhs =
1.1106
41
Problem Example 2
Calculate the support reactions and the bending moment at the
loaded point for the beam below.
>> rb=58*2.5/6
rb =
24.1667
>> ra=58-24.1667
ra =
33.8333
>> bmc=ra*2.5
bmc =
84.5833
42
Problem Example 3
Determine the location of the centroid of the section and its moment
of inertia about the horizontal centroidal axis.
>>
>>
>>
>>
>>
>>
>>
Ix
arec=60*100;yrec=50;
acir=pi*15^2;ycir=70;
atot=arec-acir;
ybar=(arec*yrec - acir*ycir)/atot;
drec=ybar-yrec;dcir=ybar-ycir;
Irec=60*100^3/12;Icir=pi*15^4/4;
Ix=(Irec+arec*drec^2) - (Icir+acir*dcir^2)
=
4.6397e+06
43
Problem Example 4
Determine the resultant of the forces
acting on the bracket shown.
>>
>>
>>
>>
>>
>>
fr
f1=450;a1=50;
f2=200;a2=0;
f3=600;a3=285;
fx=f1*cosd(a1) + f2*cosd(a2) + f3*cosd(a3);
fy=f1*sind(a1) + f2*sind(a2) + f3*sind(a3);
fr=sqrt(fx^2+fy^2)
=
Résultant angle?
685.9935
44
More practice see Problem A1
Thank You
45