1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab Basic...

33
1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab Basic Features Scientific features Array Operations Script Files or M-files

Transcript of 1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab Basic...

Page 1: 1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab  Basic Features  Scientific features  Array Operations  Script.

1

DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1)

Introduction to Matlab

Basic FeaturesScientific featuresArray OperationsScript Files or M-files

Page 2: 1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab  Basic Features  Scientific features  Array Operations  Script.

2

Basic Features

• Matlab is a tool for doing numerical computations with matrices and vectors. It can also display information graphically.

• The best way to learn to use Matlab is to run Matlab, trying the examples and experimenting.

Page 3: 1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab  Basic Features  Scientific features  Array Operations  Script.

3

Simple Math

• Matlab can be used as a calculator (Text in yellow is what you type, text in blue is what the computer “types” back):

>> 2 + 4 + 6

ans =

12

>> 2 * 4 + 6*2 - 8/2

ans =

16

Page 4: 1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab  Basic Features  Scientific features  Array Operations  Script.

4

Operation and Symbol

Operation Symbol Example

Addition, a + b

Subtraction, a – b

Multiplication, a x b

Division, a ÷ b

Power, a

+

-

*

/

^

2 + 3

5 - 4

3.14 *2.1

10/2.5

3^2

Page 5: 1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab  Basic Features  Scientific features  Array Operations  Script.

5

Expressions

• Expressions are evaluated from left to right with the power operation having the higher order of precedence, followed by both addition and subtraction having equal precedence

• Paratheses are used to alter this usual ordering. Evaluation initiates within the innermost parentheses and proceeds outward

>>3^2 - 5 - 6 / 3*2 ans= 0

Page 6: 1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab  Basic Features  Scientific features  Array Operations  Script.

6

The Matlab Workspace

• Matlab remembers the command you enter as wellas the values of any variables you create. These command and variables are said to reside in the Matlab Workspace and can be recalled whenever you wish.

>>a = 4 a = 4 >>a a = 4

Page 7: 1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab  Basic Features  Scientific features  Array Operations  Script.

7

Number Display Formats

• When a result is an integer, Matlab displays it as an integer.

>> a = 4;

>> b = 2;

>> c = a*b;

c =

8

Page 8: 1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab  Basic Features  Scientific features  Array Operations  Script.

8

Number Display Formats

• When a result is a real number, Matlab displays it with four digits to the right of t6he decimal point.

>> a = 4;

>> b = 2;

>> c = a/b;

c =

1.333

Page 9: 1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab  Basic Features  Scientific features  Array Operations  Script.

9

Variables in Matlab

• Variables are case sensitive (apple, Apple and APPLE).

• Variables can contain up to 19 characters.

• Variables must start with a letter, followed by any number of letters, digits, or underscores

Page 10: 1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab  Basic Features  Scientific features  Array Operations  Script.

10

• When Matlab performs a calculation, it does so using the values it knows at the time the requested command is evaluated.

>> a= 4; >> b= 3; >> c= a + b c= 7 >> b= 5; >> c= a + b c= 9

Page 11: 1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab  Basic Features  Scientific features  Array Operations  Script.

11

• Variables in the Matlab workspace can be unconditionally deleted by using the command clear.

>> clear a

>> a

??? Undefined function or variable ‘a’

>> clear b

>> b

??? Undefined function or variable ‘b’

Page 12: 1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab  Basic Features  Scientific features  Array Operations  Script.

12

Summary

• Matlab knows addition (+),substraction (-), multiplication (*), division (/), and power (^).• Matlab evaluates expressions from left to right

giving precedence to power over multiplication and division and these over addition and subtraction

• A semicolon(;) at the end of a Matlab statement suppresses printing of results.

• If statement too long, type three periods (…) followed by Enter to continue the Matlab statement on the next line.

Page 13: 1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab  Basic Features  Scientific features  Array Operations  Script.

13

Summary (cont.)

• As a default, Matlab stores results in the variable ans.

• Matlab remembers only the first 19 charactes of a variable name.

• Variable must begin with letter.

• Matlab is case sensitive.

• Comments in Matlab begin with %

Page 14: 1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab  Basic Features  Scientific features  Array Operations  Script.

14

Scientific Features

• Matlab offers many common functions important to mathematics, engineering, and the sciences.

• Matlab handles complex numbers

Page 15: 1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab  Basic Features  Scientific features  Array Operations  Script.

15

Common Mathematical Functions

>> a=sqrt(9)/2a = 1.5000>> b=sin(a/2)b = 0.6816>> c=round(a)*(b*180/pi)c = 78.1100

Page 16: 1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab  Basic Features  Scientific features  Array Operations  Script.

16

Complex numbers

• In matlab, the conversion between polar and rectangular form make use of the functions real, imag, abs, and angle:

>> a=1-2ia = 1.0000 - 2.0000i>> abs(a)ans = 2.2361>> real(a)ans = 1

Page 17: 1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab  Basic Features  Scientific features  Array Operations  Script.

17

>> imag(a)ans = -2>> b_angle=angle(a)b_angle = -1.1071>> b_degree=b_angle*180/pib_degree = -63.4349

Page 18: 1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab  Basic Features  Scientific features  Array Operations  Script.

18

Script files, or M-files

• Matlab commands can be placed in a text file, called script or M-file.

• To create M-file choose New from the File menu and select M-file. This procedure brings up a text editor window.

• Commands within the M-file have acces to all variables in the Matlab workspace, and all variables created in the M-file become part of the workspace.

Page 19: 1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab  Basic Features  Scientific features  Array Operations  Script.

19

Matlab Graphics

• Creating Simple Plots

• Manipulating Plots

Page 20: 1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab  Basic Features  Scientific features  Array Operations  Script.

20

Creating Simple Plots

• Plots are a powerful visual way to interpret data

• Matlab has an extensive graphics capabilities

Page 21: 1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab  Basic Features  Scientific features  Array Operations  Script.

21

Case Study #1: y=sin(x)

• Plot a sine function over one period: y = sin(x) for 0≤x ≤2π• First we choose data point for the

independent variable x . This data forms the horizontal axes of the plot.

• Then the sine of each data point is found – this provides the vertical axes of the plot.

• Each pair {x ,y } is then marked on a suitable set of axes.

Page 22: 1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab  Basic Features  Scientific features  Array Operations  Script.

22

Case Study #1: y=sin(x) (cont.)

• Matlab uses arrays to accomplish this task:

>> x= linspace(0,2*pi,30);

creates 30 points between 0 and 2π.

>>y=sin(x);

finds the sine of the point in x.

>>plot(x,y)

generates the plot.

Page 23: 1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab  Basic Features  Scientific features  Array Operations  Script.

23

Case Study #1: y=sin(x) (cont.)

Page 24: 1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab  Basic Features  Scientific features  Array Operations  Script.

24

Case Study #1: y=sin(x) (cont.)

• The Matlab function plot automatically chooses axis limits,

• Marks the individual data points, and

• Draws straight lines between them.

Page 25: 1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab  Basic Features  Scientific features  Array Operations  Script.

25

• Options in the plot command allow us to plot multiple data sets on the same axes,

• Use different line types (dotted and dashes),

• Mark just the data points without drawing lines between them,

• Use different colors for curves,• Place labels on the axes, a title on the top,• Draw a grid at the tick marks.

Page 26: 1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab  Basic Features  Scientific features  Array Operations  Script.

26

>>z = cos(x);

>>plot(x,y,x,z)

Page 27: 1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab  Basic Features  Scientific features  Array Operations  Script.

27

Plot 2sin(x)cos(x) using dashed lines

>>plot(x,y,x,2*y*z,’--’)

Page 28: 1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab  Basic Features  Scientific features  Array Operations  Script.

28

Places a grid at the tick marks of the current plot.

• >>grid

Page 29: 1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab  Basic Features  Scientific features  Array Operations  Script.

29

Plots sine versus cosine

• >>plot(y,z)

Page 30: 1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab  Basic Features  Scientific features  Array Operations  Script.

30

Case Study #2:Plot the gain of the low pass

filter versus the frequency.

• Given parameters are as follow: Range of the frequencies : 0Hz -100KHz. value of resistor and capacitor: R=10k ohm and C=0.0033µ Farads Cutoff frequency , ω=1/RC Theoretical Gain, G=1/√(1+(2πfRC)^2)Measured gain=[1 0.98 0.98 0.8 0.7 .045 0.15 0.05]Frequencies at which gain were measured (Hz): =[100 300 1000 3000 4800 10000 30000 100000]

Page 31: 1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab  Basic Features  Scientific features  Array Operations  Script.

31

Case Study #2:Plot the gain of the low pass

filter versus the frequency.(cont.)

• First you have to set the initial parameters >>f=0:100000; >>R=10000; C=3.3*10^(-9);Then calculate the theoretical cutoff frequency and

theoretical gain >>fc=1/(2*pi*R*C); >>Gt=sqrt(1./(1+(2*pi*R*C.*f).^2));Next , plot Gt versus f>> plot (f, Gt,’r’)

Page 32: 1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab  Basic Features  Scientific features  Array Operations  Script.

32

Case Study #2:Plot the gain of the low pass

filter versus the frequency.(cont.)

Page 33: 1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab  Basic Features  Scientific features  Array Operations  Script.

33

EXERCISE SESSION