You are on page 1of 50

MATLAB

An INTRODUCTION WITH APPLICATIONS

By: Lee Kim Yee

http://www.mathworks.com
Outline:
 Introduction to MATLAB (15mins)
 Starting with MATLAB (15mins)

 Basic operations in MATLAB (30mins)

 M-file(15mins)
 Function file in MATLAB(30mins)
 Programming with MATLAB (15mins)
 Hands On Applications (60mins)
1.0 MATLAB ???

MATLAB

A tool to solve problems!!!


Scilab, Maple,
Competitors to MATLAB:
Mathematica, Python et al…
1.1 What is MATLAB?

MATLAB (matrix laboratory) is a numerical computing


environment and fourth-generation programming
language developed by MathWorks.
 MATLAB allows matrix manipulations, plotting of
functions and data, implementation of algorithms,
creation of user interfaces, and interfacing with
programs written in other languages, including C,
C++, Java, and Fortran.
 The standard MATLAB program has tools
(functions) that can be used to solve common
problems.
 has optional toolboxes that are collection of
specialized programs designed to solve specific
types of problems. Examples: tool boxes for
signal processing, symbolic calculations and
control systems.
1.2 Why learn MATLAB?
 powerful language for technical
computing, combine calculation
and graphic plotting
 Easy to learn - “Learning by Doing”
 Can as a calculator or as a programming language
 In 2004, MATLAB had around one million users
across industry and academia.
 MATLAB users come from various backgrounds of
engineering, science, and economics. MATLAB is
widely used in academic and research institutions
as well as industrial enterprises.
2.0 Starting with MATLAB
 Run the program
2.1 Command Window: Main window
 Commands are typed next to the prompt (>>) and are
executed when the Enter key is pressed.
 Output will generated, unless a semicolon (;) is typed at
the end.
 Symbol % is typed in the beginning of a comment.
 clc –clear the command window
 Up-arrow key ( ) can be used to recall commands that
were typed before.
Figure Window: open automatically when graphics
commands are executed
Editor Window: used for writing and editing programs
(detail in later creating script files section)
Help Window: contains help information
2.2 Elementary arithmetic operations with scalars

Operation Symbol Example

Addition + 5+3

Subtraction - 5-3

Multiplication * 5*3

Right division / 5/3

Left division \ 5\3

Exponentiation ^ 5^3

MATLAB as a calculator
2.3 Elementary math built-in functions
2.4 Display format
Activity 1:
Q1: Define the variable a, b, c, and d as a
=14.75, b = -5.92, c = 61.4, and d =
0.6(ab-c). Evaluate:

>>sola= -829.5390 >>solb = -84.7934


 Q2: Define two variables: alpha = 5π/9,
beta = π /7. Using these variables, show
that the following trigonometric identity is
correct by calculating the value of the left
and right sides of the equation.
Sinαcosβ= ½[ sin(α-β)+ sin(α+β)]

>>leftside = 0.8873
>>rightside = 0.8873
or
>>leftside= =rightside
ans = 1
3.0 Array
 is a list of numbers arranged in rows and/or columns
 Vector, matrix

Creating a simple array (vector)


3.1 Creating an array (Vector)
3.2 Creating a two-dimensional array (matrix)
3.3 Array addressing
3.4 Using a colon : in addressing arrays
3.5 string
 an array of characters
 Include letters, digits, other symbols, and spaces
4.0 Mathematical Operations with Arrays
4.1 Addition and subtraction of arrays
4.2 Multiplication of arrays

Do we have array division (matrix division) ???


4.3 Element-by element operation
Activity 2:
 Q3: Use MATLAB to show that the sum of the infinite series

converges to π/4. Do it by computing the sum for:


(a) n = 100
(b) n = 1000
(c) n = 5000
In each part create a vector n in which the first element is 0, the
increment is 1 and the last term is 100, 1000, 5000. Then, use
element-by-element calculation to create a vector in which the
elements are . Finally, use the function sum to add the
terms of the series. Compare the values obtained in parts, a, b,
and c with the value of π/4. (Do not forget to type semicolons at the
end of commands that otherwise will display large vectors.)

(a) ans = 0.78787335026775, (b) ans = 0.78564791358489 (c) ans = 0.78544815339895


5.0 Plotting
Additional optional arguments that used to specify the color
and style of the line and the color and type of markers, if
any are desired.
5.1 Formatted plot
Include axis labels, a title, and other features

Example:
6.0 Script Files (M-file)
 a file that contains a sequence of MATLAB commands
(also called a program)
 convenient  can stored, edited later, and executed
many times

Creating and saving a script file


 In MATLAB window select  File>New>M-file

Global variable
 Once created in one part of MATLAB, are recognized in
other parts of MATLAB. (common type)
 There are different types of files in MATLAB, called
function files, that normally do not share their variables
with parts of the program
Input to a script file
 When a script file is executed, the variables used in the
calculations within the file must have assigned value.
 3 ways
 1. define in script file
 2. define in Command window
 3. using the input command

Output from a script file


 disp displays the output on the screen
 fprintf display the output on the screen or to save the output to a file
Example: Create a program that calculates the monthly and
total pay for a loan for a given loan amount, interest rate,
and duration (years).
Payment Formula:

ratem(1  ratem) n
mpay = monthly payment

mpay  amount
amount = Loan Amount
ratem= Interest Rate =(ratex0.01)/12
(1  ratem) n  1 n= Loan Period = years x 12

Calculate the monthly and total pay of a 4-years loan of RM 25,000 with
interest rate of 7.5%

 Input=  Output=
 amount  Monthly pay (mpay)
 rate
 Total pay (tpay)
 years
7.0 Function Files

 used as a function
 have input and output arguments
Extend: Change the program into a function file
(or create a simple function) named loan that calculates the
monthly and total pay .
 Calculate the monthly and total pay of a 4-years loan of RM
25,000 with interest rate of 7.5%

For calculating, use command window


8.0 Programming in MATLAB
 A computer program is a sequence of computer
commands.
 Simple program commands are executed one after the
other in the order that they are typed.
 Many situations, however, require more sophisticated
programs
 MATLAB provides several tools that can be used to
control the flow of a program. Examples: for, while
 Changing the flow of a program requires some kind of
decision-making process using relational and logical
operations
8.1 Relational and Logical Operations
 To make decisions that control the flow of a computer
program.
 Example: compares two number (e.g., 5 <8) is true or
false. Statement is true, 1; false, 0.
8.2 Conditional Statements, if-else Structures
 Command that allows MALTAB to make a decision of
whether to execute a group of commands that follow the
conditional statement, or to skip these commands.
The if-end structure
The if-else-end structure
The if-elseif-else-end structure
8.3 Loops
for-end loops
Example: A script file program that changes the elements of given vector
such that elements that are positive and are divisible by 3 and/or 5 are
doubled. Elements that are negative but greater than -5 are raised to the
power of 3, and all the other elements are unchanged
Activity 3:
Q4: Write a user-defined function that creates a vector
whose elements are the primary numbers between two
numbers. For the function name and arguments use pr
= Primary(a,b). The input to the function are two
numbers (integers) a and b (such that a < b), and the
output pr is a vector in which the elements are primary
between a and b.
Extra Activity :
Q5. Write a user-defined function that sorts the elements of a
vector (of any length) from the largest to the smallest. For the
function name and arguments use y = downsort (X). The input to
the function is a vector x of any length, and the output y is a vector
in which the elements of x are arranged in descending order. Do
not use the MATLAB sort function. Test your function by using it in
the Command Window to rearrange the elements of the following
vector: [-2, 8, 29, 0, 3, -17, -1, 54, 15, -10, 32].
Easter eggs (fun/hidden bonus)
 Several easter eggs exist in MATLAB.
 Please typing in "spy" , "why“, "penny",
"toilet", "image", and "life".

You might also like