Lab Manual 03
Lab Manual 03
Lab Manual No 03
Dated:
28th August, 2023 to 03th September, 2023
Semester:
Autumn 2023
3-1 Variable
MATLAB does not require any type declarations or dimension statements. When
MATLAB encounters a new variable name, it automatically creates the variable and allocates
the appropriate amount of storage. If the variable already exists, MATLAB changes its contents
and, if necessary, allocates new storage. For example,
num_students = 25
creates a 1-by-1 matrix named num_students and stores the value 25 in its
single element.
Variable names consist of a letter, followed by any number of letters, digits, or
underscores. MATLAB uses only the first 31 characters of a variable name. MATLAB is
case sensitive; it distinguishes between uppercase and lowercase letters. A and a are not the
same variable.
3-2 Numbers
MATLAB uses conventional decimal notation, with an optional decimal point and
leading plus or minus sign, for numbers. Scientific notation uses the letter ( e ) to specify a
power-of-ten scale factor. Imaginary numbers use either i or j as a suffix. Some examples of
legal numbers are
3 -99 0.0001 9.6397238 1.60210e-20
6.02252e23 1i 3+5j
3-3 Arithmetic Operators
Operator Description
+ Plus
- Minus
* Matrix multiply
.* Array multiply
^ Matrix power
.^ Array power
\ Backslash or left matrix divide
/ Slash or right matrix divide
.\ Left array divide
./ Right array divide
() Specify evaluation order
3-4 Function
Some of the functions, like sqrt and sin, are built in. They are part of the MATLAB core
so they are very efficient, but the computational details are not readily accessible. Other
Application in Engineering Design Lab Instructor:-Adnan Mustafa
Session:-2023
:-2023 Computer Computer
functions, like gamma and sinh, are implemented in M-files. You can see the code and even
modify it if you want.
Command Description
abs(x) Absolute value (magnitude of complex number).
acos(x) Inverse cosine.
angle(x) Phase angle (angle of complex number).
asin(x) Inverse sine.
atan(x) Inverse tangent.
atan2(x,y) Four quadrant inverse tangent: tan-1 (x / y).
ceil(x) Round towards plus infinity.
conj(x) Complex conjugate.
cos(x) Cosine of x, assumes radians.
exp(x) Exponential: e x.
fix(x) Round towards zero.
floor(x) Round towards minus infinity.
imag(x) Complex imaginary part.
log(x) Natural logarithm: ln(x).
log10(x) Common (base 10) logarithm: log10(x).
log2(x) Base 2 logarithm: log2(x).
real(x) Complex real part.
rem(x) Remainder after division.
mod(x) Modulus after division.
round(x) Round towards nearest integer.
sign(x) Signum: return sign of argument.
sin(x) Sine of x, assumes radians.
sqrt(x) Square root.
tan(x) Tangent of x, assumes radians.
rand(n) returns an N-by-N matrix containing pseudorandomvalues
drawn from the standard uniform distribution on the open
interval(0,1). rand(M,N) returns an M-by-N
matrix. rand returns a scalar.
randn(n) returns an N-by-N matrix containing pseudorandom
values drawn from the standard normal distribution on
the open interval(0,1). randn(M,N) returns an M-by-N
matrix. randn returns a scalar.
Constant Description
eps Floating point relative accuracy ≈ 2.2204e-016.
realmax Largest positive floating point number.
realmin Smallest positive floating point number.
pi 3.1415926535897....
i and j Imaginary unit 1 .
inf Infinity, e.g. 1/0
NaN Not A Number, e.g. 0/0
1) >> x = (1+sqrt(5))/2
x =
1.6180
>> a = abs(3+4i)
a =
5
>> y=sin(pi/3)+cos(pi/4)-2*sqrt(3)
y =
-1.8910
2) Solve the following system
x+y=1
x-y+z=0
x+y+z=2
Solution
1- Write a MATLAB program to calculate the following expression and round the
answers to the nearest integer.
x + y - 2z = 3
2x + y = 7
x+y-z=4
3- Use [ round, fix, ceil, floor ] commands to round the following numbers towards integer
numbers:
Before After
1.3 1
1.5 1
1.9 2
11.9 11
-2.9 -2
-3.9 -4
3.4 3
4- Write a Program to calculate the electromagnetic force between two electrons placed
(in vacuum) at a distance ( r = 2*10-13 m ) from each other. Charge of electron (Q) is
1.6*10-19 C.
Hint
Q1Q2
Electromagnetic Force K
r2
K=9*109
5- Generate ten values from the uniform distribution on the interval [2, 3.5].
2