You are on page 1of 21

MATLAB awareness program MAP

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

i
MATLAB awareness program MAP

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


Command Main window, enters variables, runs
1
programs.
Figure Contains output from graphic
2
commands.
Editor Creates and debugs script and function
3
files.
Help Provides help information.
4
Provides access to tools, demos, and
5 Launch Pad
documentation.
Loges commands entered in the
6 Command History
Command window.
Provides information about the variables
7 Workspace
that are used.
8 Current Directory Shows the files in the current directory.

ii
MATLAB awareness program MAP

1.2 Display formats

Table 2.1 Display formats

Sr.No Command Description


Fixed-point with 4 decimal digits for:
1 format short 0.001<=number <=1000 Otherwise
display format short e.
Fixed-point with 14 decimal digits for:
2 format long 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.
Eliminates empty lines to allow more
8 format compact 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 >> log(1000)
base e logarithm (ln) ans = 3.00
5 log10(x) base 10 logarithm >> log10(1000)
ans = 3.00
6 Factorial(x) The factorial function x! >> factorial(5)
(x must be + ve ans = 120
integer)

Trigonometric

Sr.No Function Description Example


1 sin(x) Sin of angle (x in >> sin(pi/6)
radians) ans = 0.5
Note: - Syntax for cosine, tangent, cotangent etc is same.

iii
MATLAB awareness program MAP

Rounding function

Sr.No Function Description Example


1 round(x) Round to nearest >> round(17/5)
integer 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 >> floor(-9/4)
infinity ans = -3
5 rem(x) Returns remainder after >> rem(13,5)
x is divided by y ans = 3
6 sign(x) Signum function. >> sign(5)
returns 1 if x>0, -1if ans = 1
x<0, 0 if x = 0

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.

iv
MATLAB awareness program MAP

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

v
MATLAB awareness program MAP

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

vi
MATLAB awareness program MAP

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


length(A) Return no of >>A = [1 2 3];
element in the >>Length (A)
1
vector ans =
3
size(A) Returns a row >>A= [1 2 ;3 4]
vector [m,n] >>size(A)
2
ans =
22
reshape(A,m,n) Rearrange a matrix >>A= [1 2 6 ;3 4 5]
A that has r rows& >>B = reshape(A, 3, 2)
s columns to have B=
3 m rows & n 1 4
columns. r times s 3 6
must be equal to m 2 5
times n
diag (a) When a is a vector >>a = [1 2 3 ];
creates a square >>A =diag(a)
Note small a is matrix with the A=
4
variable other elements of A in the 1 0 0
than A diagonal 0 2 0
0 0 3
diag (A) When A is a matrix, >>A= [1 2 3;4 5 6;7 8
creates vector from 9 ];
diagonal elements >>B =diag(A)
5 of A B=
1
5
9

Note : For diag direction is always

vii
MATLAB awareness program MAP

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.

viii
MATLAB awareness program MAP

3. Mathematics operation with array


3.1 Addition & Subtraction

 a11 a12 a13   b11 b12 b13 


   
A   a21 a22 a23  & B   b21 b22 b23 
a a33  b b33 
 31 a32  31 b32

Addition
 a11  b11 a12  b12 a13  b13 
 
C  A  B   a21  b21 a22  b22 a23  b23 
a b a33  b33 
 31 31 a32  b32
Subtraction

 a11  b11 a12  b12 a13  b13 


 
C  A  B   a21  b21 a22  b22 a23  b23 
a b a33  b33 
 31 31 a32  b32

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
a a  b b 
A   11 12  B   11 12 
 a21 a22   b21 b22 

a b a b a11b12  a12b22 
C  A * B   11 11 12 21 
 a21b11  a22b21 a21b12  a22b22 

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
-1
Hence X=A B means X = A\B
Right division
XA = B;
X A A-1 = B A-1
Hence X = B A-1 means X = B/A

ix
MATLAB awareness program MAP

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 Function Description Example


o
mean(A) If A is a vector, returns the >> A=[5 9 2 4];
1 mean value of the elements of >> mean (A)
the vector. ans = 5
C=max(A) If A is vector, C is the largest >> A=[5 9 2 4 11 6 7
element in A. If A is a matrix, C 11 0 1];
is a row vector containing the >>C = max(A)
largest element of each column c=11
of A.
2 [d, n] = If A is vector, d is the largest
max(A) element in A, n is the position >>[d, n] = max(A)
of the element (the first if d=
several have the max value). 11
n=
5
Min(A) The same as max (a), but for >> A=[5 9 2 4];
the smallest element. >> min(A)
3
[d, The same as [d, n] = max (A), ans =
n]=min(A) but for the smallest element. 2
Sum (A) If A is vector, returns the sum >> A = [5 9 2 4];
of the elements of the vector. >> sum(A)
4
ans =
20
Sort (A) If A is vector, arranges the >> A= [ 5 9 2 4];
elements of the vector in >> sort(A)
5
ascending order. ans =
2459
Median (A) If A is vector, returns the >> A = [ 5 9 2 4];
6 median value of the elements >> median(A)
of the vector. ans =

x
MATLAB awareness program MAP

4.5000
Std (A) If A is vector, returns the >> A = [ 5 9 2 4];
standard deviation of the >> std(A)
7
element of the vector. ans=
2.9439
det (A) Returns the determination of a >> A = [2 4; 3 5];
square matrix A. >> det(A)
8
ans=
-2
dot (a, b) Calculates the scalar (dot) >> a=[1 2 3];
product of two vectors a and b. >>b=[3 4 5];
9 The vectors can each be row or >> dot (a,b)
column vectors. ans = 26

Cross (a, b) Calculates the cross product of >> a=[1 3 2];


two vectors a and b,(a x b) >>b=[2 4 1];
10
The vectors must have 3 >> cross(a,b)
elements. ans = -5 3 -2
Inv (A) Returns the inverse of a square >> A= [2 -2 1;3 2 -1 ;
matrix A. 2 -3 2];
>> inv(A)
11
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


rand Generates a single
>> rand
1 random number ans =
between 0 and 1. 0.2311
rand(1, n) Generates an element >>a = rand(1,4)
n elements row vectora=
2
of random number 0.6068 0.4860 0.8913
between 0 and 1. 0.7621
rand (n) Generates an n x n >> b = rand(3)
matrix with random b=
3 number between 0 and 0.4565 0.4447 0.9218
1. 0.0185 0.6154 0.7382
0.8214 0.7919 0.1763
rand (m, Generates an m x n >> c = rand(3)
n) matrix with random c =
4 number between 0 and 0.4057 0.9169 0.8936 0.352
1. 0.9355 0.4103 0.057 0.813

xi
MATLAB awareness program MAP

randperm Generates a row vector >> ranperm(8)


(n) with n elements that ans =
5 are random 8 2 7 4 3 6 5 1
permutation of integers
1 through n.

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 11 5 3   7 14 1 
     
A   1 7 3  B   0 12 4  C  10 3 2 
 6 10 0  2 1   8 5 9 
   6  
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

xii
MATLAB awareness program MAP

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).

xiii
MATLAB awareness program MAP

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)

xiv
MATLAB awareness program MAP

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 1 1 1 1
    ........ 
Req R1 R2 R3 Rn

xv
MATLAB awareness program MAP

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 Line style Specifier


Solid - Dotted :
(default) Dash-dot -.
dashed --

Line color Specifier


Red r Marker type Specifier
Green g Plus sign +
Blue b Circle O
Cyan c Asterisk *
Magneta m Point .
Yellow y Square S
Black k Diamond D
white w Five pointed P
Six pointed h
star

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)

xvi
MATLAB awareness program MAP

2,2,1 2,2,2

2,2,3 2,2,4

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
Mesh plot Mesh(x, y, z) Use above
1
Surface surf(x, y, z) Use above example
2 plot ;replace mesh(X,Y,Z) by
surf(X,Y,Z)
Mesh and Meshz(X,Y,Z) Draws curtain around the
3 curtain plot mesh. Use above
example.
Mesh and Meshc(X,Y,Z) Draws contour beneath
4 contour the mesh. Use above
plot example.

xvii
MATLAB awareness program MAP

Surface Surfc(X,Y,Z) Draws contour beneath


and the surface. Use above
5
contour example.
plot
Surface Surf1(X,Y,Z) Use above example.
6 plot with
lighting
Waterfall Waterfall(X,Y, Draws a mesh in one
7 plot Z) direction only. Use above
example.
3D contour Contour3(X,Y, Where n is the number of
8 Z,n) contour levels. Use above
example
2D contour Contour(X,Y,Z Where n is the number of
9 ,n) contour levels. Use above
example

Plot with special graphics

Sr.No Plot type Syntax Example


sphere Sphere(n) [X,Y,Z]=sphere(20);
Returns the x, surf(X,Y,Z)
1 y, z coordinates
of a unit sphere
with 20faces
cylinder Cylinder(r) t = linspace(0, pi,20);
Returns the x, r=1+sin(t);
2 y, z coordinates [X,Y,Z]=cylinder(r);
of a cylinder surf(X,Y,Z)
with profile r axis square
3D bar plot Bar3(Y) Y = [1 6.5 7;2 6 7; 3
Each element in 5.5 7; 4 5 7;3 4 7;2 3
Y is one bar. 7;1 2 7];
3
columns are Bar3(Y)
grouped
together
3D stem Stem3(X,Y,Z) t =0:0.2:10
Draws x=t;
sequencetial y=sin(t)
points with z=t .^1.5;
4
markers and stem(x,y,z,‟fill‟)
vertical lines grid on
from the X-Y
plane
3D scatter Scatter3(X,Y,Z) t =0:0.2:10
5
Removes x=t;

xviii
MATLAB awareness program MAP

vertical lines y=sin(t)


from stem plot. z=t .^1.5;
scatter3(x,y,z,‟filled‟)
grid on
3D pie Pie3(X, X= [5,9,14,20];
6 explode) explode =[0 0 1 0];
Plots pie charts 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

xix
MATLAB awareness program MAP

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)

xx
MATLAB awareness program MAP

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.

xxi

You might also like