map notes

21
MATLAB awareness program MAP i INDEX Sr.No Topic Name Page number Introduction to MATLAB 7 1 Starting of MALAB 1.1 MATLAB windows 1.2 Display formats 1.3 Built in functions 1.4 Rules of variables 1.5 Predefined variables 1.6 Managing variables 2 Creating Arrays 2.1 One dimensional (Vector) 2.2 Two dimensional (Matrix) 2.3 Array addressing 2.4 Built in function 2.5 Strings & Strings as variables 2.6 Assignment-I 3 Mathematics operation with array 3.1 Addition & Subtraction 3.4 Multiplication 3.5 Division 3.6 Element by element operation 3.7 Built in functions 3.8 Generation of random numbers 3.9 Assignment- II 4 File 4.1 Script 4.2 Functions 4.3 Assignment-III 5 Plotting 5.1 2D&3D plot 5.2 Assignment-IV 6 MATLAB programming 6.1 Relational operators 6.2 Logical operator 6.3 Built in functions 6.4 Assignment V

Upload: ngnirmal

Post on 26-Mar-2015

243 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: MAP Notes

MATLAB awareness program MAP

i

INDEX

Sr.No Topic Name Page number

Introduction to MATLAB 7 1 Starting of MALAB

1.1 MATLAB windows

1.2 Display formats 1.3 Built in functions

1.4 Rules of variables 1.5 Predefined variables

1.6 Managing variables

2 Creating Arrays

2.1 One dimensional (Vector)

2.2 Two dimensional (Matrix) 2.3 Array addressing

2.4 Built in function 2.5 Strings & Strings as variables

2.6 Assignment-I

3 Mathematics operation with array

3.1 Addition & Subtraction 3.4 Multiplication

3.5 Division 3.6 Element by element operation

3.7 Built in functions 3.8 Generation of random numbers

3.9 Assignment- II

4 File

4.1 Script

4.2 Functions 4.3 Assignment-III

5 Plotting

5.1 2D&3D plot

5.2 Assignment-IV

6 MATLAB programming

6.1 Relational operators

6.2 Logical operator 6.3 Built in functions

6.4 Assignment V

Page 2: MAP Notes

MATLAB awareness program MAP

ii

1. Starting with MATLAB

Why MATLAB

Numeric computation software

High level language Basic data type matrix

Dimensioning is not required No compilation or linking

High accuracy guarantee Graphics is incorporated

Varies toolboxes in variety of domain Computations are performed in complex valued double precision

arithmetic

1.1 MATLAB Windows

Table 1.1 MATLAB windows

Sr.No Window Purpose

1 Command

Main window, enters variables, runs

programs.

2 Figure

Contains output from graphic

commands.

3 Editor

Creates and debugs script and function files.

4 Help

Provides help information.

5 Launch Pad Provides access to tools, demos, and

documentation.

6 Command History Loges commands entered in the

Command window.

7 Workspace Provides information about the variables that are used.

8 Current Directory Shows the files in the current directory.

Page 3: MAP Notes

MATLAB awareness program MAP

iii

1.2 Display formats

Table 2.1 Display formats

Sr.No Command Description

1 format short Fixed-point with 4 decimal digits for: 0.001<=number <=1000 Otherwise

display format short e.

2 format long

Fixed-point with 14 decimal digits for:

0.001<=number <=100 Otherwise display format long e.

3 format short e Scientific notation with 4 decimal digits.

4 format long e Scientific notation with 15 decimal digits

5 format short g Best of 5-digit fixed or floating point.

6 format long g Best of 15-digit fixed or floating point.

7 format bank Two decimal digits.

8 format compact Eliminates empty lines to allow more lines with information displayed on the

screen.

9 formats loose Adds empty lines (opposite of compact).

1.3 Elementary functions

Sr.No Function Description Example

1 sqrt(x) Square root >> sqrt(81) ans = 9

2 exp(x) Exponential (ex) >> exp(5) ans = 148.41

3 abs(x) Absolute value >> abs(-24)

ans = 24

4 log(x) Natural logarithm

base e logarithm (ln)

>> log(1000)

ans = 3.00

5 log10(x) base 10 logarithm >> log10(1000) ans = 3.00

6 Factorial(x) The factorial function x! (x must be + ve

integer)

>> factorial(5) ans = 120

Trigonometric

Sr.No Function Description Example

1 sin(x) Sin of angle (x in

radians)

>> sin(pi/6)

ans = 0.5

Note: - Syntax for cosine, tangent, cotangent etc is same.

Page 4: MAP Notes

MATLAB awareness program MAP

iv

Rounding function

Sr.No Function Description Example

1 round(x) Round to nearest

integer

>> round(17/5)

ans = 3

2 fix(x) Round towards zero >> fix(13/5) ans = 2

3 ceil(x) Round towards infinity >> ceil(11/5) ans = 3

4 floor(x) Round towards minus

infinity

>> floor(-9/4)

ans = -3

5 rem(x) Returns remainder after

x is divided by y

>> rem(13,5)

ans = 3

6 sign(x) Signum function.

returns 1 if x>0, -1if

x<0, 0 if x = 0

>> sign(5)

ans = 1

Rules for MATLAB

If a semicolon (;) is typed at end of command output of the

command is not displayed. To write comment % sign at the beginning of line

Using command clc command , it clears command window If command is to long to fit in one line , it can be continued to the

next line by typing three periods … (Called an ellipsis)

1.4 Rules of variables

Variable names can be to 63 (in MATLAB 6.5) characters long (31

characters in MATLAB 6.0 Can contain letters, digits and underscore character

Must begin with letter

MATLAB is case sensitive Avoid using names of built-in functions (sin, cos etc)

Command can be of 4096 characters

1.5 Predefined variables

A number of frequently used variables are already defined when MATLAB

is started.

Ans : If the user does not assign the value of an expression to a variable MATLAB Automatically stores the result in ans.

Page 5: MAP Notes

MATLAB awareness program MAP

v

Pi : The number Π (22/7 or 3.14)

Eps : The smallest difference between two numbers.

Inf : used for infinity.

i : defined as √-1, which is 0+1.0000i.

NaN : stands for not a number (0/0) (Syntax is CAPITAL -small- CAPITAL)

1.6 Managing variables:

Command outcome

Clear Removes all variables from the memory

Who Displays a list of variables currently in

the memory

Whos Displays a list of variables currently in

the memory

Page 6: MAP Notes

MATLAB awareness program MAP

vi

2. Creating Arrays

2.1One dimensional (Vector) Row vector

A = [ 1 2 3 4 5 6 ]; Or using single space between two numbers

A = [1,2,3,4,5,6]; Or using single comma between two numbers

A = [1:1:6]; Or Syntax: variable_name = [start : step : final]

A = [1:6]; Or Syntax: variable_name = [xi : xf]

A = linspace (1,6,6); Syntax: variable_name = linspace (xi, xf,n)

Column Vector

A = [ 1; 2; 3; 4; 5; 6 ];

2.2 Two dimensional (Matrix) A(i, j) = ith row & jth column

A = [1 2 3; 2 3 4; 5 6 7];

1 2 3

A = 2 3 4

5 6 7

2.3 Array addressing

A = [1 2 3; 2 3 4; 5 6 7];

1 2 3 A = 2 3 4

5 6 7

A (1,2) = 2 A(i, j) = ith row & jth column

A ( : , n ) = Refers to the element in all the rows (:) of column n

A ( n , : ) = Refers to the element in all the columns (:) of row n

A( : , m : n) = Refers to the element in all the rows (:) between columns

m & n

Page 7: MAP Notes

MATLAB awareness program MAP

vii

A( m : n ,:) = Refers to the element in all the columns (:) between rows

m & n

A( m : n , p: q) = Refers to the element in rows m through n & Columns

p through q

2.4 Built in functions for handling arrays

Sr.No Function Description Example

1

length(A) Return no of element in the

vector

>>A = [1 2 3]; >>Length (A)

ans = 3

2

size(A) Returns a row vector [m,n]

>>A= [1 2 ;3 4] >>size(A)

ans = 2 2

3

reshape(A,m,n) Rearrange a matrix

A that has r rows& s columns to have

m rows & n columns. r times s

must be equal to m times n

>>A= [1 2 6 ;3 4 5]

>>B = reshape(A, 3, 2) B =

1 4 3 6

2 5

4

diag (a)

Note small a is

variable other than A

When a is a vector

creates a square matrix with the

elements of A in the diagonal

>>a = [1 2 3 ];

>>A =diag(a) A =

1 0 0 0 2 0

0 0 3

5

diag (A)

When A is a matrix,

creates vector from diagonal elements

of A

>>A= [1 2 3;4 5 6;7 8

9 ]; >>B =diag(A)

B =

1 5

9

Note : For diag direction is always

Page 8: MAP Notes

MATLAB awareness program MAP

viii

2.5 Strings & Strings as variable

A string is an array of characters. It is created by typing the

characters within single quotes.

String can include letters, digits, other symbols, and spaces Examples of strings: „ad ef ‟, „3%fr2‟,‟ {edcba:21!‟, „MATLAB‟.

A string that contains a single quote is created by typing two single quotes within the string.

When a string is being typed in, the color of the text on the screen changes to purple when the first single quote is typed.

When the single quote at the end of the string is typed the color of the string changes to maroon.

Strings have several different uses in MATLAB. They are used in output commands to display text messages, in formatting

commands of plots, and as input argument of some function. When strings are being used in formatting plots, characters

within the string can be formatted to have a specified font, size, position, .color, etc.

Assignment- I

Q.1 Create a row vector that has the elements 32, 4, 81, e2.5, 63, cos

(Π/3) & 14.12

Q.2 Create a column vector that has the elements : 55, 14, ln(51), 987, 0 & 5sin(2.5 Π )

Q.3 Create the following matrix 6 43 2 11 87

A = 2 6 34 0 5 34 18 7 41 9

a) Create a five element row vector named va that contains

the elements of second row of A. b) Create a three element row vector named vb that contains

the elements of fourth column of A. c) Create a ten element row vector named vc that contains

the elements of the first &second rows of A. d) Create a six element row vector named vd that contains

the elements of second & fifth columns of A.

Q.4 Using Zeros and Ones commands create a 3x5 matrix in which

the 1st ,2nd & 5th columns are 0‟s and 3rd ,4th columns are 1‟s.

Page 9: MAP Notes

MATLAB awareness program MAP

ix

3. Mathematics operation with array

3.1 Addition & Subtraction

11 12 13

21 22 23

31 32 33

a a a

A a a a

a a a

&

11 12 13

21 22 23

31 32 33

b b b

B b b b

b b b

Addition

11 11 12 12 13 13

21 21 22 22 23 23

31 31 32 32 33 33

a b a b a b

C A B a b a b a b

a b a b a b

Subtraction

11 11 12 12 13 13

21 21 22 22 23 23

31 31 32 32 33 33

a b a b a b

C A B a b a b a b

a b a b a b

3.2 Multiplication

The product of the multiplication of two square matrices is also square

matrix of the same size. The multiplication of matrices is not

commutative. A*B ≠ B*A

11 12

21 22

a aA

a a

11 12

21 22

b bB

b b

11 11 12 21 11 12 12 22

21 11 22 21 21 12 22 22

*a b a b a b a b

C A Ba b a b a b a b

3.3 Division

Take A & B matrix. Where B is inverse of A (A-1). Then multiply A*B

AI = IA = A & BA = AB =I Two types of division is possible

Left division AX = B;

A-1 AX = A-1B but A-1 AX = IX = X Hence X = A-1B means X = A\B

Right division XA = B;

X A A-1 = B A-1 Hence X = B A-1 means X = B/A

Page 10: MAP Notes

MATLAB awareness program MAP

x

3.4 Element by element operations All possible operations with matrices can be done or limited by element by

element operations. These operations can be done by putting dot before any multiplication (.*), division (. / or.\), or exponentiation (. ^).

a = [a1 a2 a3 a4] & b = [b1 b2 b3 b4]

Example:

a .*b = [ a1b1 a2b2 a3b3 a4b4]

a ./ b = [a1/b1 a2/b2 a3/b3 a4/b4]

a .^ b = [ (a1)b1 (a2)b2 (a3)b3 (a4)b4]

3.5 Built in function for mathematics

Sr.N

o

Function Description Example

1

mean(A) If A is a vector, returns the

mean value of the elements of the vector.

>> A=[5 9 2 4];

>> mean (A) ans = 5

2

C=max(A)

[d, n] = max(A)

If A is vector, C is the largest element in A. If A is a matrix, C is a row vector containing the

largest element of each column of A.

If A is vector, d is the largest element in A, n is the position of the element (the first if

several have the max value).

>> A=[5 9 2 4 11 6 7 11 0 1]; >>C = max(A)

c=11

>>[d, n] = max(A) d =

11 n =

5

3

Min(A)

[d, n]=min(A)

The same as max (a), but for

the smallest element. The same as [d, n] = max (A), but for the smallest element.

>> A=[5 9 2 4];

>> min(A) ans = 2

4

Sum (A) If A is vector, returns the sum of the elements of the vector.

>> A = [5 9 2 4]; >> sum(A)

ans = 20

5

Sort (A) If A is vector, arranges the elements of the vector in

ascending order.

>> A= [ 5 9 2 4]; >> sort(A)

ans = 2 4 5 9

6 Median (A) If A is vector, returns the

median value of the elements of the vector.

>> A = [ 5 9 2 4]; >> median(A) ans =

Page 11: MAP Notes

MATLAB awareness program MAP

xi

4.5000

7

Std (A) If A is vector, returns the standard deviation of the element of the vector.

>> A = [ 5 9 2 4]; >> std(A) ans=

2.9439

8

det (A) Returns the determination of a

square matrix A.

>> A = [2 4; 3 5];

>> det(A) ans=

-2

9

dot (a, b) Calculates the scalar (dot)

product of two vectors a and b. The vectors can each be row or column vectors.

>> a=[1 2 3];

>>b=[3 4 5]; >> dot (a,b) ans = 26

10

Cross (a, b) Calculates the cross product of

two vectors a and b,(a x b) The vectors must have 3

elements.

>> a=[1 3 2];

>>b=[2 4 1]; >> cross(a,b)

ans = -5 3 -2

11

Inv (A) Returns the inverse of a square

matrix A.

>> A= [2 -2 1;3 2 -1 ;

2 -3 2]; >> inv(A) ans= 0.2 0.2 0

-1.6 0.4 1 -2.6 0.4 2

3.6 Built in function for random number

A set of numbers can be a random number. The rand command generates

uniformly distributed numbers with values between 0 & 1.The command can be used to assign these numbers to a scalar, vector, or a matrix

Sr.No Function Description Example

1

rand Generates a single

random number between 0 and 1.

>> rand

ans = 0.2311

2

rand(1, n) Generates an element n elements row vector of random number

between 0 and 1.

>>a = rand(1,4) a = 0.6068 0.4860 0.8913

0.7621

3

rand (n) Generates an n x n

matrix with random number between 0 and

1.

>> b = rand(3)

b = 0.4565 0.4447 0.9218

0.0185 0.6154 0.7382 0.8214 0.7919 0.1763

4

rand (m, n)

Generates an m x n matrix with random number between 0 and

1.

>> c = rand(3) c =

0.4057 0.9169 0.8936 0.352

0.9355 0.4103 0.057 0.813

Page 12: MAP Notes

MATLAB awareness program MAP

xii

5

randperm (n)

Generates a row vector with n elements that

are random permutation of integers

1 through n.

>> ranperm(8) ans =

8 2 7 4 3 6 5 1

Some time there is need to have random numbers that are distributed in

an interval other than (0, 1) or to have numbers that are only integers. The random numbers that are distributed in a range (a, b) can be

obtained by multiplying rand by (b-a) and adding the product to a (b - a)*rand + a

Example:- A vector of 10 elements with random values between (a = -5) and

(b = 10) can be created as above equation

r = 15*rand (1, 10)-5 r = -1.8 0.6973 6.7499 5.2122 1.9164 3.5174 6.9132 -4.1123

4.0430 -4.2460

Assignment- II

Q.1 The depth of well ,d,in meters can be determined from the time it

takes for a stone that is dropped into well (zero intial velocity) to hit the bottom by d= 0.5 x g x t2, where t is the time in seconds

and g=9.81m/s2. Determine d for t= 1 to 10s.(create a vector t and determine d using element by element calculations.

Q.2 Use the following matrices A, B, C to find

5 2 4

1 7 3

6 10 0

A

11 5 3

0 12 4

2 6 1

B

7 14 1

10 3 2

8 5 9

C

a) Does A*B = B*A ?

b) Does A*(B*C) = (A*B)*C ? c) Does (A*B)t = Bt*At ? (t means transpose)

d) Does (A+B)t = At +Bt ? (t means transpose)

Q.3 Solve the following system of four linear equations

5x+4y-2z+6w = 4 3x+6y+6z+4.5w = 13.5

6x+12y-2z+16w = 20 4x-2y+2z-4w = 6

Q.4 Use matrices from Q.2 for the following hence solve a) Calculate A+B and B+A to show that addition of matrices is

commutative. b) Calculate A+(B+C) and (A+B)+C to show that addition of

Page 13: MAP Notes

MATLAB awareness program MAP

xiii

matrices is associative. c) Calculate 5(A+C) and 5A+5C to show that , when matrices

are multiplied scalar , the multiplication is distributive. d) Calculate A*(B+C) and A*B+A*C to show that matrix

multiplication is distributive.

4. File

4.1 Script

A script file is a sequence of MATLAB commands also called

program. When script file runs, MATLAB executes the commands in the order

they are written just as if they were typed in the command window. When a script file has command that generates as output (e.g.

assignment of a value to a variable without semicolon at the end ), the output is displayed in the command window.

Using script file is convenient because it can be edited and executed several times

Script files are also called as M-files .It uses extension .m when they saved.

4.1.1 Creating

% This script file calculates the average points scored in three

games. % The points from each game is assigned to the variable by input

command. % The disp command is used to display the output.

g1=input('Enter the points scored in 1st game '); g2=input('Enter the points scored in 2nd game ');

g3=input('Enter the points scored in 3rd game '); ave=(g1+g2+g3)/3 ;

disp(' ') disp('The average of points scored in a game is :')

disp(' ') disp(ave)

4.1.2 Saving

Save this file with any name (not number) at default folder „work‟ which

located at C:\MATLAB7\work check the same path at MATLAB7 window current directory location.

4.1.3 Running After saving go to debug menu and click on run or save and run (if not

saved earlier).

Page 14: MAP Notes

MATLAB awareness program MAP

xiv

4.1.4 Outing Using disp command we are outing result .The command window will look

like as Enter the points scored in 1st game 5

Enter the points scored in 2nd game 4 Enter the points scored in 3rd game 5

The average of points scored in a game is :

4.6667 We can use fprintf('The average of point scored in the three games%f

',ave); in place of disp

4.2 Function

Function files are m-files .That are used to create new MATLAB functions. Variables defined and manipulated inside a function file are

local to function. The general form of function file is

Function variable (s) = function_name(argument)

% help text in the usage of the function %

.

.

end

4.2.1 Creating Ex- Write a function file to solve the equivalent resistance of series

connected resistor R1, R2, R3, R4….Rn R = R1+R2+R3+…..+Rn

Function req =equiv_sr (r) % equiv_sr (r) is a function program for obtaining the equivalent

resistance of series connected resistor % Usage : req = equiv_sr(r)

% r is input vector of length n

% req is an output, the equivalent resistance (scalar) n=length(r); % number of resistance

req = sum(r); % sum of resistor end

4.2.2 Saving Save this file with equiv_sr.m (not number) at default folder „work‟ which

located at C:\MATLAB7\work check the same path at MATLAB7 window current directory location.

4.2.3 Running After saving go to command promt >> use the function as shown

>>a=[1.1 100 2.2 14]; >> series=equiv_sr(a)

Page 15: MAP Notes

MATLAB awareness program MAP

xv

4.4.4 Outing

series = 117.3000

Assignment- III

Q.1 Write a program in a script file that determines the real roots of a quadratic equation ax2+bx+c=0. Name the file quadroots. When

the file runs it asks the user to enter the values of the constants a, b, and c.To calculate the roots of the equation the program

calculates the discrimination D given by: D= b2-4ac

If D >0 the program displays a message “ the equation has two

roots”, and the roots are displayed in the next line. If D = 0 the program displays a message “ the equation has one

root”, and the roots are displayed in the next line. If D<0 the program displays a message “ the equation has no

real roots”, and the roots are displayed in the next line. Run this file in command window to obtain solution for

X2+3x+2=0 15X2+10x+5=0

x2-2x+3=0 Q.2 Write a function file that can be used to calculate the equivalent

resistance of n parallel connected register

1 2 3

1 1 1 1 1........

eq nR R R R R

Page 16: MAP Notes

MATLAB awareness program MAP

xvi

5. Plotting

5.1 2D PLOTS

MATLAB has built in functions that allow one to generate bar charts, X-Y polar, contour and 3D.

MATLAB also allows one to give titles to graph, label the X-Y axes and add grid to graphs.

Syntax : Plot (x,y)

: plot(x, y,‟ line specifier‟, „property name‟, property value)

Line specifier It is an optional. It can be used to define the style & color of

line and type of marker

Line style Specifier

Dotted :

Dash-dot -.

Line color Specifier

Red r

Green g

Blue b

Cyan c

Magneta m

Yellow y

Black k

white w

Function Plot

Bar (x,y) Vertical bars

Barh (x,y) Horizontal bars

Stairs(x,y) Stairs case nature

Stem(x,y) Sampled

graph

Pie(x) Pie chart

5.1.1 Multiple plots on one graph 1) „hold all‟ command can be used.

2) Subplot (x, y, position)

Line style Specifier

Solid (default)

-

dashed --

Marker type Specifier

Plus sign +

Circle O

Asterisk *

Point .

Square S

Diamond D

Five pointed P

Six pointed

star

h

Page 17: MAP Notes

MATLAB awareness program MAP

xvii

5.2 3D PLOTS

Mesh & surface plot

%This program plots 3D for given function x=-3:0.25:3;

y=-3:0.25:3; [X,Y]=meshgrid(x,y);

Z=1.8.^(-1.5*sqrt(X.^2+Y.^2)).*cos(0.5*Y).*sin(X); mesh(X,Y,Z)

xlabel('x');ylebel('y');zlebel('z')

Some other types

Sr.No Plot type Syntax Example

1 Mesh plot

Mesh(x, y, z) Use above

2 Surface plot

surf(x, y, z) Use above example ;replace mesh(X,Y,Z) by

surf(X,Y,Z)

3

Mesh and

curtain plot

Meshz(X,Y,Z) Draws curtain around the

mesh. Use above example.

4

Mesh and

contour plot

Meshc(X,Y,Z) Draws contour beneath

the mesh. Use above example.

2,2,1 2,2,2

2,2,3 2,2,4

Page 18: MAP Notes

MATLAB awareness program MAP

xviii

5

Surface

and contour

plot

Surfc(X,Y,Z) Draws contour beneath

the surface. Use above example.

6

Surface

plot with lighting

Surf1(X,Y,Z) Use above example.

7

Waterfall

plot

Waterfall(X,Y,

Z)

Draws a mesh in one

direction only. Use above example.

8 3D contour Contour3(X,Y,

Z,n) Where n is the number of contour levels. Use above

example

9 2D contour Contour(X,Y,Z

,n) Where n is the number of contour levels. Use above

example

Plot with special graphics

Sr.No Plot type Syntax Example

1

sphere

Sphere(n) Returns the x,

y, z coordinates of a unit sphere

with 20faces

[X,Y,Z]=sphere(20); surf(X,Y,Z)

2

cylinder Cylinder(r) Returns the x,

y, z coordinates of a cylinder

with profile r

t = linspace(0, pi,20); r=1+sin(t);

[X,Y,Z]=cylinder(r); surf(X,Y,Z)

axis square

3

3D bar plot Bar3(Y)

Each element in

Y is one bar. columns are

grouped together

Y = [1 6.5 7;2 6 7; 3

5.5 7; 4 5 7;3 4 7;2 3

7;1 2 7]; Bar3(Y)

4

3D stem Stem3(X,Y,Z) Draws

sequencetial points with

markers and

vertical lines from the X-Y

plane

t =0:0.2:10 x=t;

y=sin(t) z=t .^1.5;

stem(x,y,z,‟fill‟)

grid on

5 3D scatter Scatter3(X,Y,Z)

Removes

t =0:0.2:10

x=t;

Page 19: MAP Notes

MATLAB awareness program MAP

xix

vertical lines

from stem plot.

y=sin(t)

z=t .^1.5; scatter3(x,y,z,‟filled‟)

grid on

6

3D pie Pie3(X,

explode) Plots pie charts

X= [5,9,14,20];

explode =[0 0 1 0]; Pie3(X,explode)

Assignment- IV

Q.1 Plot sin and cos function on same graph. Use linspace command to generate steps?

Q.2 Plot polar graph for r= sin2 (Φ)

Q.3 A message signal m(t) and the carrier signal c(t) of a communication system are respectively

m(t) = 4cos(120Πt)+2cos(240Πt) c(t) = 10cos (10000Πt)

A double sideband suppressed carrier s(t) is given as S(t) = m(t)c(t)

Plot m(t), c(t) and s(t) using the subplot command

Page 20: MAP Notes

MATLAB awareness program MAP

xx

6. MATLAB programming

6.1 Relational & logical operators

A relational operator compares two numbers by determining

whether a comparison statement is true or false.

Sr.No Relational operator Description

1 < Less than

2 > Greater than

3 <= Less than or equal to

4 >= Greater than or equal to

5 = = Equal to

6 ~ = Not equal to

Note: More important in order of precedence type mathematics

numericals. Example:

>> 5>8 ans =

0 (false) Logical operators have numbers as operands. A nonzero number is

true, and a zero number is false.

Sr.No Logical operator Description

1 & ANDing

2 | ORing

3 ~A NOT

Precedence

Precedence operation

1 highest Parentheses

2 Exponentiation

3 Logical not

4 Multiplication/ division

5 Addition/ Subtraction

6 Relational operators

7 Logical AND

8 Logical OR

These are some built in functions

And (A,B), or (A, B), not (A), xor (a, b), all(A), any(A), find (A), find(A>d)

Page 21: MAP Notes

MATLAB awareness program MAP

xxi

Assignment- V Q.1 Evaluate the following expressions without using MATLAB. Check

the answer with MATLAB. a) 5< = 8-3

b) y = 7<3-1+6>2

c) y = (7<3) -1+(6>2) d) y = 2x4+5 = = 7 + 20/4

Q.2 Write a user defined the function that sorts the elements of a vector (of nay length) from largest to smallest for the function

name and arguments use Y= downsort(X) .The input to the function is vector X of any length and output Y is vector in which

elements of X are arranged in descending order .Do not use the MATLAB sort function test your function on a vector with 14

numbers (integers) randomly distributed between -30 and 30. Use MATLAB rand function to generate the initial vector.