You are on page 1of 34

v

Engineering MATLAB, MEEG221


Week 1: Starting with MATLAB
Amit Shukla, PhD
Mechanical Engineering Department
Background
! MATLAB

is a high-level language and interactive


environment for numerical computation, visualization,
and programming.
! MATLAB (MATrix LABoratory)
! Cleve Barry Moler is an American mathematician,
who founded MathWorks sometimes around 1984.
! hup://en.wlklpedla.org/wlkl/MA1LA8
Engineering MATLAB, MEEG221
Week 1: Starting with MATLAB
Application of Matlab
Engineering MATLAB, MEEG221
Week 1: Starting with MATLAB
http://www.mathworks.com/help/matlab/examples/2-d-plots.html
Application of Matlab
Engineering MATLAB, MEEG221
Week 1: Starting with MATLAB
http://www.mathworks.com/help/matlab/examples/index.html
MATLAB Desktop View
Engineering MATLAB, MEEG221
Week 1: Starting with MATLAB
(1) Current Folder Access your files.
(2) Command Window Enter commands at the command line, indicated by
the prompt (>>).
(3) Workspace Explore data that you create or import from files.
(4) Command History View or rerun commands that you entered at the
command line.
Current Folder
Command Window
Workspace
Command History
MATLAB Help
Engineering MATLAB, MEEG221
Week 1: Starting with MATLAB
Help, Toolboxes
MATLAB Help
Arithmetic Operations with Scalars
Engineering MATLAB, MEEG221
Week 1: Starting with MATLAB
!"#$%&'() +,-.'/) 01%-"/#)
Addluon + 3 + 3
SubLracuon - 3 - 3
Mulupllcauon * 3 * 3
8lghL dlvlslon / 3 / 3
Lxponenuauon ^ 3^3 = 123
Order of Precedence
- MATLAB executes the calculations according to the
order of precedence displayed from highest to lowest
as shown below.
1 (Highest) Parenthesis
2 Exponentiation
3 Logical NOT (~)
4 Multiplication, division
5 Addition, subtraction
6 Relational operators( >, <, >=, <=, ==, ~= )
7 Logical AND ( & )
8 (Lowest) Logical OR ( | )
Engineering MATLAB, MEEG221
Week 1: Starting with MATLAB
Order of Precedence
- Example:
5(5x4 - 2)/6 + 8
1/3
(2(3x7 + 4)
1/2
)
=5(20 - 2)/6 + 8
1/3
(2(21 + 4)
1/2
)
=5(18)/6 + 8
1/3
(2(25)
1/2
)
=90/6 + 8
1/3
(2(5))
=15 + 2(10)
=35


Engineering MATLAB, MEEG221
Week 1: Starting with MATLAB
Arithmetic Operation: Examples
Example: Evaluate 3+4.
>>3+4
ans=7


Engineering MATLAB, MEEG221
Week 1: Starting with MATLAB
Arithmetic Operation: Examples
>>3+4
ans = 7
>>3+4/5-0.1
ans = 3.7
>>3^4*2
ans =162
>>27^(1/3)
ans =3
>> 3+4 + 3+4/5-0.1 + 3^4*2 + 27^(1/3)
+ 3+4 + 3+4/5-0.1 + 3^4*2 + 27^(1/3)
ans =351.4

Engineering MATLAB, MEEG221
Week 1: Starting with MATLAB
Display Formats


Engineering MATLAB, MEEG221
Week 1: Starting with MATLAB
Command Description Example
format compact no empty line for output
format loose add empty line for output
format bank 2 decimal digits 41.12
format short fixed point with 4 decimal digits for
0.001<number<1000
Otherwise short e
41.1234
format long fixed point with 14 decimal digits for
0.001<number<1000
Otherwise long e
41.12345678901
234
format short e scientific notion with 4 decimal digits 4.1123e+001
format long e scientific notion with 15 decimal digits 4.112345678901
234e+001
Display format does not change the stored value
Display Format: 41.123456789012345
Engineering MATLAB, MEEG221
Week 1: Starting with MATLAB
14
Display Format: 41.123456789012345
Elementary Math Functions


Engineering MATLAB, MEEG221
Week 1: Starting with MATLAB
Expression Description - MATLAB Example
X
1/2
sqrt(x) >> sqrt(9) " ans = 3
X
1/n
Real nth root of a real number >> nthroot(64,6) " ans = 2
e
x
exp(x) >> exp(5) " ans = 148.4132
|x| abs(x) >> abs(-31) " ans = 31
ln(x) log(x) >> log(1000) " ans = 6.9078
log10(x) log10(x) >> log10(100) " ans = 2
X! Factorial(x) >> factorial (4) = " ans = 24
Trigonometric Math Functions


Engineering MATLAB, MEEG221
Week 1: Starting with MATLAB
Function Description Example
sin(x) sind(x) Sine of angle x (x in radians). Sine of angle x (x in degrees).
>> sin(pi/6)
ans = 0.5000
cos(x) cosd(x) Cosine of angle x (x in radians). Cosine of angle x (x in degrees).
>> cosd(30)
ans = 0.8660
tan(x) tand(x) Tangent of angle x (x in radians). Tangent of angle x (x in degrees).
>> tan(pi/6)
ans = 0.5774
cot(x) cotd(x)
Cotangent of angle x (x in radians). Cotangent of angle x (x in
degrees).
>> cotd(30)
ans = 1.7321
Function Description Example
sin(x) sind(x) Sine of angle x (x in radians). Sine of angle x (x in degrees).
>> sin(pi/6)
ans = 0.5000
cos(x) cosd(x) Cosine of angle x (x in radians). Cosine of angle x (x in degrees).
>> cosd(30)
ans = 0.8660
Rounding Functions


Engineering MATLAB, MEEG221
Week 1: Starting with MATLAB
Function Description Example
round(x) Round to the nearest integer.
>> round(17/5)
ans = 3
fix(x) Round toward zero.
>> fix(13/5)
ans = 2
ceil(x) Round toward infinity.
>> ceil(11/5)
ans = 3
floor(x) Round toward minus infinity.
>> floor(-9/4)
ans = -3
rem(x,y) Returns the remainder after x is divided by y.
>> rem(13,5)
ans = 3
sign(x)
Signum function, it returns
1 if x>0, -1 if x<0,
0 if x=0.
>> round(-5)
ans = -1
sign(x) =
!1,
0,
+1,
"
#
$
%
$
x < 0
x = 0
x > 0
The assignment operator: Scalar Variable
>Example: Define a variable a1 that has a value of 5.
>>a1=5 " a1=5
Engineering MATLAB, MEEG221
Week 1: Starting with MATLAB
Rules for Variable Names
1. Begin with a letter
2. Case sensitive (AA and Aa are different)
3. Up to 63 characters
4. No punctuation characters (e.g. ,, :)
5. No space between characters (use underscore _ when space
is desired)
6. No names of built-in function (e.g. cos, sqrt, exp)
Engineering MATLAB, MEEG221
Week 1: Starting with MATLAB
Xyz
Var1
Var_1
Xy z
cos
Va:r_1
Reserved Keywords & Predefined Variables
break case catch continue for switch persistent otherwise if

elseif else end function try return while global



Engineering MATLAB, MEEG221
Week 1: Starting with MATLAB
2%$3%./# 4#5(3&'(
%() A varlable LhaL has Lhe value of Lhe lasL expresslon LhaL was noL
asslgned Lo a speclc varlable. lf Lhe user does noL asslgn Lhe value of an
expresslon Lo a varlable, MA1LA8 auLomaucally sLores Lhe resulL ln ans.

"3 1he number n.
#") 1he smallesL dlerence beLween Lwo numbers. Lqual Lo 2^(-32), whlch
ls approxlmaLely 2.2204e-016.

3(6 used for lnnlLy.
3 uened as , whlch ls: 0 + 1.0000l.
7 Same as l.
8%8 SLands for noL-a-number. used when MA1LA8 cannoL deLermlne a valld
numerlc value. Lxample: 0/0.
!1
Commands for Managing Variables

Engineering MATLAB, MEEG221
Week 1: Starting with MATLAB
>>a=5
a=5
>>b=(6+4)/2
b=5
>>c=a^2
c=25
>>d=a+3 ;
>>d
d=8
>>d2=sin(a/4)+exp(b)+sqrt(c)
d2=154.3621
>>f=round(d2) f=154
Command for Managing Variables
Engineering MATLAB, MEEG221
Week 1: Starting with MATLAB
>> a=5, b=(6+4)/2, c=a^2, d=a+3; ...
d1=sin(a/4)+exp(b)+sqrt(c), f=round(d1)
a =
5
b =
5
c =
25
d1 =
154.3621
f =
154
>>
Examples
Engineering MATLAB, MEEG221
Week 1: Starting with MATLAB
>> who
Your variables are:
a b c d d1 f
>> whos
Name Size Bytes Class
a 1x1 8 double array
b 1x1 8 double array
c 1x1 8 double array
d 1x1 8 double array
d1 1x1 8 double array
f 1x1 8 double array
Grand total is 6 elements using 48 bytes
Examples
Engineering MATLAB, MEEG221
Week 1: Starting with MATLAB
>> clear c d1
>> who
Your variables are:
a b d f

>> clear
>> who
>>
Commands for Managing Variables


Engineering MATLAB, MEEG221
Week 1: Starting with MATLAB
9'--%(: 4#);$3"&'(
< no ouLpuL of command
= Conunue Lo nexL llne
> 8emarks
;/; 8emoves all varlables from Lhe memory.
?@' ulsplays a llsL of Lhe varlables currenLly ln Lhe memory.
?@') ulsplays a llsL of Lhe varlables currenLly ln Lhe memory and
Lhelr slzes LogeLher wlLh lnforma- uon abouL Lhelr byLes and
class.
9/#%$ 1 , A 8emoves only varlables x, y, and z from Lhe memory.
Creating and Saving a Script File
Engineering MATLAB, MEEG221
Week 1: Starting with MATLAB
Also called M-files because of the extension .m
A sequence of MATLAB commands.
It can be edited and executed many times
=>File =>New =>M-file
=>File =>Save as=>
Rules for variables applies for the name of M-files
Creating and Saving a Script File
Engineering MATLAB, MEEG221
Week 1: Starting with MATLAB
M-File/s in the current directory
Current directory
Creating and Saving a Script File
Engineering MATLAB, MEEG221
Week 1: Starting with MATLAB
1.8 Script Files 21
1.8.2 Creating and Saving a Script File
In MATLAB script files are created and edited in the Editor/Debugger Window.
This window is opened from the Command Window. In the File menu, select
New, and then select Script. An open Editor/Debugger Window is shown in Fig-
ure 1-6.
Once the window is open, the commands of the script file are typed line by
line. MATLAB automatically numbers a new line every time the Enter key is
pressed. The commands can also be typed in any text editor or word processor
program and then copied and pasted in the Editor/Debugger Window. An example
of a short program typed in the Editor/Debugger Window is shown in Figure 1-7.
The first few lines in a script file are typically comments (which are not executed
since the first character in the line is %) that describe the program written in the
script file.
Figure 1-6: The Editor/Debugger Window.
Figure 1-7: A program typed in the Editor/Debugger Window.
The commands in the script file are
typed line by line. The lines are num-
bered automatically. A new line
starts when the Enter key is pressed.
Line
number
Comments.
Define three
variables.
Calculating the two roots.
The Run icon.
Creating and Saving a Script File
Engineering MATLAB, MEEG221
Week 1: Starting with MATLAB
M-File/s in the current directory
Current directory
1.8 Script Files 21
1.8.2 Creating and Saving a Script File
In MATLAB script files are created and edited in the Editor/Debugger Window.
This window is opened from the Command Window. In the File menu, select
New, and then select Script. An open Editor/Debugger Window is shown in Fig-
ure 1-6.
Once the window is open, the commands of the script file are typed line by
line. MATLAB automatically numbers a new line every time the Enter key is
pressed. The commands can also be typed in any text editor or word processor
program and then copied and pasted in the Editor/Debugger Window. An example
of a short program typed in the Editor/Debugger Window is shown in Figure 1-7.
The first few lines in a script file are typically comments (which are not executed
since the first character in the line is %) that describe the program written in the
script file.
Figure 1-6: The Editor/Debugger Window.
Figure 1-7: A program typed in the Editor/Debugger Window.
The commands in the script file are
typed line by line. The lines are num-
bered automatically. A new line
starts when the Enter key is pressed.
Line
number
Comments.
Define three
variables.
Calculating the two roots.
The Run icon.
Examples
Engineering MATLAB, MEEG221
Week 1: Starting with MATLAB
1.10 Problems 27
When the script file is executed, the following (the values of the variables B, t,
years, and months) is displayed in the Command Window:
1.10 PROBLEMS
The following problems can be solved by writing commands in the Command
Window, or by writing a program in a script file and then executing the file.
1. Calculate:
(a) (b)
2. Calculate:
(a) (b)
3. Calculate:
(a) (b)
4. Calculate:
(a) (b)
>> format short g
B =
20011
t =
16.374
years =
16
months =
5
The values of the variables B, t,
years, and months are displayed
(since a semicolon was not typed at the
end of any of the commands that calcu-
late the values).
14.8
2
6.5
2
( )
3.8
2
----------------------------------
55
2 14
------------------- 3.5 ( )
3 e
6
524 ln
-------------- 206
1 3
16.5
2
8.4 70 ( )
4.3
2
17.3
-----------------------------------------
5.2
3
6.4
2
3
1.6
8
2
-----------------------------------
13.3
5
----------


1.5
15
10 3.7
2
10
1365 ( ) log 1.9
-------------------------------------------


2.5
3
16
216
22
---------


1.7
4
14
------------------------------------ 2050
4
2.3
2
1.7
1 0.8
2
( )
2
2 0.87 ( )
2
-------------------------------------------------------------------
2.34
1
2
---
2.7 5.9
2
2.4
2
( ) 9.8 51 ln
2. (a)
>> 16.5^2*(8.4 - sqrt(70))/(4.3^2 - 17.3)
ans =
7.6412
2. (b)
>> (5.2^3 - 6.4^2 + 3)/(1.6^8 - 2) + (13.3/5)^1.5
ans =
6.8450
Examples
Engineering MATLAB, MEEG221
Week 1: Starting with MATLAB
28 Chapter 1: Starting with MATLAB
5. Calculate:
(a) (b)
6. Define the variable x as x = 2.34, then evaluate:
(a) (b)
7. Define the variable t as t = 6.8, then evaluate:
(a) (b)
8. Define the variables x and y as x = 8.3 and y = 2.4, then evaluate:
(a) (b)
9. Define the variables a, b, c, and d as:
, , , and , then evaluate:
(a) (b)
10. A cube has a side of 18 cm.
(a) Determine the radius of a sphere that has the same surface area as the
cube.
(b) Determine the radius of a sphere that has the same volume as the cube.
11. The perimeter P of an ellipse with semi-minor axes a and
b is given approximately by: .
(a) Determine the perimeter of an ellipse with in.
and in.
(b) An ellipse with has a perimeter of cm. Determine a and b.
12. Two trigonometric identities are given by:
(a) (b)
For each part, verify that the identity is correct by calculating the values of the
left and right sides of the equation, substituting .
7
9
------


sin
5
7
---



cos
2
-----------------------
1
7
---
5
12
------



tan
64 tan
14 cos
2
-------------------
3 80 sin
0.9
3
--------------------
55 cos
11 sin
-----------------
2x
4
6x
3
14.8x
2
9.1
e
2x
14 x
2
x
------------------------------
t
2
t
3
( ) ln
75
2t
------
0.8t 3 ( ) cos
x
2
y
2
x
2
y
2
----- xy x y
x y
x 2y
--------------


2
x
y
--
a 13 b 4.2 c 4b ( ) a d
abc
a b c
---------------------
a
b
c d
------------
d
c
---
a
b
---
a b
2
( ) c d ( )
a
2
b
2
d c ( )
--------------------- b a c d ( ) ln
a
b
P 2
1
2
---
a
2
b
2
( )
a 9
b 3
b 2a P 20
4x sin 4 x x cos sin 8 x sin
3
x cos 2x cos
1 x tan
2
1 x tan
2
----------------------
x

9
---
8. (a)
>> x = 8.3; y = 2.4;
>> x^2 + y^2 - (x^2 / y^2)
ans =
62.6899
8. (b)
>> x = 8.3; y = 2.4;
>> sqrt(x*y) - sqrt(x + y) + ((x - y)/(x - 2*y))^2 - sqrt(x/y)
ans =
2.1741
Example
Engineering MATLAB, MEEG221
Week 1: Starting with MATLAB
Four circles are placed as shown in the figure. At each point where two
circles are in contact they are tangent to each other. Determine the
distance between the centers C2 and C4.
The radii of the circles are:
R1 = 16mm, R2 = 6.5mm, R3 = 12mm, and
R4 = 9.5mm.
24 Chapter 1: Starting with MATLAB
1.9 EXAMPLES OF MATLAB APPLICATIONS
Sample Problem 1-1: Trigonometric identity
A trigonometric identity is given by:
Verify that the identity is correct by calculating each side of the equation, substi-
tuting .
Solution
The problem is solved by typing the following commands in the Command Win-
dow.
Sample Problem 1-2: Geometry and trigonometry
Four circles are placed as shown in the figure.
At each point where two circles are in contact
they are tangent to each other. Determine the
distance between the centers C
2
and C
4
.
The radii of the circles are:
mm, mm, mm, and
mm.
Solution
The lines that connect the centers of the cir-
cles create four triangles. In two of the trian-
gles, C
1
C
2
C
3
and C
1
C
3
C
4
, the lengths of all
the sides are known. This information is used to
calculate the angles
1
and
2
in these triangles by
using the law of cosines. For example,
1
is cal-
culated from:
>> x=pi/5;
>> LHS=cos(x/2)^2
LHS =
0.9045
>> RHS=(tan(x)+sin(x))/(2*tan(x))
RHS =
0.9045
x
2
--- cos
2
x tan x sin
2 x tan
---------------------------
x

5
---
Define x.
Calculate the left-hand side.
Calculate the right-hand side.
R
1
16 R
2
6.5 R
3
12
R
4
9.5
24 Chapter 1: Starting with MATLAB
1.9 EXAMPLES OF MATLAB APPLICATIONS
Sample Problem 1-1: Trigonometric identity
A trigonometric identity is given by:
Verify that the identity is correct by calculating each side of the equation, substi-
tuting .
Solution
The problem is solved by typing the following commands in the Command Win-
dow.
Sample Problem 1-2: Geometry and trigonometry
Four circles are placed as shown in the figure.
At each point where two circles are in contact
they are tangent to each other. Determine the
distance between the centers C
2
and C
4
.
The radii of the circles are:
mm, mm, mm, and
mm.
Solution
The lines that connect the centers of the cir-
cles create four triangles. In two of the trian-
gles, C
1
C
2
C
3
and C
1
C
3
C
4
, the lengths of all
the sides are known. This information is used to
calculate the angles
1
and
2
in these triangles by
using the law of cosines. For example,
1
is cal-
culated from:
>> x=pi/5;
>> LHS=cos(x/2)^2
LHS =
0.9045
>> RHS=(tan(x)+sin(x))/(2*tan(x))
RHS =
0.9045
x
2
--- cos
2
x tan x sin
2 x tan
---------------------------
x

5
---
Define x.
Calculate the left-hand side.
Calculate the right-hand side.
R
1
16 R
2
6.5 R
3
12
R
4
9.5
(C
2
C
3
)
2
= (C
1
C
2
)
2
+ (C
1
C
3
)
2
! 2(C
1
C
2
)(C
1
C
3
) cos("
1
)
(C
3
C
4
)
2
= (C
1
C
3
)
2
+ (C
1
C
4
)
2
! 2(C
1
C
3
)(C
1
C
4
) cos("
2
)
(C
2
C
4
)
2
= (C
1
C
2
)
2
+ (C
1
C
4
)
2
! 2(C
1
C
2
)(C
1
C
4
) cos("
3
)
!
3
= !
1
+!
2
Examples
Engineering MATLAB, MEEG221
Week 1: Starting with MATLAB
1.9 Examples of MATLAB Applications 25
Next, the length of the side C
2
C
4
is calculated by considering the triangle
C
1
C
2
C
4
. This is done, again, by using the law of cosines (the lengths C
1
C
2
and
C
1
C
4
are known and the angle
3
is the sum of the angles
1
and
2
).
The problem is solved by writing the following program in a script file:
When the script file is executed, the following (the value of the variable C2C4) is
displayed in the Command Window:
Sample Problem 1-3: Heat transfer
An object with an initial temperature of that is placed at time t = 0 inside a
chamber that has a constant temperature of will experience a temperature
change according to the equation
where T is the temperature of the object at time t, and k is a constant. A soda can at
a temperature of F (after being left in the car) is placed inside a refrigerator
where the temperature is F. Determine, to the nearest degree, the temperature
of the can after three hours. Assume k = 0.45. First define all of the variables and
then calculate the temperature using one MATLAB command.
Solution
The problem is solved by typing the following commands in the Command Win-
dow.
% Solution of Sample Problem 1-2
R1=16; R2=6.5; R3=12; R4=9.5;
C1C2=R1+R2; C1C3=R1+R3; C1C4=R1+R4;
C2C3=R2+R3; C3C4=R3+R4;
Gama1=acos((C1C2^2+C1C3^2-C2C3^2)/(2*C1C2*C1C3));
Gama2=acos((C1C3^2+C1C4^2-C3C4^2)/(2*C1C3*C1C4));
Gama3=Gama1+Gama2;
C2C4=sqrt(C1C2^2+C1C4^2-2*C1C2*C1C4*cos(Gama3))
C2C4 =
33.5051
C
2
C
3
( )
2
C
1
C
2
( )
2
C
1
C
3
( )
2
2 C
1
C
2
( ) C
1
C
3
( )
1
cos
Define the Rs.
Calculate the
lengths of the sides.
Calculate
1
,
2
, and
3
.
Calculate the length of
side C
2
C
4
.
T
0
T
s
T T
s
T
0
T
s
( )e
kt
120
38
1.9 Examples of MATLAB Applications 25
Next, the length of the side C
2
C
4
is calculated by considering the triangle
C
1
C
2
C
4
. This is done, again, by using the law of cosines (the lengths C
1
C
2
and
C
1
C
4
are known and the angle
3
is the sum of the angles
1
and
2
).
The problem is solved by writing the following program in a script file:
When the script file is executed, the following (the value of the variable C2C4) is
displayed in the Command Window:
Sample Problem 1-3: Heat transfer
An object with an initial temperature of that is placed at time t = 0 inside a
chamber that has a constant temperature of will experience a temperature
change according to the equation
where T is the temperature of the object at time t, and k is a constant. A soda can at
a temperature of F (after being left in the car) is placed inside a refrigerator
where the temperature is F. Determine, to the nearest degree, the temperature
of the can after three hours. Assume k = 0.45. First define all of the variables and
then calculate the temperature using one MATLAB command.
Solution
The problem is solved by typing the following commands in the Command Win-
dow.
% Solution of Sample Problem 1-2
R1=16; R2=6.5; R3=12; R4=9.5;
C1C2=R1+R2; C1C3=R1+R3; C1C4=R1+R4;
C2C3=R2+R3; C3C4=R3+R4;
Gama1=acos((C1C2^2+C1C3^2-C2C3^2)/(2*C1C2*C1C3));
Gama2=acos((C1C3^2+C1C4^2-C3C4^2)/(2*C1C3*C1C4));
Gama3=Gama1+Gama2;
C2C4=sqrt(C1C2^2+C1C4^2-2*C1C2*C1C4*cos(Gama3))
C2C4 =
33.5051
C
2
C
3
( )
2
C
1
C
2
( )
2
C
1
C
3
( )
2
2 C
1
C
2
( ) C
1
C
3
( )
1
cos
Define the Rs.
Calculate the
lengths of the sides.
Calculate
1
,
2
, and
3
.
Calculate the length of
side C
2
C
4
.
T
0
T
s
T T
s
T
0
T
s
( )e
kt
120
38
Thanks!


Question?
Engineering MATLAB, MEEG221
Week 1: Starting with MATLAB

You might also like