Вы находитесь на странице: 1из 6

2 LAB#2

IMPLEMENTATION OF CONSTANTS, VARIABLES


AND EXPRESSIONS
The learning objectives are:

• Knowledge of MATLAB character set and Data types.


• Study of constants and variables and related useful functions.
• Study of functions used for conversion of Co-ordinate systems.
• Study of arithmetic, relational and logical operators.
• Knowledge of hierarchy of operations and built-in functions.  Use of assignment statement.
2.1 Introduction
MATLAB is a powerful computing language. Programming languages process data consisting of
numbers, characters and strings and provide output, known as useful information. Data
processing is carried out by executing a sequence of instructions called a computer program. This
group of instructions or program is written by the user, using certain symbols and words
according to syntax rules of a particular language. MATLAB also has its vocabulary and
grammar. In this Lab, concepts of character set, constants, variables, data types, operators and
expressions are discussed.
2.2 Character Set
It is a set of characters that form a part of the statements written using the programming
language. Characters can be broadly classified into four categories:
• Alphabets
• Numerals
• Special characters
• White space characters
MATLAB recognizes all 26 alphabets of English language. It is case-sensitive that is, upper-case
letters and lower-case letters are recognized as different characters. Numerals 0 to 9 are
recognized in MATLAB special characters.
White space characters like tab, blank, new line, vertical tab and line-feed are also included in
the character set.
2.3 Data Types
Data types are in form of array. This array is a minimum single element in size and can grow to
an ndimensional array of any size. Two dimensional version of these arrays are called Matrices.
2.3.1 Character Data Types
Alphabets or words e.g. MATLAB is a character array. Array of strings (alphabets)
can be used to store number of strings, provided all of them have the same length
that is 16 bits long.
2.3.2 Numeric Data Types
Single data type e.g. int8, int16, int32 and int64 as well as double data type e.g. uint8, uint16,
uint32, and uint64 includes signed and unsigned integer arrays that are 8, 16, 32, 64 bits in
length respectively.
2.4 Constants and Variables
Constants
Constants refer to find values which donot change during the execution of a program.
Constants can be of two types:
Numeric Constants

Numeric constants are of three types. That are Integer constants, Real constants and Complex
constants. MATLAB does not differentiate between Integer and Real constants during
execution.
2.5 Commands Related To Complex Numbers
Let x is defined as: 𝑥 = 𝑎 + 𝑏𝑖
Sr. # Operations Description

1 𝑟𝑒𝑎𝑙(𝑥) Give real part of x

2 𝑖𝑚𝑎𝑔(𝑥) Give imaginary part of x

3 𝑐𝑜𝑛𝑗(𝑥) Produce conjugate of x , i.e 𝑥 = 𝑎 − 𝑏𝑖

4 𝑎𝑏𝑠(𝑥) Use to obtain magnitude of complex number

5 𝑎𝑛𝑔𝑙𝑒(𝑥) Use to obtain angle of complex numbers in radians (𝜃 = tan−1 𝑦)


𝑥

By default MATLAB accepts and return angle in radians.


2.6 Conversions of Coordinate System:
Conversion Command

Cartesian To Polar [theta,r]=cart2pol(x,y)


(𝑥, 𝑦) → (𝑟, 𝜃) e.g. >>x=1; y=1;
>>[theta,r]=cart2pol(1,1)
>>theta=0.7854 r
=1.4142
Polar To Cartesian [x,y]=pol2cart(theta,r)
(𝑟, 𝜃) → (𝑥, 𝑦) e.g. >>theta=1, r=5;
>>[x,y] = pol2cart(1,5)
>>x=2.7015 y=4.2074

2.7 Character Constant:


Character constants are of following types
• Single Character constants
• String Constants
• Escape Sequence Constants
2.7.1 Single Character Constants
A single character constant contains a single character enclosed with a pair of single quote
marks. Some examples are valid single character constants are ‘y’,’g’,’1’,’_’,’ ’. It may be noted
that space is also a character.
2.7.2 String Constants:
A string constant is a sequence of characters enclosed in pair of single quotes. The characters
may be alphabets, numbers, special characters or blank space. e.g. ‘GOOD’ ‘1947’ ‘BEST
WISHES’. A word containing a single quote is specified by using two single quote together e.g.
isn’t written as ‘isn’t’.
2.7.3 Escape Sequence Constants
Escape Sequence character constants are used in output functions. For example
\b Backspace
\t Tab
\n New line
\r Carriage return
\f Form feed
\\ Print back slash
\’ Print apostrophe
%% Print % or create a cell
2.8 Variables
Variables and constants form an integral form of program in any language. The different types of
data are identified by their names for case in reference. Programming languages require a
programmer to declare these variables and the type of data in advance at the beginning of
program, whereas no such declaration is required in MATLAB.
• Blank spaces cannot be included in MATLAB variables names.
• MATLAB is case-sensitive. Lower-case and upper-case latters represent two different
variables names, for example “GOLD” and “gold” are different variable names.
• Words that are reserved for the key constructs of a programming language are called
“KEYWORDS” and cannot be used as variable names. For example: for, while, switch
and functions are keywords.


2.9 Special Constants & Variables
Pi 3.141519

i/j

Inf ∞

Ans Default output variables

2.10 Built-in Functions


The functions which are already available in MATLAB are called built-in functions. A complete
list of all MATLAB functions can be seen through Help browser. General Syntax for using these
functions is:
Function_name (expression)
Built-in function Description

exp(x) Calculate 𝑒𝑥

log(x) Calculate Natural logarithm

factorial(x) Calculate Factorial of a number

sin(x) Compute the sine of ‘x’ in radians

cos(x) Compute the cosine of ‘x’ in radians

tan(x) Compute the tangent of ‘x’ in radians

csc(x) Compute the cosecant of ‘x’ in radians

sec(x) Compute the secant of ‘x’ in radians


cot(x) Compute the cotangent of ‘x’ in radians

sind(x),cosd(x) Compute angles in degree


tand(x),cscd(x)
secd(x),cotd(x)

asin(x) Compute the inverse sine of ‘x’ where ‘x’ must between -1 and 1. The function
−𝜋 𝜋
returns an angle in radians between ⁄2 and ⁄2

acos(x) Compute the inverse cosine of ‘x’ where ‘x’ must between -1 and 1. The function
returns an angle in radians between 0 and π

atan(x) Compute the inverse tangent of ‘x’. The function returns an angle in radians
−𝜋 𝜋
between ⁄2 and ⁄2

atan2(y,x) 𝑦
Compute the inverse tangent of value ⁄𝑥 the function returns an angle in radians
that will be between –π and π depending on signs of ‘x’ & ‘y’.

sinh(x) 𝑒𝑥−𝑒−𝑥
Compute the hyperbolic sine of ’x’ which is equal to
2
cosh(x) 𝑒𝑥+𝑒−𝑥
Compute the hyperbolic cosine of ’x’ which is equal to
2
tanh(x) 𝑒𝑥−𝑒−𝑥
Compute the hyperbolic tangent of ’x’ which is equal to 𝑥+𝑒−𝑥
𝑒
asinh(x) Compute the inverse hyperbolic sine of ’x’ which is equal to

acosh(x) Compute the inverse hyperbolic cosine of ’x’ which is equal to

atanh(x) Compute the inverse hyperbolic tangent of ’x’ which is

1+ 𝑥 equal
ln √ 1−
to for |x|
≤1
𝑥
2.11 Element by element operation
We use element by element operation, when we have expressions in fractional forms.
Operations Operators Algebraic form MATLAB form

Element by element multiplication .* 𝑝×𝑞 p.*q

Element by element division ./ 𝑝/𝑞 p./q

Element by element exponentiation .^ 𝑝𝑞 p.^q

Вам также может понравиться