Download as ppsx, pdf, or txt
Download as ppsx, pdf, or txt
You are on page 1of 20

ENGFF012

Computer Methods
Lesson 1 - Introduction
Contents
• Introduction to MATLAB
• Tools and Windows included in the MATLAB
Desktop
• Variables
• Using MATLAB as a Calculator
• Output Display Formats
• Useful Commands
Introduction to MATLAB
• MATLAB is MATrix LABoratory.
• A special-purpose computer program to perform
engineering and scientific calculations.
• The MATLAB program is a combination of:
1. An integrated development environment (IDE)
including an editor and debugger
2. Rich set of functions that can perform many types of
technical calculations
3. A procedural programming language
Command
Window

Current Workspace
Directory Browser
Browser

Command
History
Window
Click on MATLAB
Tools and Windows included in the
MATLAB Desktop
Tool Description
Current Directory Browser Shows a list of the files in the current
directory
Command Window A window where the user can type
commands and see immediate results
Workspace Browser Shows variables defined in workspace
Command History Window Displays recently used commands
Using MATLAB as a Calculator
• MATLAB can be used as a calculator to perform
mathematical calculations.
Name of Operation Operators
Addition +
Subtraction -
Multiplication *
Division /
Power ^
Hierarchy of Arithmetic Operations
Precedence Operation
1 The contents of all parentheses are
evaluated, starting from the innermost
parentheses and working outward.
2 All exponentials are evaluated, working
from left to right.
3 All multiplications and divisions are
evaluated, working from left to right.
4 All additions and subtractions are
evaluated, working from left to right.
Example 1
Evaluate the following expressions using MATLAB:
a) 5+8
18
b) 7+
3

c) 52 − 23
1 1
d) 27 +32
3 5
Solution
MATLAB commands:
a) >> 5+8
b) >> 7+18/3
c) >> 5^2-2^3
d) >> 27^(1/3)+32^(1/5)
Exercise 1
Evaluate the following expressions using MATLAB:
a) 49 ÷ 7
Solutions
81 27
b) − >> 49/7
3 (1+23 )
>> 81/3 – 27/(1+2^3)
c) 92 + 23 >> 9^2+2^3
2 2 >> 8^(2/3) – 243^(2/5)
d) 8 -243
3 5
Programming Pitfalls
• If you want to reuse the result of a calculation in
MATLAB, be sure to include a variable name to
store the result. Otherwise, the result will be
overwritten the next time that you perform a
calculation.
Variables
If an equal sign is used in the expression, then the
result of the calculation is saved in the variable
name to the left of the equal sign.
MATLAB Commands:
>> a=3+8 a and b are
variables
>> b=a+7
Example 2
Suppose that we would like to calculate the volume
of a cylinder of radius r and height h. The total
volume of the cylinder is given by the equation:
volume = 𝜋r2h r

h
Solution
If the radius of the cylinder is 0.1 m and the height is
0.5 m, then the volume of the cylinder can be found
using the MATLAB statement:
>> volume=pi*0.1^2*0.5 Use a semicolon at the end of
all MATLAB assignment
OR statements to suppress echoing
>> radius=0.1; of assigned values in the
Command Window. This greatly
>> height=0.5; speeds program execution.

>> volume=pi*radius^2*height
Note that pi is predefined to be the value 3.141592 . . . .
M-files
• As an alternative to typing commands directly in
the Command Window, a series of commands can
be placed into a file, and the entire file can be
executed by typing its name in the Command
Window.
• Such files are called script files. Script files are also
known as M-files, because they have a file
extension of “.m”.
Exercise 2
Suppose that u=1 and v=3. Evaluate the following
expressions using MATLAB:
a)
4u Script file (solution)
3v
u=1;
2𝑣 −2 v=3;
b) (𝑢+𝑣)2
a=(4*u)/(3*v)
𝑣3
c) b=(2*v^-2)/(u+v)^2
𝑣 3 −𝑢3
4
c=v^3/(v^3-u^3)
d) π𝑣 2 d=(4/3)*pi*v^2
3
Output Display Formats
Format Command Results Examples
format short 4 digits after decimal 12.3456
(default format)
format long 15 digits after decimal 12.345678901234567
format bank “dollar and cents” format 12.35
Useful Commands
Command Usage
who List workspace variables
clc Clear contents of the Command Window
clear Clear variables in the workspace
clear x y z Clear variables x, y and z in the workspace
up-arrow () and Scroll through recent commands typed in
down-arrow () keys the Command Window
help search for an exact function name match
lookfor Search the quick summary information in
each function for a match
Summary
• Perform calculation using +, -, *, /, and ^ for
addition, subtraction, multiplication, division, and
exponentiation, respectively.
• Use variable name to store the result.
• Output display formats – long, short, bank
• Useful Commands – who, clc, clear, help, lookfor

You might also like