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

امتحان مختبر سيطرة

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 9

University of Technology Control Laboratory

Electromechanical Engineering Department 3rd year


Electromechanical Systems Engineering Branch

Experiment No.(2)
Expressions
Theory:
Like most other programming languages, MATLAB provides
mathematical expression, but unlike most programming languages, these
expressing involve entire matrices, the building blocks of expressions are:
1. Variables.
2. Numbers.
3. Operators.
4. Functions.

2-1 Variables:
A variable name must comply with following two rules:
1. It may consist only of the letters (a to z), the digits (0 to 9), and the
underscore ( _ ).
2. It must start with a letter.
A variable name may as long as you like, but MATLAB only remembers the
first 31 characters.
Example of valid variable names: r2d2 pay_day.
Example of invalid variable names: 2a pay-day name$ -2a.
A variable is created simply by assigning a value to it, e.g.
a= 98
If you attempt to refer to a non-existent variable you may get the error
message (Undefined function or variable).
Enter two correct and uncorrected variable consist 4 characters.

9-2 Numbers:
Numbers can be represented in MATLAB in the usual decimal form (fixed
point), with an optional decimal point, e.g.
1.2345 -165 .0001
A number may also represent in scientific notation, e.g. 1.2345 × 10^9 may
be represented in MATLAB as 1.2345e9. This is called floating point
notation. On computers using standard floating point arithmetic, numbers are
represented to approximately 16 significant decimal digits. The relative
accuracy of numbers is given by the function eps, which is defined as the
distance between 1.0 and the next largest number. Enter eps to see its value
on your computer.
It is very easy to handle complex numbers in MATLAB. The special value I
and j stands for sqrt (-1), e.g.
Z=2+3i
Represents the complex number 2+3i (real part 2, imaginary part 3). Some
examples of legal numbers are:
3 -99 0.0001 1.60210e-20 -3.14159
2-3 Arithmetic Operators:
Operator's expressions use familiar arithmetic operators and precedence
rules.
Table (2-1) the arithmetic operators and precedence rules
+ ADDITION
- Subtraction
* Multiplication
/ Division
\ Left division
^ Power
' Complex conjugate transpose
() Specify evaluation order
2-4 Function:
MATLAB provides a large number of standard elementary mathematical
function, 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 many more advanced mathematical functions including
Bessel and Gamma functions. Most of these functions accept complex
arguments. For a list of 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
MATLAB core so they are very efficient, but the computational details are
not readily accessible. Other functions, like gamma and sinh, are
implemented in M-files.
Table(2-2) Several special functions provide values of useful constants.
CONSTANT DESCRIPTION
esp. Floating point relative accuracy ≈ 2.2204e-016
pi 3.1415926535897….
inf Infinity, e.g. 1/0
Nan Not-a-number e.g. 0/0
i Imaginary unit
j Same as i
realmax Largest positive floating point number
realmin Smallest positive floating point number
Write this equation using Matlab.
2
y= x^2 – x + sin x
3
1- Find the value of y.
2- Find the absolute value of y.
3- Find real and imaginary value of y.
4
If X = 0, 4, -8, ,5.8
5
Discussion:

1. State, giving reasons, which of the following are not valid


MATLAB variable names:
(a). `a`one (b). min*2 (c). info_ (d). _x_1 (e). pay_day

2. Decide which of the following numbers are not acceptable in


MATLAB and state why not:
(a). 9, 87 (b). -356231 (c). 3.57e-2 (d). 3,57e-7 (e). .0

3. Translate the following expressions into MATLAB:


yz
(a) y
w
(b) p+
u
y z
(c) ( x )
4. write a MATLAB program to calculate the following expression:

(a) z= 5x 2  y 2 , where x=2, y=4

(b) z= 4cos(x) + j6sin(x) , where x=  /4

5. write a program to calculate x, where:

 b  b 2  4ac
x=
2a
University of Technology
Electromechanical Engineering Department Control Laboratory
Electromechanical Systems Engineering Branch 3rd year

Experiment No.(3)
Dimensional Array

3-1 Simple Arrays:


To create an array in MATLAB, all you have to do is to start with a left
bracket, enter the desired values separated by spaces or commas, and then
close the array with aright bracket.
Arrays can be entered in several different ways:
- Entered by an explicit list of elements,
- Generated by built-in statements and functions,
- Created in M-files,
- Loaded from external data files
The following session is self explanatory, study it in detail.
x= [2 6 12]
x=
2 6 12
y= [3 5 1]
y=
351
X=x.*y
ans =
6 30 12

Example(1):
If y= sin(x) , 0 ≤ x ≤ pi , compute the values of sin function over one-half
of its period.

>> x=[0 .1*pi .2*pi .3*pi .4*pi .5*pi .6*pi .7*pi .8*pi .9*pi pi]
x=
Columns 1 through 10
0 0.3142 0.6283 0.9425 1.2566 1.5708 1.8850 2.1991
2.5133 2.8274
Column 11
3.1416
>> y=sin(x)
y=
Columns 1 through 10
0 0.3090 0.5878 0.8090 0.9511 1.0000 0.9511 0.8090
0.5878 0.3090
Column 11
0.0000
3-2 Array addressing:
In MATLAB, the elements of an array are identified by their index using
subscripts, e.g., x (1)
Is the first element in x, x (6) is the sixth element in x, and so on.
Example(2):
>> x (3)
ans =
0.6283
>> x (end)
ans =
3.1416
Here MATLAB, function linspace is used cerate x.This function is described
by:
Linspace (first value, last value, number of values).

3-3 Array Orientation:


●Row vector: An array that contains one row and multiple columns.
● Column vector: An array that contains one column and multiple rows.
One way to create column vector is to specify it element by element,
separating values with semicolons (;)

>> c= [1; 2; 3; 4; 5]
c=
1
2
3
4
5
A row vector can be converted into a column, and vice versa, by
transposition. This operation is performed in MATLAB by the apostrophe
(‘).
>> b=c'
b=
1 2 3 4 5

3-4 Scalar array mathematics:


Addition, subtraction, multiplication, and division by a scalar simply apply
the operation to all the elements of the array:
>> p= [2 3 4 7 11 13];
>> p-2
ans =
0 1 2 5 9 11
>> 2*p-1
ans =
3 5 7 13 21 25
Table (10-1) Element by Element Array Operations
ELEMENT BY ELEMENT ARRAY OPERATIONS
Operation Algebraic Form MATLAB
Addition ai + bi a+b
Subtraction ai - bi a-b
Multiplication ai* bi a. * b
Right division ai/bi a./b
Left division bi ai a b
bi
Power a a.^b

3-5 Array – Array Mathematics:


When tow arrays have the same length, addition, subtraction, multiplication
and division, to all the elements of the array:
>> p= [2 3 5 7 11 13];
>> n= [1 2 3 4 5 6];
>> p+n
ans =
3 5 8 11 16 19
> p-n
ans =
1 1 2 3 6 7
For the arrays p and n used above, their array multiplication, indicated in
MATLAB by (.*)
> p.*n
ans =
2 6 15 28 55 78

Array division, or dot division, also requires use of the dot symbol:
> p./n

ans =

2.0000 1.5000 1.6667 1.7500 2.2000 2.1667

Try to enter array below a described and writ some statements to find the
sum, difference, product and quotient of a and b:

A = 4 -6 2.6 5
B = 13 9 -4 -1.2
Discussion:
1- If a = 1:5, b = 1:2:9, create an array c composed of elements of b
followed by those of a.
2. For the same values of a and b mentioned above, create an array d
composed of the first, third, and fifth element of a followed by three
additional elements.
3- Generate the following row vector: b = [1, 2, 3, 4, 5….9, 10], then
transpose it to column vector.
4- If x= [2 3 -1], then
a- Add 1 to each element of x.
b- Multiply each element of x by 3.
5- Apply addition, subtraction, multiplication, and division operation on the
following vectors, x = [4 8 10 12], y =[2 4 5 6].

You might also like