0% found this document useful (0 votes)
16 views

Lab 2 MATLAB

Uploaded by

mohammed rasheed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Lab 2 MATLAB

Uploaded by

mohammed rasheed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Islamic University ‫الجامعة اإلسالمية‬

Faculty of Engineering
Department of Electrical
‫كلية الهندسة‬
Engineering ‫قسم الهندسة الكهربائية‬

ENGR 3032

SCIENTIFIC COMPUTING LANGUAGES


Fall Semester 2024 (Term 461)

LAB 2

Problem Solving using scientific computing languages:


Trigonometric functions, Complex and Random numbers
[CO 2; PI_6_3; SO 6]

Submitted To
Eng. Osamah Alkhalifah

Submitted By
Mohammed Rasheed
431014033

Section No:…2623…

September 16, 2024


ENGR 3032 Page 1 of 20
Task Grade Comments
Task 1 /10
Task 2 /20
Task 3 /20
Task 4 /10
Task 5 /30
Conclusion /10
Total /100

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

Figure 1 Vector creation and evaluation of sine function.


✓ Vector Addressing and Colon Notation

ENGR 3032 Page 2 of 20


Using the MATLAB / OCTAVE, picking a specific element or elements in a vector (or vector
addressing) can be achieved as shown in figure 2

Figure 2. Select an element or a sub group in a vector (addressing).

✓ Combining, Changing, and extending a vector

Using the MATLAB / OCTAVE, a Vector can be combined, changed, and extended as
shown in figure 3

Figure 3. Combining, Changing and extending a vector

ENGR 3032 Page 3 of 20


✓ Linspace:

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)

Figure 4. Linspace and Logspace

ENGR 3032 Page 4 of 20


2. Elementary MATHS functions
Elementary math functions include logarithms, exponentials, absolute value, rounding
functions, and functions used in discrete mathematics.

Table 2.1.: Common Math Functions

➢ 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

ENGR 3032 Page 5 of 20


gives the same result as
5*4*3*2*1
ans =
120
The value of a factorial quickly becomes very large. Ten factorial is 3,628,800. MATLAB /
OCTAVE can handle up to 170!

Table 2.1.2: Functions Used in Discrete Mathematics

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/)

ENGR 3032 Page 6 of 20


MATLAB / OCTAVE Code:

degrees = radians * 180/pi;


radians = degrees * pi/180;

Table 2.2: Trigonometric Functions

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.

ENGR 3032 Page 7 of 20


Table 2.3: Random-Number Generators

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

ENGR 3032 Page 8 of 20


Table 2.4: Functions Used with Complex Numbers

ENGR 3032 Page 9 of 20


Lab Work
Task 1

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.

ENGR 3032 Page 10 of 20


ENGR 3032 Page 11 of 20
Task 2

Q1. Create a vector x from -2 to +2 with an increment of 1. Your vector should be

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.

Q2. Find the square root of both -3 and +3.


i. Use the sqrt function.
ii. Use the nthroot function. (You should get an error statement for -3)

ENGR 3032 Page 12 of 20


iii. Raise -3 and +3 to the ½ power.

How do the results vary?


For positive numbers like `+3`, all methods (`sqrt`, `nthroot`, and raising to `1/2`)
return the same result, approximately `1.7321`. For negative numbers like `-3`, the
`sqrt` function and raising to `1/2` return a complex number (`0 + 1.7321i`), while the
`nthroot` function returns `NaN` because it only computes real roots and cannot
handle the square root of negative numbers.

ENGR 3032 Page 13 of 20


Q3. Create a vector x from -9 to 12 with an increment of 3.
i. Find the result of x divided by 2.
ii. Find the remainder of x divided by 2.

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

ENGR 3032 Page 14 of 20


ciphers.) (Hint: Use the primes function and the length command.)

Task 3

Q1. Sin (2ϴ)for ϴ = 3π/2.

ENGR 3032 Page 15 of 20


Q2. Cos (ϴ) for 0 ≤ ϴ ≤ 2 π; let ϴ change in steps of 0.2 π.

Q3. Sin-1 (1).

ENGR 3032 Page 16 of 20


Q4. Cos-1 (x) for -1 ≤ x≤ 1; let x change in steps of 0.2.

Task 4

Q1. Create a 3 x 3 matrix of evenly distributed random numbers.

ENGR 3032 Page 17 of 20


Q2. Create a 3 x 3 matrix of normally distributed random numbers.

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

ENGR 3032 Page 18 of 20


ENGR 3032 Page 19 of 20
Conclusion (What did you understood from this Lab session?)

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.

ENGR 3032 Page 20 of 20

You might also like