0% found this document useful (0 votes)
79 views152 pages

MATLAB Chapter 1: An Overview of MATLAB

The document provides an overview of MATLAB, explaining that it is a numerical computing environment and programming language used for matrix manipulations, plotting of functions and data, implementation of algorithms, creation of user interfaces, and interfacing with programs written in other languages. It introduces some basic MATLAB concepts such as variables, arrays, built-in functions, complex numbers, and formatting commands. The document also describes how to manage the MATLAB workspace and command history.

Uploaded by

HusseinHazime
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views152 pages

MATLAB Chapter 1: An Overview of MATLAB

The document provides an overview of MATLAB, explaining that it is a numerical computing environment and programming language used for matrix manipulations, plotting of functions and data, implementation of algorithms, creation of user interfaces, and interfacing with programs written in other languages. It introduces some basic MATLAB concepts such as variables, arrays, built-in functions, complex numbers, and formatting commands. The document also describes how to manage the MATLAB workspace and command history.

Uploaded by

HusseinHazime
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 152

MATLAB Chapter 1

An Overview of MATLAB
MATLAB

MATLAB = MATrix LABoratory

 Extremely useful mathematical software


 Can be used as an advanced calculator/graphing tool
 Can be used as a powerful programming language

2
Why use MATLAB?

 MATLAB is an easy introduction language for


programming.

 MATLAB provides a “quick-and-easy” development


environment.

 MATLAB is very useful in many engineering contexts.

 MATLAB is used in industry.

3
Programming with MATLAB

 Programming is a TRANSFERABLE SKILL


 Programming concepts are common for all languages
 Syntax may change, but usually similar

 MATLAB is and INDEPENDENT PLATFORM


 Can write software once for many Operating Systems

 MATLAB can be linked to other software


 C/C++, Fortran, etc…

4
MATLAB in University/Industry

 MATLAB can be used to solve applied mathematical


models.

 MATLAB can be used to develop and analyze new


models. For example, in like
 Structural Analysis
 Electric Circuits Analysis
 Communication Systems
 Systems and Control

5
1.1 Starting Matlab

This window
shows the current
directory or the This is the
workspace command
This window window
shows the
command
history

6
1.1 Entering Commands and Expressions

 You can enter expressions at the command line and


evaluate them right away.

previous >> 8/10


command
ans =
0.8000
next
command
>>

The >> symbols indicates where commands are typed.


7
1.1 Entering Commands and Expressions

 Matlab uses scientific notations for large numbers.

 Just like Excel, we use the notation “e” to represent


exponentiation
 Example: 5.316×102
In Matlab: 5.316e+02

8
1.1 Variable

 Matlab has assigned the answer to a variable called


ans, which contains the most recent answer.

 ans can be used for further calculations.


 Example: >> 5*ans

ans =
4
>>
9
1.1 Variable

 You can also use variables >> temp1_a=50


of your own. temp1_a =
 Variables has to start with a
letter, can include numbers 50
and underscores: >>temp1_a/2
 For example: temp1_a can
be used as a variable. ans =
 Note: Matlab is case- 25
sensitive. >>
10
1.1 Built-in functions

 Matlab has 100s of built-in functions. >> sqrt(9)


 Example: the square root
function ans =
 In Matlab: sqrt(x), where x can
e.g. be a number, a variable or
3
an array…
>>
 Attention: It is possible to assign
See Table 1.3-1 and Appendix A
variable names with built-in
for a list of commonly used
functions/ variables names, but it is functions
not practical to do so.

11
Appendix A

 Appendix A presents a quick guide to commands and


functions used in the textbook.

 Many tables seen in the chapters are found again in


Appendix A.

12
1.1 Arithmetic operators

Operator MATLAB Example


 See Table 1.1-1 for more
information on operators.  + 5 + 4 = 9

 - 5 - 4 = 1
 See Table 1.1-2 for the
order of precedence.  * 5 * 4 = 20

 / 5 / 4 = 1.25

ab a^b 5^4 = 625

13
1.1 The Assignment Operator

 The = sign in Matlab is called the >> x=2


assignment operator. When you
type x=2, you are assigning the x =
value 2 to the variable x.
2

 In Matlab, it is however also >> x=x+2


possible to type x=x+2. This tells
Matlab to add the value 2 to the x =
current value of x, replacing the old
4
value of x and making the most
recent value of x=4.
>>

14
Example 1.1-1

 Volume of a circular
>> r=8;
cylinder:
>> h=15;
 Given radius r and >> V=pi*r^2*h;
height h and volume >> V=V+0.2*V;
V=pi*r^2*h of a cylinder, >> r=sqrt(V/(pi*h))
find radius of another
cylinder with the same r =
height and a volume
8.7636
20% greater.
 Notice: Using a semicolon >>
“;” after a command
removes screen printing. Notice that new values
of r and V replace old values
15
1.1 Managing the Work Session

 Variable names must begin with a letter and must


contain less than 32 characters.
 It is advisable to assign logical names to your variables:
e.g. speed, distance, temp_1, etc…
 Remember: Matlab is case-sensitive.

 Table 1.1-3 summarizes some commands and special


symbols for managing the work session: e.g. clc,
clear, quit, exist(‘name’), who, etc…

16
1.1 Managing the Work Session

 Instead of retyping the entire line, press the up-arrow () and the
down-arrow () to recall previous commands you typed in your
session.

 Notice that commands are stored in the Command History Window.


Double-clicking on a command in the command history window
recalls this command automatically.

 If you go to Edit/Clear Command History, all previous commands


will be deleted (and can’t be recalled anymore)

17
1.1 Managing the Work Session

 Pressing tab while typing  auto


completes commands.
 Some useful commands:
 clear: clears all previous variables
 clear var1 var2: clears variables
var1 and var2.
 exist(‘var1’): checks if variable
var1 exist (returns 1 or 0)
 clc: clears screen
 who: lists all variables
 who var1 var2: lists var1 and var2
if they exist.
 who A*: lists all variables that start with
the letter A.
 whos: lists the variable names and their
sizes
18
1.1 Predefined Constants
 Table 1.1-4: Special variables and constants
 Examples
 pi represents the number  
 Inf represents Infinity (number/0)
 NaN indicates an undefined numerical result (0/0)
 i,j represent the square root of –1,
necessary for complex numbers. Example
5+2i or 5+2j can be typed directly in Matlab.
 etc…

19
1.1 Complex Number Operation
>> s=3+7i;
>> w=5-9i;
>> w+s
 Matlab handles complex ans =
number algebra
8.0000 - 2.0000i
automatically. For example
>> w*s
c1= 1-2i (or 1-2*i).
ans =
 Caution: You CANNOT 78.0000 + 8.0000i
type 1-i2. >> w/s

ans =

-0.8276 - 1.0690i

>> (-3+7i)*(-3-7i)

ans =

58

20 >>
1.1 Formatting Commands

 Table 1.1-5: Numeric display formats


Examples:
 format short Four decimal digits
 format long 16 digits
 format short e Five digits plus exponent
 etc…

21
1.2 Menus and the Toolbar

 Check out the Menu bar and the Shortcut Toolbar in


Matlab
 Notice: Depending which Window is selected, the Menu
Bar changes accordingly.

22
1.3 Computing with Matlab Arrays

>> x=[0 1 2 3]
 Matlab can handle collections of
numbers, called arrays, as if they x =
were a single variable.
0 1 2 3

>> x=[0,1,2,3]
 Possible way to define an array
elements must be separated by a x =
comma or space (or both). 0 1 2 3

>>
 Order of elements is important.

23
1.3 Arrays
>> x=2:8

 It is not necessary to x =

type all the numbers in 2 3 4 5


6 7 8
the array if they are
regularly spaced. >> x=2:1:8

 The use of colon x =


indicates the spacing 2 3 4 5
and the length of the 6 7 8
array (only for real >> x=2:2:8
scalars).
x =
 By default, the spacing
2 4 6 8
 is 1.
>>

24
1.3 Array Operations
 Simple array operations can be done like matrix operations:
>> x=0:3;
>> y=sin(x)

y =
0 0.8415 0.9093 0.1411 Note: Matlab
>> z=cos(x) trigonometric
functions use
z =
radian measure.
Using degrees can
1.0000 0.5403 -0.4161 -0.9900 be done by hand:
>> y+z cosine 60° is
cos(60*pi/180)
ans =

1.0000 1.3818 0.4932 -0.8489

>>
25
1.3 Arrays

 Array operations are done in Matlab mathematically correct. For


example, A*B results in a correct matrix multiplication, and not in an
element by element multiplication.

 However, Matlab enables also element by element operations. A dot


“.” before the operator must be used for that purpose.
 Examples:
 A.*B results in an element by element multiplication.
 Also, exponentiation and division operators must have a dot:
A.^2 results in squaring the single elements of the array A, and
A./B results in an element by element division.

26
1.3 Arrays
>> x=0:3;
>> y=sin(x)

y =

0 0.8415 0.9093 0.1411

>> z=cos(x)

z =

1.0000 0.5403 -0.4161 -0.9900

>> y.^2+z.^2

ans =

1.0000 1.0000 1.0000 1.0000

>> y^2+z^2
??? Error using ==> mpower
Matrix must be square.

>>
27
1.3 Array Index

 You can see several values of an array by typing the


location or index of the value as follows:
>> x=-5:0.1:10;
>> x=-5:0.1:10;
>> x(151)
>> x(1)
ans =
ans =
10
-5
>> x(200)
??? Index exceeds matrix dimensions.
>> x(7)
>> x(0)
ans =
??? Subscript indices must either be real positive
integers or logicals.
-4.4000
>>
>>

28
1.3 Array Index

 To know how many elements a vector has, use the


length command

>> x=0:0.1:10; Note: for the index, we can use:


>> length(x) x(1), x(2), …
or
ans = x(1,1), x(1,2), …

101 length actually computes either the


number of elements of an array if the array
>> is a line vector, or the largest value of m or n
if the array is an mxn matrix.

29
1.3 mxn Arrays
 One way to write an mxn array in Matlab, use semicolons “;” to
separate between rows: >> A=[1;3;5]
>> A=[1 2 4 5;5 -3 9 2]
A =
A =
1
1 2 4 5 3
5 -3 9 2 5

>> size(A) Here use >> A=[1+i;3i;2]


size(A)to
ans = A =
know array
2 4 dimension 1.0000 + 1.0000i
Index: mxn 0 + 3.0000i
2.0000
A= A(1,1) A(1,2)… >> length(A)
A(2,1) A(2,2)… ans = >> A=[1,2,4;2,4,5]

4 A =

1 2 4
30 2 4 5
1.3 Polynomial Roots

 A polynomial can be described in Matlab >> roots([1,-7,40,-34])


with an array whose elements are the
ans =
polynomial coefficients, starting with the
coefficient with the highest power of x. 3.0000 + 5.0000i
 Use the roots command to solve for 3.0000 - 5.0000i
roots 1.0000

 Example: f(x) = 1x3  7x2 + 40x  34 = 0 >> poly([1,3-5i,3+5i])

ans =
 To find the polynomial from its roots, use
the function poly([root1,root2,…]) 1 -7 40 -34

>>

31
1.3 Working with files

 There are several types of files that enables you to save


programs, data, and session results. Most importantly
are:
 MAT-files
 M-files

32
1.3 MAT-files

 If you want to stop using Matlab but continue the session


at a later time, you must use the save and load
commands.

 Typing save causes Matlab to save the workspace


variables in a file called matlab.mat.

 Typing load at a later time loads the variables that were


saved in the file matlab.mat.

33
1.3 MAT-files
 You can define your own MAT-file by using
 save filename and load filename
 The variables are then stored in the file filename.mat

 You can save only certain variables by using


 Save filename var1 var2

 Caution: If variables already exist in the workspace, they


are overwritten with the values of the variables from the
file filename.mat or matlab.mat.

34
1.3 M-files

 To save a sequence of commands


for later use, commands can be
written in a script file called M-file.
 Go to File/New/M-file
 A text editor/debugger will open
where you can write your
commands just like in the
command window.

 Note: You can use any other text


editor to write your M-file, but the
Matlab editor has more practical
options.

35
1.3 Directories and Search Path

 It is important to know the location of the files you use


within Matlab. To open the files easily in Matlab, they
should be saved in a directory within Matlab’s “search
path”.
 To know if the directory is in the search path, type path
 To add or remove a directory from the search path, type
pathtool. This will take you to another path-setting
dialog-box

36
1.3 Directories and Search Path

 If an M-file is saved in a directory within the search path


then loading this M-file can be done by directly typing its
name in the command window: filename

 To open an M-file, you can type open filename, or go


to File/Open and choose it.

37
1.3 Directories and Search Path

 Table 1.3-2 System, directory, and file commands


Examples
 dir lists all files in the current directory
 pwd displays the current directory
 pathtool starts the set path tool
 what lists all Matlab specific files (like MAT-
files) in the current directory
 etc…

38
1.3 Plotting with Matlab

 Matlab contains many powerful functions for easily


creating plots of different types, such as rectilinear,
logarithmic, surface, and contour plots.

 Steps to make a simple plot


1. Define x-axes range (e.g. x=[0:0.1:10])
2. Define y=f(x) (e.g. y=sin(x))
3. Type plot(x,y)

>> x=0:0.1:10;
>> y=sin(x);
>> plot(x,y)
39
1.3 Plotting with Matlab

 The plot appears


on the screen in a
graphical window,
named
Figure1.fig,
which is a Matlab
file.

40
1.3 Plotting with Matlab

 This Figure1.fig
file can be saved
under a different
name and format
for usage within
other programs
like MS Word.

41
1.3 Plotting with Matlab

 Table 1.3-3 Some Matlab plotting commands


Examples:
 grid puts grid lines on the plot
 title(‘text’) puts text in a title at the top of
the plot
 xlabel(‘text’) adds a text label to the x-axis
 ylabel(‘text’) adds a text label to the y-axix
 etc…

 Example:
plot(x,y),xlabel(‘time(s)’),ylabel(‘voltage(V)’),title(‘V vs. t’)

42
1.3 Plotting with Matlab

 You can create multiple plots by including another set of values in the plot:
plot(x,y1,x,y2,x,y3)

 The function [x,y]=ginput(n), gets n points and returns the x and y


coordinates in the vectors x and y which have a length n. For example,
plot(x,y),[x,y]=ginput(2)

 Typing plot(x,y,’+’) will return a scatter plot without a line, where the
data points are represented by a “+” sign.

 Typing plot(x,y,’+-’) will return a scatter plot with data points


represented with + and connected by a line. (or plot(x,y,x,y,’+’))

 loglog(x,y), semilogx(x,y), and semilogy(x,y) creates


logarithmic plots. (see Appendix A for more special plot functions)
43
1.3 Linear Algebraic Equations

 We can solve simultaneous linear equation in Matlab


very easily using matrices. For example solving the
linear equation system:
6x + 12y + 4z = 70
7x – 2y + 3z = 5
2x + 8y – 9z = 64
>> A=[6,12,4;7,-2,3;2,8,-9];
>> B=[70;5;64];
>> Solution=A\B

Solution =

3
5
44 -2
1.3 Statistics

 Matlab has number of useful statistical functions (not all


built-in) like mean, median, mode, std, var, min,
max…that work on vectors.
>> x=0:10; >> x=0:10;
>> min(x) >> mean(x)

ans = ans =

0 5

>> max(x) >> std(x)

ans = ans =

10 3.3166

45
1.4 Script Files and Editor/Debugger
Effective Use ofScript Files

 % is used to write
comments (will not be
compiled). Text color in M-
file editor after % is green.
 Semicolons “;” can be
used as usual.
 Filenames can be typed
directly in the command
window, if their directory is
in the search path.
 Writing type filename
in the command editor
displays the file in the See page 31-32 in TB on what to
command window. remember when using script files
46
1.4 Debugging Script files

 Debugging means finding program “Bugs” or errors:


 Syntax errors: e.g. missing parenthesis or comma, …
 Runtime errors: e.g. dividing by zero,…
 Syntax errors are usually found by Matlab, and program will not run
until error is corrected. Runtime errors are however harder to locate.

 To run an M-file, file has first to be saved.


 To run and save automatically from editor:
 Press F5
 Go to Debug/Save and Run

 If due to a certain mistake, you would like to stop the program from
running, press
Ctrl+c
47
1.4 Controlling Input and Output

 Table 1.4-2 Input/Output commands


Examples
 disp(A) displays content, but not
the name of an array A.
 disp(‘text’) displays text string
enclosed in quotes.
 x = input(‘text’) displays text in quotes
imp
and waits for user to
 etc… input value for x.
 Example: T4.1-2
48
1.5 Matlab Help System

 In the command window, you can type:


 help funcname
 lookfor funcname
 doc funcname
 etc…

 Go to the Help menu item in the Menu Bar.

 Table 1.5-1 Matlab Help functions

49
1.6 Programming in Matlab

 Table 1.6-1 Relational Operators


< less than
 <= less than or equal
> greater than
 >= greater than or equal
 == Equal to
 ~= Not equal to

 The result is either 1 (for true) or 0 (for false).


 This result can be used as a logical variable.
50
1.6 Relational Operators

 With the relational operators, we can directly compare


variables: >> x=1;
>> y=2;
>> x==y

ans =

>> x='m';
>> y='m';
>> x==y

ans =

>>
51
1.6 Relational Operators
 We can compare arrays on an element-by-element basis:
>> x=[6,3,9]; >> A=[1,2,3;4,5,6];
>> y=[14,2,9]; >> B=[1,2,3;5,5,6];
>> z=(x<y) >> A==B

z = ans =

1 0 0 1 1 1
0 1 1
>> z=(x==y)
>> C=A~=B
z =
C =
0 0 1
0 0 0
>> z=x==y %another way to write it 1 0 0

z = >>

0 0 1

52
1.6 Relational Operators

 Relational operators can also be used within arguments. For example


writing x(x<y) returns the elements of the array x which are less than y.

>> x=[2,3,1];
>> y=[1,2,5];
>> x(x>y)

ans =

2 3

>>

53
1.6 Relational Operators

 The find function: finds the NONZERO elements in an


array.
 Returns the indices not the element values
 This is useful when writing decision-making programs.

>> x=[1,2,9,0,3,0,3];
>> y=find(x)

y =
Only
1 2 3 5 7 indices are
returned.

54
1.6 Conditional Statements IF-ELSE-ELSEIF

 Conditional statements allow us to write programs that make


decisions.
 We should pay attention to the order of expressions.
 We can write nested IFs by using the elseif statement.
 Syntax If expression
commands
elseif expression
If expression commands
commands elseif expression
else commands
commands …
end else
commands
55 end
1.6 Conditional Statements

1.6 Example 1.6-1

 Example: Write a program in Matlab to compute y(x) for


15 4x 10 if x  9

y(x)   10x 10 if 0  x  9
 10
 if x  0

56
1.6 Conditional Statements

 Example: Write a program in Matlab to compute y(x) for


15 4x 10 if x  9

y(x)   10x 10 if 0  x  9
 10
 if x  0
%ifexample: Evaluation of y(x)
x=input('Enter the value of x: '); >> ifexample
if x>=9 Enter the value of x: 2
y=15*sqrt(4*x)+10
elseif x>=0 y =
y=10*x+10
else 30
y=10
end >>

57
1.6 Loops

 A loop is a structure for repeating a calculation an


number of times. There are two types of loops:

 for loop: used when the number of executions is


known ahead of time.

 while loop: used when the looping process must


terminate when a specified condition is satisfied, and
thus the number of executions is not known in
advance.

58
1.6 Loops

 Syntax:

for expression while expression


commands commands
end end

Note: commands are executed in order

59
1.6 Programming in Matlab

 Table 1.6-2 Some Matlab programming statements


Examples:
 else Delineates an alternate block of
commands.
 if Executes commands conditionally
 etc…

60
1.6 Loops

 for-loop example:
%first method %second method
m=0; m=1;
x(1)=10; x(m)=10;
for k=[2:3:11] for k=2:3:11
m=m+1; x(m+1)=x(m)+k^2;
x(m+1)=x(m)+k^2; m=m+1;
end end
x x

>> x

x =

10 14 39 103 224
61
1.6 Loops

 while-loop example:

%while loop example >> y


clear
x=5;
k=0; y =
while x<25
k=k+1; 15 27 51
y(k)=3*x;
x=2*x-1; >>
end
y

62
1.6 Example 1.6-2

15 4x 10 if x  9

y(x)   10x 10 if 0  x  9
 10
 if x  0

Plot y(x) vs x for -5x30

63
1.6 Example 1.6-2
15 4x 10 if x  9

y(x)   10x 10 if 0  x  9
 10
Plot y(x) vs x for -5x30  if x  0
%Example 1.6-2: using for loop %Example 1.6-2: using while loop
clear clear
dx=35/300; dx=35/300;
x=[-5:dx:30]; x=[-5:dx:30];
k=1;
for k=1:length(x)
if x(k)>=9 while k<=length(x)
y(k)=15*sqrt(4*x(k))+10;
elseif x(k)>=0 if x(k)<0
y(k)=10*x(k)+10; y(k)=10;
else elseif (x(k)>=0)&&(x(k)<9)
y(k)=10; y(k)=10*x(k)+10;
end else
end y(k)=15*sqrt(4*x(k))+10;
plot(x,y),xlabel('x'),ylabel('y') end

k=k+1;
end
64 plot(x,y),xlabel('x'),ylabel('y')
1.6 Example 1.6-3
 Compute sum of the 1st 15 terms in the series 5k2-2k, for
k=1,2,…,15

65
1.6 Example 1.6-3
 Compute sum of the 1st 15 terms in the series 5k2-2k, for
k=1,2,…,15
%Example 1.6-3: using for loop (TB) %Example 1.6-3: using the sum function
clear clear
total=0; k=1:15;
for k=1:15; y=5*(k.^2)-2*k;
total = 5*(k^2)-2*k + total;
end total2=sum(y)
total %it sums over all ys, i.e y(1)to y(15)

%Example 1.6-3: using for loop %Example 1.6-3: using while loop
clear clear
total1=0; total3=0;
k=1:15; k=1:15;
y=5*(k.^2)-2*k; y=5*(k.^2)-2*k;

for h=1:15 h=1;


total1 = total1 + y(h); while h<=15
end total3 = total3 + y(h);
total1 h=h+1;
end
66 total3
1.6 Example 1.6-4
 Find how many terms it’s required for the sum of the series 5k2-2k,
for k=1,2,…to exceed 10000. What’s the sum of these many terms?

67
1.6 Example 1.6-4
 Find how many terms it’s required for the sum of the series 5k2-2k,
for k=1,2,…to exceed 10000. What’s the sum of these many terms?

k=0; >> Example_1_6_4


total=0; the number of terms is:
while total<=1e4 18
k=k+1;
total=5*k^2-2*k+total; the sum is:
end 10203
disp('the number of terms is:')
disp(k)
disp('the sum is:')
disp(total)

68
1.6 Example 1.6-5
 Determine how long it will take to accumulate at least $10000 in a
bank account if you deposit $500 initially and 500$ at the end of
each year, if the account pays 5% annual interest

69
1.6 Example 1.6-5
 Determine how long it will take to accumulate at least $10000 in a
bank account if you deposit $500 initially and 500$ at the end of
each year, if the account pays 5% annual interest

%Example 6.1-5
clear
>> Example_1_6_5
amount=500;
years=0;
amount =
while amount<1e4
1.0789e+004
amount=amount*1.05+500;
%or amount=amount+500+amount*0.05
years=years+1;
end years =
amount
years 14

70
MATLAB Chapter 2
Numeric and Matrix Arrays
2.1 Arrays

 x=[1 2 4] 1x3 line vector (1 row and 3 columns)


 x=[1;2;4] 3x1 column vector (3 rows and 1 column )
 length(x) gives the number of elements in vector x

 A=[1 2;4 2;7 -1] 3x2 matrix (3 rows and 2 columns )


 size(A) gives the number of rows and columns in matrix A

 B=[1 2 3];C=[4 5 6];


 D1=[B C]; 1x6 line vector D1=[1 2 3 4 5 6]
 D2=[B;C]; 2x3 matrix D2=[1 2 3;4 5 6]

2
2.1 Arrays

 x=(0:2:10); returns vector x=[0 2 4 6 8 10];


 Initial value 0, step 2 and final value 10

 z=(10:-2:0); returns vector z=[10 8 6 4 2 0];

 By default if omitted step=1

3
2.1 Matrix Transpose

 A=[1 2 3 4; 5 6 7 8; 9 10 11 12]; 3x4 matrix

 B=A’; Matlab transpose matrix given by


 B=[1 5 9; 2 6 10; 3 7 11; 4 8 12]; 4x3 matrix

 For array with complex elements


 C=[1+1i 2 3 4; 5 6 7 8; 9 10 11 12-3i];
 D=C’; produces complex conjugate transpose
 D=[1-1i 5 9; 2 6 10; 3 7 11; 4 8 12+3i];

 E=C.’; produces only the transpose matrix


 E=[1+1i 5 9; 2 6 10; 3 7 11; 4 8 12-3i];

4
2.2 Array Addressing

 v=(0:0.2:1); v(5)=0.8 element of vector v with index 5

 A=[1 2 3 4; 5 6 7 8; 9 10 11 12];

A(2,3)=7 element of matrix A at row 2 and column 3

A(1:2:end,:) submatrix of matrix A with odd rows

A(:,2:2:end) submatrix of matrix A with even columns

A(1:3,3:4) submatrix of matrix A with rows one to three


and columns three to four

5
2.2 Array Addressing

 Examples:
>> A
 v(2:5) represents elements
2 to 5 of vector v A =

1 2 3 4
 A(:,3) denotes all elements 5 6 7 8
in column 3 of A 9 10 11 12

>> B=A(2:3,1:3)
 A(:,2:4) denotes all elements
B =
in columns 2 to 5 of matrix A
5 6 7
9 10 11
 A(2:3,1:3) denotes submatrix
of A with elements in rows 2
to 3 and columns 1 to 3
6
2.2 Other array manipulations

A =
6 9 4 D =
1 5 7
3 8 5
>>B=A(:,end:-1:1) 2 -6 9

B = >> E=D([2,2,2],:)

4 9 6 E =
7 5 1
2 -6 9
2 -6 9
%read matrix A from right to left 2 -6 9

%repeats row 2 in D three times

14
2.3 Useful Array Functions

 Array functions from Appendix A


Examples:

 max(A)  returns the largest element in A if A is a vector.


Returns a row vector containing the largest elements
in each column of A if A is a matrix…
 [x,k]=max(A)  similar to max(A) but stores the maximum values in
the row vector x and their indices in the row vector
k.
 sum(A)  sums the elements in each column of the array A and
returns a row vector containing the sums.
 sort(A)  sorts each column of the array A in ascending order
and returns an array the same size as A.

8
2.3 Element by Element Operations

 Table 2.3-1 Element-by-element (e-b-e) operations


Examples
+ Scalar-array addition A+b
+ Array addition A+B
 .* Array e-b-e multiplication A.*B
 ./ Array e-b-e division A./B

 Remember, A*B is Matrix multiplication and not e-b-e


multiplication (focus on DIMENSIONS issue)
9
2.3 Matrix Operations
Matrix multiplication A*B is done directly in Matlab.

Dimension issue:
Number of A columns SHOULD be equal to number of B rows!!!!!!

>> A=[6,-2 ; 10,3; 4,7];


>> B=[9;-5];
>> A*B

ans =

64
75
1

10 20
2.3 Matrix Operations
 Matrix multiplication is not commutative:
AB  BA
Example:
A=[1 2 3; 4 5 6; 7 8 9];
B=[10 11 12; 13 14 15; 16 17 18];
Result: A*B=[84 90 96; 201 216 231; 318 342 366];
B*A=[138 171 204; 174 216 258; 210 261 312];

 Matrix multiplication is associative and distributive:


A*(B+C)=A*B+A*C
A*(B*C)=(A*B)*C
Example:
Same A and B matrixes
C=[9 8 7; 6 5 4; 3 2 1];

11
2.3 Special Matrices

 Special matrices (Appendix A) Examples:


 eye(n) creates an nxn identity matrix
 eye(size(A)) creates an identity matrix the
same size as the matrix A

 ones(n) creates an nxn matrix of ones


 ones(m,n) creates an mxn array of ones
 zeros(n) creates an nxn matrix of zeros

12
2.3 Special Matrices

 When A is a square matrix, then raising the matrix to a power is


equivalent to repeatedly multiplying the matrix by itself:

 A^2 = A*A
 A^3 =A*A*A
 Give Example with A=[1 2 3; 4 5 6; 7 8 9];

13
2.4 Polynomial Operations (Appendix A)

 A polynomial can be described in Matlab >> roots([1 -7 40 -34])


with an array whose elements are the
ans =
polynomial coefficients, starting with
coefficient with the highest power of x 3.0000 + 5.0000i
3.0000 - 5.0000i
 Use the roots command to solve for 1.0000
roots >> poly([1 3-5i 3+5i])

ans =
 Example: f(x) = 1x3  7x2 + 40x  34 = 0
1 -7 40 -34
 To find the polynomial from its roots, use >>
the function poly([root1,root2,…])

14
2.4 Polynomial Addition and Subtraction

 To add polynomials, add the arrays that describe their


coefficients.
 If polynomials are of different degrees, add zeros to
the coefficient array of the lower-degree polynomial.
>> f=[9 -5 3 7];
>> g=[0 6 -1 2];
>> f+g
Example:
 f(x) = 9x3 – 5x2 +3x +7 ans =
 g(x) = 6x2 – x + 2
 f(x) + g(x) = 9x3 + x2 +2x +9 9 1 2 9

15
2.4 Polynomial Multiplication and Division
 The product of the polynomials f(x) and g(x) is
f(x)g(x) = (9x3 – 5x2 +3x +7)(6x2 – x + 2)
= 54x5 – 39x4 + 41x3 + 29x2 – x + 14

 Dividing f(x) by g(x) gives


f(x) 9x – 5x  3x  7
3 2
  1.5x  0.5833
g(x) 6x – x  2
2

with a remainder of -0.5833x+8.1667.

 In Matlab, polynomial multiplication and division can be done using


the functions:
conv(a,b)
[q,r]=deconv(num,den)
16
2.4 Polynomial Multiplication and Division

>> f=[9,-5,3,7];
>> g=[6,-1,2];
Notice: Unlike >> product=conv(f,g)
during addition or product =
subtraction,
polynomials don’t 54 -39 41 29 -1 14
need to have the
>> [quotient,remainder]=deconv(f,g)
same degree
during quotient =
multiplication or
division (i.e. no 1.5000 -0.5833
need to add
zeros) remainder =

0 0 -0.5833 8.1667
17
2.4 Plotting Polynomials

 The polyval(a,x) function


evaluates a polynomial at specified
values of its independent variable x,
which can be a matrix or a vector.
The polynomial’s coefficient array is
a. The result is the same size as x.
Example:
 f(x) = 9x3 – 5x2 +3x +7
 x=0:2:10

>> a=[9,-5,3,7];
>> x=0:2:10;
>> f=polyval(a,x);
>> plot(x,f),xlabel('x'),ylabel('f(x)'),grid

18
2.6 Polynomial functions

 Polynomial functions from Appendix A

 conv(a,b)  computes the product of two


polynomials
 computes the value of polynomial a in
 polyval(a,x)
specific value or vector x

19
Test Your Understanding

20
MATLAB Chapter 3
Functions and Files

1
3.1 Elementary Mathematical Functions
 Appendix A: Common mathematical functions

 exp(x) Exponential 𝒆𝒙
 log(x) Natural logarithm 𝒍𝒏𝒙
 log10(x) Base 10 logarithm 𝒍𝒐𝒈 𝒙
 cos(x) Trigonometric cosine in radians
 cosd(x) Trigonometric cosine in degrees
 atan(x) Inverse tangent in radians
 acosh(x) Inverse hyperbolic cosine

2
3.2 User Defined Functions
Function File

 Function files are useful when you need to repeat a set


of commands several times.

 Function Files are building blocks for larger programs.

 The first line in a function file must begin with a function


definition line that has a list of inputs and outputs. This
line distinguishes a function M-file from a script M-file.

3
3.2 User Defined Functions
Function File

 The first line Syntax is as follows:

function[output variables] = function_name(input variables)

 The function_name should be the same as the file name in which it is


saved (with the .m extention), i.e. if you name a function drop, it should be
saved in the file: drop.m

 The function can be used by typing its name (e.g. drop) at the command
line. The word function in the function definition line must be lowercase.

 Before choosing a function name, use the exist function to see if another
Matlab built-in function has the same name.

4
3.2 User Defined Functions
Function File

 Example1: Write a function to calculate the volume of a cube,


given its height, width and length.

volume.m
function vol=volume(height,width,length)
vol=height*width*length;

In Command Window

>> V=volume(2,2,3)
no need to use
brackets when having V =
only one variable
12

5
3.2 User defined Function file
Example:
Consider the following parametric equations:

𝒙 𝜽 = 𝒓 𝜽 − 𝒔𝒊𝒏(𝜽)
𝒚 𝜽 = 𝒓 𝟏 − 𝒄𝒐𝒔(𝜽)

Create a function in Matlab called cycloid.m which:


• accepts as inputs angle 𝜽 and radius 𝒓
• Calculates outputs 𝒙 and 𝒚 defined by the equations above.

The first declaration line of the function should look like:


𝒇𝒖𝒏𝒄𝒕𝒊𝒐𝒏 𝒙, 𝒚 = 𝒄𝒚𝒄𝒍𝒐𝒊𝒅(𝜽, 𝒓)

Plot the cycloid graph in the command window for the following inputs:
• angle 𝜽 = 𝟎: 𝟎. 𝟎𝟏 ∗ 𝒑𝒊: 𝟔 ∗ 𝝅
• radius 𝐫 = 𝟓.
6
3.2 User defined Function file
function [x,y] = cycloid(theta,r)
%This function calculates the Cartesian coordinates
%x and y from Polar coordinates theta and r for
%cycloid figure

x=r*(theta-sin(theta));
y=r*(1-cos(theta));
end

In Command Window
>> theta=(0:0.01:6*pi);
>> r=5;
>> [x,y]=cycloid(theta,r);
>> plot(x,y), grid
>> xlabel('Abscisse'),ylabel('Ordinate')
>> title('Graph of cycloid')

7
3.2 User defined Function file

8
3.3 Matlab built-in functions
 The fzero Matlab function can be used to find the zero of a
function of a single variable 𝒙.
 First, 𝒚 = 𝒇 𝒙 function needs to be written in a M-file function.
 Second, in the command window, the function
fzero(‘function_name’,x0) can be called to find the zero
crossing closest to the initial guess 𝒙𝟎.
function y = f_1(x)
y=x+2*exp(-x)-3;
In Command Window
>> fzero('f_1',-1) >> fzero('f_1',3)

ans = ans =

-0.5831 2.8887

9
3.3 Matlab built-in functions

 If the function has more


than one zero, it is
better to plot it first, as
the fzero function
only returns one zero
closest to 𝒙𝟎 .

 Note: If a function only


touches the zero axis
(tangent), no solution is
returned.

y=x+2*exp(-x)-3

10
3.3 Matlab built-in functions

 Other ways to write the fzero function:

 [x,value]=fzero(‘function_name’,x0)
returns the value of the function at the solution x.

 [x,value,sol]=fzero(‘function_name’,x0)
returns the value of the function at the solution 𝒙 and returns
if the function found a zero or not.
If the variable 𝒔𝒐𝒍 is positive, then there is a solution, elseif it is
negative, then the function fzero didn’t find a zero.

11
3.3 Matlab built-in functions

Minimizing a Function of one variable


 The 𝒇𝒎𝒊𝒏𝒃𝒏𝒅 ′𝒇𝒖𝒏𝒄𝒕𝒊𝒐𝒏_𝒏𝒂𝒎𝒆′, 𝒙𝟏 , 𝒙𝟐 finds the minimum of a
function in the interval 𝒙𝟏 , 𝒙𝟐 .
 For example consider the same function:
 𝒚 = 𝒇𝟏 𝒙 = 𝒙 + 𝟐 ∗ 𝒆𝒙𝒑 −𝒙 − 𝟑
 The function 𝒚 = 𝒇𝟏 𝒙 has a
minimum at 𝒙 = 𝟎. 𝟔𝟗𝟑𝟐.

12
3.3 Matlab built-in functions

Minimizing a Function of one variable


 The abscissa giving minimum for function 𝒇𝟏 can be found by:

function y = f_1(x)
y=x+2*exp(-x)-3;

In Command Window
>> fminbnd('f_1',0,1)

ans =

0.6932

13
3.3 Matlab built-in functions

Minimizing a Function of one variable


 Other ways to write the 𝒇𝒎𝒊𝒏𝒃𝒏𝒅 function:
 [x,value]=fminbnd(‘function_name’,𝒙𝟏 , 𝒙𝟐 ) returns
also the value of the function at the solution 𝒙.

[x,value,sol]=fminbnd(‘function_name’, 𝒙𝟏 , 𝒙𝟐 )
returns also if the function converged to a solution or not.

If the variable sol is 𝟏, then there is a solution, elseif it is different


than 1, the function 𝒇𝒎𝒊𝒏𝒃𝒏𝒅 did not converge to a solution.

 Note:
 To find the maximum of a function, use the fminbnd function
with the opposite of the function of interest.

14
Test Your Understanding

 Test 3.1
The equation 𝒆−𝟎.𝟐𝒙 𝒔𝒊𝒏 𝒙 + 𝟐 = 𝟎. 𝟏 has three solutions in the
interval 𝟎, 𝟏𝟎 . Find these solutions using Matlab function 𝒇𝒛𝒆𝒓𝒐.

 Test 3.2
The function 𝐲 = 𝟏 + 𝒆−𝟎.𝟐𝒙 𝒔𝒊𝒏 𝒙 + 𝟐 has two minimum points in
the interval 𝟎, 𝟏𝟎 . Find the values of 𝒙 and 𝒚 at each minimum
using Matlab function 𝒇𝒎𝒊𝒏𝒃𝒏𝒅.

15
Test 3.1 Matlab solution

 function y = f1(x)
 %Function f1 in Chapter 3 Test 3.1
 y=exp(-0.2*x).*sin(x+2)-0.1;
 end

16
Test 3.1 Matlab solution
 %Graph of the function difference
 x=(0:0.01:10);
 y=exp(-0.2*x).*sin(x+2)-0.1;
 figure; plot(x,y), grid
 xlabel('Abscissa'), ylabel('Amplitude')
 title('Graph of the function difference of the equation in Test
3.1')

 %On the graph, we see 3 crossings of the function to zero
horizontal axis
 %First solution in the interval [0,2] close to initial guess
x01=1
 x1=fzero('f1',1); disp('Solution x1= '), disp(x1)

 %Second solution in the interval [4,5] close to initial guess
x02=4.5
 x2=fzero('f1',4.5); disp('Solution x2= '), disp(x2)

 %Third solution in the interval [6,8] close to initial guess
x03=7
 x3=fzero('f1',7); disp('Solution x3= '), disp(x3)

17
Test 3.2 Matlab solution

 function y = f2(x)
 %Function f2 in Chapter 3 Test 3.2
 y=1+exp(-0.2*x).*sin(x+2);
 end

18
Test 3.2 Matlab solution
 %Graph of the function
 x=(0:0.01:10);
 y=1+exp(-0.2*x).*sin(x+2);
 figure; plot(x,y), grid
 xlabel('Abscissa'), ylabel('Amplitude')
 title('Graph of the function in Test 3.2')

 %On the graph, we see 2 function’s minimum in the interval [0,10]
 %First minimum in the interval [2,3]
 [x1,y1]=fminbnd('f2',2,3);
 disp('First minimum abscissa x1= '), disp(x1)
 disp('First minimum ordinate y1= '), disp(y1)

 %Second minimum in the interval [8,10]
 [x2,y2]=fminbnd('f2',8,10);
 disp('First minimum abscissa x2= '), disp(x2)
 disp('First minimum ordinate y2= '), disp(y2)

19
MATLAB Chapter 4
Advanced Plotting and Model Building
4.1 Plotting 1D functions (Appendix A)
 figure
 axis([xmin xmax ymin ymax])
 grid
 plot(x,y)
 plot(x1,y1,x2,y2) etc.
 legend(‘string1’,‘string2’) etc.
 subplot(mnp)
 loglog(x,y)
 semilogx(x,y)
 semilogy(x,y)
 polar(theta,r,’type’)
 stairs(x,y)
 stem(x,y)
4.1 Example A: Archimedes Spirals

• Use Matlab function plot to show the imaginary part versus the
real part from the complex function:
𝒛 𝒏 = 𝟎. 𝟐 + 𝟎. 𝟖𝒊 𝒏 𝟎 ≤ 𝒏 ≤ 𝟐𝟎
Choose enough points to obtain a smooth curve.

• Use Matlab function subplot to present the 2 graphs of


imaginary part versus real one and vice versa.

• Plot using Matlab function polar the equation given by:


𝒓 𝜽 = 𝒂𝜽 𝒘𝒊𝒕𝒉 𝒂 = 𝟓 𝟎 ≤ 𝜽 ≤ 𝟔𝝅
4.1 Example A: Archimedes Spirals

%Given data
n=(0:0.1:20);
theta=(0:0.01*pi:6*pi);

%Calculations
z=(0.2+0.8*1i).^n;
x=real(z); y=imag(z);

r=5*theta;

%Output Graph 1
figure; plot(x,y), grid
xlabel('Real part'), ylabel('Imaginary part')
title('Graph of imaginary part with respect to real
part for z(n)=(0.2+0.8i)^n')
4.1 Example A: Archimedes Spirals

%Output Graph 2
figure; subplot(211)
plot(x,y), grid
xlabel('Real part'), ylabel('Imaginary part')
title('Graph of Imaginary part with respect to Real
part for complex z(n)=(0.2+0.8i)^n')

subplot(212)
plot(y,x), grid
xlabel('Imaginary part'), ylabel('Real part')
title('Graph of Real part with respect to Imaginary
part for complex z(n)=(0.2+0.8i)^n')

%Output Graph 3
figure; polar(theta,r)
4.1 Example A: Archimedes Spirals
4.1 Example A: Archimedes Spirals
4.1 Example B: Sampling theorem

Plot using Matlab function stem the sampled decaying


oscillations signal 𝐬 𝒌 with sampling period 𝑻𝒔 given by:

𝒔 𝒌 = 𝟏𝟎𝒆−𝒂𝒌𝑻𝒔 𝒄𝒐𝒔 𝒃𝒌𝑻𝒔 𝟎 ≤ 𝒌 ≤ 𝟐𝟗

Where sampling means we have replaced continuous time


variable 𝒕 by samples given by:
𝒕 = 𝒌𝑻𝒔
Simulations as follows:
 𝒂 = 𝟐 & 𝒃 = 𝟐𝝅 ∗ 𝟓 𝒂𝒏𝒅 𝑭𝒔 = 𝟏𝟎 𝑯𝒛
 𝒂 = 𝟓 & 𝒃 = 𝟐𝝅 ∗ 𝟓 𝒂𝒏𝒅 𝑭𝒔 = 𝟓 𝑯𝒛
4.1 Example B: Sampling theorem

%Given data
k1=(0:1:29); k2=(0:2:29);

a=2; b=2*pi*5;
Fs1=10; Ts1=1/Fs1;
Fs2=05; Ts2=1/Fs2;
s1=10*exp(-a*k1*Ts1).*cos(b*k1*Ts1);
s2=10*exp(-a*k2*Ts1).*cos(b*k2*Ts1);

%Output Graph 4
figure; subplot(211), stem(k1,s1), grid,
xlabel('Samples k1'), ylabel('Amplitude')
title('Graph of sampled signal with Sampling period
Ts1=0.1 s = GOOD SAMPLING')
subplot(212), stem(k2,s2,'r'), grid
xlabel('Samples k2'), ylabel('Amplitude')
title('Graph of sampled signal with Sampling period
Ts2=0.2 s = BAD SAMPLING')
4.1 Example B: Sampling theorem
4.2 Data Modelling

 Data modelling is the process of fitting a typical function to a


certain set of data.

 3 Basic models:

 The Linear model:


𝒚 = 𝒂𝒙 + 𝒃
 The power model:
𝒚 = 𝒃𝒙𝒂
 The exponential model:
𝒚 = 𝒃𝒆𝒂𝒙

 Practically, this means determining model parameters 𝒂 & 𝒃


in our case from measured data saved into vectors 𝒙 and 𝒚.
4.2 Example C: Modelling of mechanical
problem Deflection versus Force
 In Matlab, a built-in application tool called Curve Fitting can be
used for advanced fitting function.

 You can found it easily under the menu APPS in recent Matlab
versions after 2015.

 We enter data vectors 𝒙 and 𝒚 in the command window in order to


have them available in the workspace.
>> force=(0:100:800);
>> deflection=[0 0.09 0.18 0.28 0.37 0.46 0.55 0.65
0.74];

 We launch Curve Fitting application tool and upload data vectors


inside of it. We can choose between several typical models
(polynomial till order 10, power, exponential and fourier etc …)
4.2 Example C: Modelling of mechanical
problem Deflection versus Force
4.3 3D and Surface plots

 The plot3(x,y,z) function can be used to plot 3D plots in Matlab.

x=[0:0.01:20];
y=exp(-0.05*x).*sin(x);
z=exp(-0.05*x).*cos(x);

plot3(x,y,z)
title('exponentially decaying siprial')
xlabel('x')
ylabel('sine')
zlabel('cosine')
4.3 3D and Surface plots

 Example:
Generate a surface plot of the function:
𝒛 = 𝒄𝒐𝒔 𝒙 ∗ 𝒔𝒊𝒏 𝒚𝟐
for -2  x  2 and -3  y  3.

x=-2:0.1:2;
y=-3:0.01:3;
[X,Y]=meshgrid(x,y);
Z=cos(X).*sin(Y.^2);
mesh(X,Y,Z)
xlabel('x'),ylabel('y'),zlabel('z')
Problems
Problems
Problems
MATLAB – Application
Signals & Systems
Outline

1. Time-Domain Representation of Signals

2. Fourier Series

3. Fourier Transform

4. Laplace Transform

2
Time-Domain Representation of Signals

Objective:

Model and generate different continuous as well as discrete time signals using MATLAB.

• Discrete time Impulse sequence:


>> k=(-5:10);
>> imp=(k==0);
>> stem(k,imp), grid

3
Time-Domain Representation of Signals

• Discrete amplitude Scaled and time Shifted Impulse sequence:


>> k=(-5:10);
>> imp2=4*(k==2);
>> stem(k,imp2), grid

4
Time-Domain Representation of Signals

• Continuous amplitude Scaled and time Shifted Step signal:


>> t=(-5:0.01:10);
>> x=4*(t>5);
>> plot(t,x), grid
>> axis([-5 10 -0.5 4.5])

5
Time-Domain Representation of Signals

• Continuous time Shifted Ramp signal:


>>t=(-5:0.01:10);
>>x=(t-1).*(t>1);
>>figure; plot(t,x), grid
>>axis([-5 10 -0.5 10])

6
Time-Domain Representation of Signals

• Continuous amplitude Scaled and time Shifted Rectangular Signal:


>> t=(-5:0.01:5);
>> x=3*rectpuls(t-1,2);
>> figure; plot(t,x), grid
>> axis([-5 5 0 3.5])

7
Time-Domain Representation of Signals

• Continuous amplitude Scaled and time Shifted Triangular signal:


>> t=(-5:0.01:5);
>> x=3*tripuls(t+1,4);
>> figure; plot(t,x), grid
>> axis([-5 5 0 3.5])

8
Time-Domain Representation of Signals
Practice Exercises:

Create the script file exercise1_time_domain_represent that returns the following:

a) Stem plot of the discrete sequence:

𝒙 𝒏 = −𝟓𝜹 𝒏 + 𝟒 + 𝜹 𝒏 + 𝟐𝜹[𝒏 − 𝟑]

b) Plot of the continuous signal:

𝒇 𝒕 = 𝒖 𝒕 + 𝒖 𝒕 − 𝟏 + 𝒖 𝒕 − 𝟐 − 𝟑𝒖 𝒕 − 𝟑 −𝟏≤𝒕≤𝟓

c) Verify graphically that the sequences 𝒙𝟏 [𝒏] and 𝒙𝟐 [𝒏] for −10 ≤ 𝑛 ≤ 10 are equal:

𝒙𝟏 𝒏 = 𝟑𝒖 −𝒏 ∗ 𝒖 𝒏 + 𝟓
𝒙𝟐 𝒏 = 𝟑 𝒖 𝒏 + 𝟓 − 𝒖 𝒏 − 𝟏

9
Outline

1. Time-Domain Representation of Signals

2. Fourier Series

3. Fourier Transform

4. Laplace Transform

10
Fourier Series (FS)

Fit Fourier Models Interactively

1. Open the Curve Fitting app by entering cftool. Alternatively, click Curve Fitting on
the Apps tab.

2. In the Curve Fitting app, select curve data (X data and Y data, or just Y data
against index). Curve Fitting app creates the default curve fit, Polynomial.

3. Change the model type from Polynomial to Fourier.

11
Fourier Series (FS)
Example 1:

>> X=(0:pi/10:10*pi);

1 term
>> Y=sin(X)+0.5+cos(2*X)+cos(5*X)+cos(7*X);

4 terms
8 terms
1
Outline

1. Time-Domain Representation of Signals

2. Fourier Series

3. Fourier Transform

4. Laplace Transform

13
Fourier Transform (FT)

Objectives:
1. Determine the Fourier transform of time functions using MATLAB function fourier.

2. Determine the inverse Fourier transform of frequency functions using MATLAB


function ifourier.

3. Analyze the effect of filtering on a noisy sine wave using MATLAB functions fft, ifft,
and fftshift.

14
Fourier Transform (FT)

Example:
Use MATLAB to find the FT of the following time function:

𝒇 𝒕 = 𝒄𝒐𝒔 𝟑𝒕

MATLAB Solution

15
Fourier Transform (FT)

Example 4:
Use MATLAB to find the inverse FT of the following frequency function:

𝑭 𝒘 =𝝅 𝜹 𝒘−𝟑 +𝜹 𝒘+𝟑

MATLAB Solution

16
Fourier Transform (FT)

Example 4:
Use MATLAB to find the inverse FT of the following frequency function:

𝑭 𝒘 =𝝅 𝜹 𝒘−𝟑 +𝜹 𝒘+𝟑

MATLAB Solution

17
Fast Fourier Transform (FFT)

Example: Low pass Filtering of a sinewave with additive white Gaussian noise

1. Generate a sinewave of amplitude A=4 and frequency 10 Hz with sampling


period 𝑇𝑠 = 0.01 𝑠 and time interval [0,1].
2. Generate noisy sinewave by adding white Gaussian noise to signal1 using
MATLAB function randn and by adjusting noise amplitude with Signal-to-
Noise-Ratio (SNR).
3. Use MATLAB functions fft and fftshift to plot the frequency content of the
corrupted sinewave.
4. Design Ideal low pass and RC first order low pass filter with cutoff frequency
for both 𝑓𝑐 = 25 𝐻𝑧. Plot on the same graph the magnitude of their
corresponding frequency responses.
5. Apply in the frequency domain the 2 filters to the corrupted sinewave.
6. Reconstruct the filtered sinewaves using inverse fast Fourier transform
MATLAB functions ifft and ifftshift .

example_filtering_sinewave_with_noise.m
18
Outline

1. Time-Domain Representation of Signals

2. Fourier Series

3. Fourier Transform

4. Laplace Transform

19
Laplace Transform (LT)

Objectives:

1. Determine the Laplace transform of time functions using MATLAB function laplace.

2. Determine the inverse Laplace transform of frequency functions using MATLAB


function ilaplace.

3. Determine the partial fraction expansion of a transfer function using MATLAB


function residue.

4. Use MATLAB function pzmap to represent the constellation of poles and zeros of a
transfer function.

20
Laplace Transform (LT)

Example:
Use MATLAB to find the LT of the following time function:
𝒇𝟏 𝒕 = 𝒆𝒙𝒑 𝒕 𝒇𝟐 𝒕 = 𝒕 𝒆𝒙𝒑 𝒕 𝒇𝟑 𝒕 = 𝒄𝒐𝒔 𝒕

MATLAB Solution

21
Laplace Transform (LT)

Example:
Use MATLAB to find the inverse LT of the following s-domain functions:
𝟏 𝟏 𝒔 𝟏
𝑭𝟏 𝒔 = 𝑭𝟐 𝒔 = 𝑭 𝟑 𝒔 = 𝑭 𝟒 𝒔 =
𝒔−𝟏 𝒔−𝟏 𝟐 𝒔𝟐 + 𝟏 𝒔𝟐 + 𝟏

22
Laplace Transform (LT)

• The LTI system transfer function H(s) is a rational function, given as the ratio of two
polynomials in s.

• These rational functions can be expressed in terms of a partial fraction expansion (PFE).

• Partial fraction expansion can be used in the evaluation of the inverse Laplace transform.

23
Laplace Transform (LT)
Example:
Verify that multiple poles are present in H(s) and evaluate the partial fraction expansion of H(s).
𝟓𝒔𝟒 + 𝟕𝒔𝟑 + 𝟑𝒔𝟐 + 𝟓𝒔 − 𝟑𝟎
𝐇 𝒔 = 𝟒
𝒔 + 𝟒𝒔𝟑 + 𝟕𝒔𝟐 + 𝟔𝒔 + 𝟐

24
“There will come a time when you
believe everything is finished…That
will be the beginning.”

25

You might also like