Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
26 views

Lab Manual 03

Matlab Manual 3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Lab Manual 03

Matlab Manual 3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING


COMPUTER ENGINEERING DEPARTMENT

Computer Application in Engineering Design

Lab Manual No 03

Variables, Numbers, Operators and Functions in MATLAB

Dated:
28th August, 2023 to 03th September, 2023

Semester:
Autumn 2023

Application in Engineering Design Lab Instructor:-Adnan Mustafa


Session:-2023
:-2023 Computer Computer
Like most other programming languages, MATLAB provides mathematical expressions,
but unlike most programming languages, these expressions involve entire matrices. The
building blocks of expressions are:

1- Variable 2- Numbers 3- Operators 4- Functions

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

MATLAB provides a large number of standard elementary mathematical functions,


including abs, sqrt, exp, and sin. Taking the square root or logarithm of a negative number is
not an error; the appropriate complex result is produced automatically. MATLAB also provides
many more advanced mathematical functions, including Bessel and gamma functions. Most of
these functions accept complex arguments. For a list of the elementary mathematical functions,
type
>> help elfun

For a list of more advanced mathematical and matrix functions, type


>> help specfun
>> help elmat

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.

Several special functions provide values of useful constants.

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

3-5 Examples of Expressions

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

>> a=[1 1 0; 1 -1 1; 1 1 1]; b=[1;0;2];


>> x=inv(a)*b
or
>> x=a\b
Exercises

1- Write a MATLAB program to calculate the following expression and round the
answers to the nearest integer.

a) z  5x2  y 2 where x=2, y=4

b) z  4cos(x)  j6sin(x) where x=π/4

c) z  3sin(x)  4cos(x)  3ey where x=π/3 , y=2


d) y sin(x) / x where 0 ≤ x ≤ 2π

2- Solve the following system

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

You might also like