Lab2 - Task1 - Sultan Momar Alshalabi - 422039543

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

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

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

ENGR 3032

SCIENTIFIC COMPUTING LANGUAGES


Term 443 2023

LAB 2

Problem Solving using scientific computing languages:


Trigonometric functions, Complex and Random numbers
[CO 2; PI_1_3; SO 1]

Submitted To
Dr. Abdulmajeed Alenezi

Submitted By
Sultan Momar Alshalabi
422039543

Section No: 2068

ENGR 3032 Term 443 2023 Page 1 of 9


Lab Work
Task 1

Q1. Create a matrix named m of evenly spaced values from 0 to 10, with an increment of
1/10.
Answer: m=0:1/10:10

Result:

Q2. Use the linspace function to create a matrix of six evenly spaced values from 10 to 20.

Answer: A=linspace(10,20,6)

ENGR 3032 Term 443 2023 Page 2 of 9


Result: A = 10 12 14 16 18 20

Q3. Use the logspace function to create a matrix of five logarithmically spaced values
between 10 and 100.
Answer: B=logspace(10,100,5)

Result:
B = 1.0000e+10 3.1623e+32 1.0000e+55 3.1623e+77 1.0000e+100

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]
Answer: X=(-2:1:2)

Result: X = -2 -1 0 1 2

i. Find the absolute value of each member of the vector.


Answer: Y= abs(X)

Result: Y = 2 1 0 1 2

ENGR 3032 Term 443 2023 Page 3 of 9


ii. Find the square root of each member of the vector.
Answer: Z= sqrt(X)

Result: Z = 0 + 1.4142i 0 + 1.0000i 0 + 0i 1.0000 + 0i 1.4142 + 0i

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)
iii. Raise -3 and +3 to the ½ power.
Answer:
Q= sqrt(-3)
Q = 0 + 1.7321i

QQ= sqrt(3)
QQ = 1.7321

W= nthroot(-3,2)
error: nthroot: N must be an odd integer if X contains negative values

H= nthroot(3,2)
H = 1.7321

C=-3^(1/2)
C = -1.7321

D= 3^(1/2)
D = 1.7321

How do the results vary?


1 square root
2 roots
3/ power (exp)
Nevertheless, we discovered that whereas nthroot exclusively works with positive integers,
sqrt and power can handle both positive and negative values.
ENGR 3032 Term 443 2023 Page 4 of 9
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.
Answer:
E= [-9:3:12]
F= E/2
J= rem(E,2)
E = -9 -6 -3 0 3 6 9 12

F = -4.5000 -3.0000 -1.5000 0 1.5000 3.0000 4.5000 6.0000

J = -1 0 -1 0 1 0 1 0

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.
Answer: P= nchoosek(52,5)
Result: P = 2598960

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
ciphers.) (Hint: Use the primes function and the length command.)
Answer:
U= primes(20000);
Y=primes(10000);
G=length(U)-length(Y) %length of primes numbers between U and Y

ENGR 3032 Term 443 2023 Page 5 of 9


Task 3

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


Q2. Cos (ϴ) for 0 ≤ ϴ ≤ 2 π; let ϴ change in steps of 0.2 π.
Q3. Sin-1 (1).
Q4. Cos-1 (x) for -1 ≤ x ≤ 1; let x change in steps of 0.2.

Answer:
%Task 3
thetah=3*pi/2;
V= sin(2*thetah)
kk=[0:0.2*pi:2*pi];
Q2= cos(kk)
Q3= asin(1)
RT= [-1:0.2:1];
RR= acos(RT)

Result:
V = 3.6738e-16
Q2 = 1.0000 0.8090 0.3090 -0.3090 -0.8090 -1.0000 -0.8090 -0.3090 0.3090 0.8090
1.0000
Q3 = 1.5708
RR = 3.1416 2.4981 2.2143 1.9823 1.7722 1.5708 1.3694 1.1593 0.9273 0.6435
0

ENGR 3032 Term 443 2023 Page 6 of 9


Task 4

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


Q2. Create a 3 x 3 matrix of normally distributed random numbers.
Answer:
%Task 4
EV= rand(3,3)
ND= randn(3)

Result:
EV =
0.956925 0.450455 0.335663
0.079911 0.539150 0.157638
0.823759 0.897506 0.783992
ND =
-0.3863 -1.6478 0.8768
0.4245 -2.1568 1.8802
2.3864 -0.2946 -0.8054

ENGR 3032 Term 443 2023 Page 7 of 9


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

Answer:
%Task 5
AS = 1 + i;
BS = 2 + 3i;
CS = 8 + 2i;
AA= abs(AS)
NA= angle(AS)
AB= abs(BS)
NB= angle(BS)
AC= abs(CS)
NC= angle(CS)

Result:
AA = 1.4142
NA = 0.7854
AB = 3.6056
NB = 0.9828
AC = 8.2462
NC = 0.2450

ENGR 3032 Term 443 2023 Page 8 of 9


Conclusion (What did you understood from this Lab session?)

• how to add a specified amount to an array.


• The correct usage of the linspace and logspace functions.
Sqrt, nthroot, and power functions are employed.
• How to generate uniformly or randomly dispersed arrays and how to locate
reminders.
• The process for determining a number's absolute angles.

ENGR 3032 Term 443 2023 Page 9 of 9

You might also like