0% found this document useful (0 votes)
8 views9 pages

MATLAB Operators and Functions Guide

Uploaded by

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

MATLAB Operators and Functions Guide

Uploaded by

Sagar kumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1. List the relational operators in MAT Lab.

Ans:

Relational operators:
Operator Description
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Equal to
~= Not equal to

2. What is the usage of control system tool box in MAT Lab?

Ans: a) Can create models of linear time invariant (LTI) in transfer function, zero pole gain.
b) Can calculate graph time response, frequency response, loci of roots.

3. List any three differences between C and MAT Lab.

Ans:

C MAT Lab
1. It is a low level language [Link] is a high level language
2. Is a strong programming language 2. is a scripting and mathematical language
3. Fast 3. Slow

4. State the need for MAT Lab.

Ans: a) For numerical computation, visualization and application development.


b) Provides the built in graphics for visualization of data.
c) Provides tools for Graphic User Interface (GUI).

5. What are the uses of simulink?

Ans: a) is a simulation and model based environment for dynamic and embedded systems.
b) Supports system level design.
c) Simulation.
d) Automatic Code Generation.
e) Testing and verification of embedded systems.

6. State the significance of MAT Lab over other high level languages.

Ans: a) Its data element is matrix.


b) Vectorized operations.
c) Graphics output.
d) Functionality expanded by using tool boxes.

7. Write the syntax of if-end statement in MAT Lab.

Ans:

Syntax:
If < expression >
Statements….. ( will execute if expression is true)
end
8. List any six data types in MAT Lab.

Ans:

Data Types
int 8
unit 8
single
Double
Logical
char

9. List logical and special operators in MAT Lab.

Ans:

Logical operators:

Operator Description
& AND
| OR
~ NOT

Special operators:

Operator Description
@ Function handle construction and reference
. Element-wise operations
, Separator
: Vector creation

10. List relational and arithmetic operators in MAT Lab.

Ans:

Relational operators:

Operator Description
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Equal to
~= Not equal to
Arithmetic operators:

+ Addition

+ Unary plus

- Subtraction

- Unary minus

.* Element-wise multiplication

* Matrix multiplication

./ Element-wise right division

/ Matrix right division

.\ Element-wise left division

\ Matrix left division


(also known as backslash)
.^ Element-wise power

^ Matrix power

.' Transpose

11. State the syntax of while loop.

Ans:

Syntax:
while<condition>
Statements….. ( repeatedly executes the statements while condition is true)
End

12. What are the input/output commands in MAT lab ?

Ans.

MATLAB provides the following input and output commands

Command Purpose

disp Displays contents of an array or string.

fscanf Read formatted data from a file.

format Controls screen-display format.

fprintf Performs formatted writes to screen or file.

input Displays prompts and waits for input.


1. Explain the plot commands.
a) plot(x,y) b) title( ) c) xlabel( ) d) ylabel( ) e) legend( )

plot(x,y) - Generates x,y plot.


title( ) - Put text at the top of the plot.
xlabel( ) - Add text label to x-axis.
ylabel( ) - Add text label to y-axis.
Legend( ) - Legend properties control the appearance and behavior
Of a Legend object.
Ex:

x = [0:0.1:10];
y =sin(x);
z = cos(x);
plot(x,y);
xlabel(‘x’);
ylabel(‘sin(x)’);
title(‘sin(x) Graph’);

Output:

plot(x,z);
xlabel(‘x’);
ylabel(‘cos(x)’);
title(‘cos(x) Graph’);

Output:

plot(x,y,x,z,’.-‘);
legend(‘sin(x)’,’cos(x)’);
Output:

2. 1D & 2D arrays in MAT Lab:

All the variables of data types in MAT Lab are multidimensional arrays. A vector is a one dimensional
array and a matrix is a two dimensional array.
A vector is a one dimensional array of numbers. MAT Lab allows creating two types of vectors.

1. Row Vectors:
Row vectors are created by enclosing the set of elements in square bracket, using space or
comma to delimit the elements.

Ex: r = [ 1 2 3 4 5]
Output:
r=1 2 3 4 5

2. Column Vectors:
Column vectors are created by enclosing the set of elements in square bracket, using semicolon
to delimit the elements.

Ex: c = [ 1; 2; 3; 4; 5]
Output:
C=
1
2
3
4
5

3. Matrices:
A Matrix is a two dimensional array of numbers. In MAT Lab, a matrix is created by entering each
row as sequence of space or comma separated elements, and end of row is demarcated by a
semicolon.

Ex: m = [1 2 3; 4 5 6; 7 8 9]
Output:

m=
1 2 3
4 5 6
7 8 9
3. Write short notes on following operators:
a) Arithmetic b)Relational c) Logical d) Bitwise

Arithmetic operators:

+ Addition
+ Unary plus
- Subtraction
- Unary minus
.* Element-wise multiplication
* Matrix multiplication
./ Element-wise right division
/ Matrix right division
.\ Element-wise left division
\ Matrix left division
(also known as backslash)
.^ Element-wise power
^ Matrix power
.' Transpose

Relational operators: Logical operators:

Operator Description
& AND
| OR
~ NOT

Operator Description
< Less than
<= Less than or equal to Bitwise Operators:
> Greater than
>= Greater than or equal to
== Equal to
~= Not equal to
Operator Description
bitand(a, b) Bit-wise AND of integers a and b
bitcmp(a) Bit-wise complement of a

bitget(a,pos) Get bit at specified position pos, in the integer array a

bitor(a, b) Bit-wise OR of integers a and b

bitset(a, pos) Set bit at specific location pos of a

Returns a shifted to the left by k bits, equivalent to multiplying by 2k. Negative values of
bitshift(a, k) k correspond to shifting bits right or dividing by 2|k| and rounding to the nearest integer
towards negative infinite. Any overflow bits are truncated.

bitxor(a, b) Bit-wise XOR of integers a and b

swapbytes Swap byte ordering


4. Explain the following matrix operations.

a) Addition of Matrices:

To add two matrices, both the matrices must have the same number of rows and columns.
‘+’ operator is used to add two matrices.

Ex:

a = [1 2 3 ; 4 5 6 ; 7 8 9]; b = [7 5 6 ; 2 0 8 ; 5 7 1];

a+b=

8 7 9
6 5 14
12 15 10

b)Subtraction of Matrices:

To subtract a matrix from another, both the matrices must have the same number of rows
and columns.
‘-’ operator is used to subtract two matrices.

Ex:

a = [1 2 3 ; 4 5 6 ; 7 8 9]; b = [7 5 6 ; 2 0 8 ; 5 7 1];

a-b=

-6 -3 -3
2 5 -2
2 1 8

c) Multiplication of two Matrices:

In matrix multiplication, the elements of the rows in the first matrix are multiplied with
corresponding columns in the second matrix.
Each element in the (i, j)th position, in the resulting matrix C, is the summation of the products of
elements in ith row of first matrix with the corresponding element in the jth column of the second
matrix.
Matrix multiplication in MATLAB is performed by using the * operator.

Ex:

a = [1 2 3 ; 2 3 4 ; 4 5 6]; b = [2 3 4 ; 1 2 3 ; 1 4 5];

m=a*b

m=
7 19 25
11 28 37
19 46 61
d) Transpose of a Matrix:

The transpose operation exchanges the rows and columns in a matrix. It is represented by a
single quote ( ‘ ).

Ex:

a = [1 2 3 ; 4 5 6 ; 7 8 9];

a=
1 2 3
4 5 6
7 8 9

b=a’

b =

1 4 7

2 5 8

7 8 9

e) Inverse of a Matrix:

The inverse of a matrix A is denoted by A −1 such that the following relationship holds – A A −1 = A−1 A
=1
The inverse of a matrix does not always exist. If the determinant of the matrix is zero, then the
inverse does not exist and the matrix is singular.
Inverse of a matrix in MATLAB is calculated using the inv function. Inverse of a matrix A is given by
inv(A).

Ex:

a = [1 2 3; 2 3 4 ; 1 2 5]

b =inv(a)

-3.5000 2.0000 0.5000


3.0000 -1.0000 -1.0000
0.5000 0 0.5000
5. Explain decision making statements (i) if...end statement and(ii) if...else...end statement used in
MATLAB with a simple example.

Ans:

If (condition)
Statement
Else
Statement
end

Example: 1

let us consider an example to find a large or less than a specific number.

a=5
if ( a < 10 ) - - - - - condition 1
disp ( ' number is less than 10 ' ) - - - - - condition 1 is true
else
disp ( ' number is large than 10 ' ) - - - - - condition 1 is false
end

Output:

a = 5

the number is less than 10

Example: 2

num1 = input('Enter number 1: ');


num2 = input('Enter number 2: ');
if (num1 > num2)
disp('num1 is greater than num2')
else if (num1 < num2)
disp('num1 is less than num2')
else if (num1 == num2)
disp('num1 is equal to num2')
end

Output:

Enter number 1: 15
Enter number 2:20
num1 is less than num2

You might also like