Lab 2 MATLAB
Lab 2 MATLAB
Faculty of Engineering
Department of Electrical
كلية الهندسة
Engineering قسم الهندسة الكهربائية
ENGR 3032
LAB 2
Submitted To
Eng. Osamah Alkhalifah
Submitted By
Mohammed Rasheed
431014033
Section No:…2623…
Objective:
The objective of this Laboratory session is to learn how to use different mathematical built-in
functions to perform calculations including trigonometric functions, complex and random
numbers.
1. Arrays:
Variables that represent more than one number. Each number → element of the array. Rather
than performing the same operation on one number at a time, array operations allow operating
on multiple numbers at once. A row of numbers (called a row vector) or a column of numbers
(called a column vector).
✓ Vector Creation
Example: Using MATLAB / OCTAVE, we wish to calculate the sine values for a vector of 11
values
Using the MATLAB / OCTAVE, a Vector can be combined, changed, and extended as
shown in figure 3
This function generates a vector of uniformly incremented values, but instead of specifying the
increment, the number of values desired is specified as shown in figure 4.
linspace (start, end, number of desired points)
The increment is computed internally, having the value:
→Increment = [end − start]/ [ numberof desired points− 1]
✓ Logspace:
This function generates logarithmically spaced values and has the form,
→logspace (start_exponent, end_exponent, number)
➢ Discrete Mathematics
MATLAB / OCTAVE includes functions to calculate the factorial numbers, find common
denominators and multiples, calculate factorials, and explore prime numbers (Table 2.1.2). All
these functions require integer scalars as input. A factorial is the product of all the positive
integers from 1 to a given value. Thus 3 factorial (indicated as 3!) is 3 *2 *1 =6. Many problems
involving probability can be solved with factorials. For example, the number of ways that five
cards can be arranged is
5 *4*3*2 *1 = 5! = 120.
To calculate 5! in MATLAB / OCTAVE use the factorial function. Thus factorial(5)
ans =
120
3. Trigonometric Functions
MATLAB / OCTAVE includes a complete set of the standard trigonometric functions and
the hyperbolic trigonometric functions. Most of these functions assume that angles are
expressed in radians. To convert radians to degrees or degrees to radians, you need to take
advantage of the fact that π radians equals 180: degrees = radians *(180/)
4. Random Numbers
Random numbers are often used in engineering calculations to simulate measured data.
Measured data rarely behave exactly as predicted by mathematical models, so you can add
small values of random numbers to your predictions to make a model behave more like a real
system. Random numbers are also used to model games of chance.
5. Complex Numbers
MATLAB / OCTAVE includes several functions used primarily with complex numbers.
Complex numbers consist of two parts: a real and an imaginary component.
For example,
5 + 3i
is a complex number. The real component is 5, and the imaginary component is 3.
Complex numbers can be entered into MATLAB / OCTAVE in three ways: as an
addition problem, such as:
A = 5 + 3i
or
A = 5+3*i
or with the complex function, as in
A = complex (5,3)
which returns
A =
5.0000 + 3.0000i
Q1. Create a matrix named mof evenly spaced values from 0 to 10, with an increment of 1/10.
Q2. Use the linspace function to create a matrix of six evenly spaced values from 10 to 20.
Q3. Use the logspace function to create a matrix of five logarithmically spaced values
between 10 and 100.
x = [-2, -1, 0, 1, 2]
i. Find the absolute value of each member of the vector.
ii. Find the square root of each member of the vector.
Q4.There are 52 different cards in a deck. How many different hands of 5 cards each are
possible? Remember, every hand can be arranged 120 (5!) different ways.
Q5.Very large prime numbers are used in cryptography. How many prime numbers are
there between 10,000 and 20,000? (These aren’t big enough primes to be useful in
Task 3
Task 4
Task 5
Write an m-file to find the absolute value and the angle of each of the following vectors
(complex numbers). The m-file should be well formatted with sufficient comments (and
explanations) in the header. Attach the print-out of the screen shot of your m-file with output
results.
a. A = 1 + i
b. B = 2 + 3i
c. C = 8 + 2i
Through completing these MATLAB tasks, we learned how to create and work with vectors
and matrices, use mathematical functions like sine and cosine, handle complex numbers,
generate random numbers, and write simple scripts. These exercises helped us understand the
basics of MATLAB and showed how it can be used for various calculations and data analysis.
Overall, this practice built a strong foundation for using MATLAB in future projects.