امتحان مختبر سيطرة
امتحان مختبر سيطرة
امتحان مختبر سيطرة
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:
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
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).
>> 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
Array division, or dot division, also requires use of the dot symbol:
> p./n
ans =
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].