MATLAB - Chp-3 Ex (Redit)

You might also like

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

1.

### Method 1 (Matrix Multiplication):


u = [4, 9, -5];
v = [-3; 6; -7];
dotProductMatrix = u * v
### Method 2 (Using the dot function):
u = [4, 9, -5];
v = [-3, 6, -7];
dotProduct = dot(u, v);

2.
x = [-2.5, -2, -1.5, -1, -0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3];
y = x.^3 .* (x.^2 + 1).^3;
---------------------------------Output------------------------------------------
Values of x:
-2.5000 -2.0000 -1.5000 -1.0000 -0.5000 0 0.5000 1.0000 1.5000
2.0000 2.5000 3.0000

Values of y:
275.6094 108.0000 46.8750 16.0000 3.6094 0.0000 3.6094 16.0000
46.8750 108.0000 275.6094 559.0000

3.
g = 9.81;
t = 1:10;
d = 0.5 * g * t.^2
4.
x = [2, 4, 6, 8, 10];
y = [3, 6, 9, 12, 15];
z = (x.*y + y./x) ./ (x + y).^(y - x) + 12.^(x./y)
---------------------------------Output------------------------------------------

Values of z:
33.3929 267.0056 587.6491 1282.0287 2847.5522

5.
h = 0.9;
k = 12.5;
x = [1, 2, 3, 4];
y = [0.9, 0.8, 0.7, 0.6];
z = [2.5, 3, 3.5, 4];
T = (x .* y .* z) ./ ((h + k).^(k/5)) + (k .* exp((z ./ x) + y)) ./ (z.^h)
------------------------------------Output-------------------------------------
[164.20043282 46.39237005 26.18890142
17.79441838]

6.
n = [1, 10, 100, 500, 1000, 2000, 4000, 8000];
y = (1 + 1 ./ n) .^ n
e_value = exp(1);
absolute_difference = abs(y - e_value)

7.

% Part a: n = 100
n_a = 1:100;
sum_a = sum(1 ./ n_a.^2)
approximation_a = pi^2 / 6

% Part b: n = 1000
n_b = 1:1000;
sum_b = sum(1 ./ n_b.^2)
approximation_b = pi^2 / 6

% Part c: n = 10000
n_c = 1:10000;
sum_c = sum(1 ./ n_c.^2)
approximation_c = pi^2 / 6
-------------------------------Output------------------------------------------

n = 100:
1.6439340668482264

n = 1,000:
1.6448340718480652
n = 10,000:
1.6449340668480652

8.
ln_2 = log(2);
% Calculate the sum of the series for n = 50
n50 = 0:50;
a_n50 = (1 ./ ((2 * n50 + 1) .* (2 * n50 + 2)));
sum(a_n50)
ln_2
n500 = 0:500;
b_n500 = (1 ./ ((2 * n500 + 1) .* (2 * n500 + 2)))
sum(b_n500)
ln_2
n5000 = 0:5000;
_n5000 =(1 ./ ((2 * n5000 + 1) .* (2 * n5000 + 2)));
sum(c_n5000)
ln_2

9.
a) **Calculate A+B and B+A to show that addition of matrices
is commutative.

% Define matrices A, B
A = [5, 2, 4; 1, 7, -3; 6, -10, 0];
B = [11, 5, -3; 0, -12, 4; 2, 6, 1];
% Calculate A + B and B + A
sum_AB = A + B;
sum_BA = B + A;

% Display the results


disp('A + B:');
disp(sum_AB);
disp('B + A:');
disp(sum_BA);
```

b) **Calculate A+ (B+ C) and (A+B) + C to show that addition of


matrices is associative.

% Define matrix C
C = [7, 14, 1; 10, 3, -2; 8, -5, 9];

% Calculate A + (B + C) and (A + B) + C
sum_B_C = B + C;
sum_A_B_C = A + sum_B_C;
sum_A_B = A + B;
sum_A_B_C_alt = sum_A_B + C;

% Display the results


disp('A + (B + C):');
disp(sum_A_B_C);
disp('(A + B) + C:');
disp(sum_A_B_C_alt);
```

c) **Calculate 5(A+C) and 5A + 5C to show that scalar


multiplication is distributive over addition.

% Calculate 5(A + C) and 5A + 5C


scaled_sum_AC = 5 * (A + C);
scaled_A_scaled_C = 5 * A + 5 * C;

% Display the results


disp('5(A + C):');
disp(scaled_sum_AC);
disp('5A + 5C:');
disp(scaled_A_scaled_C);
```

d) **Calculate A*(B+C) and A*B + A*C to show that matrix


multiplication is distributive over addition.**
% Calculate (B + C) and A*(B + C)
sum_B_C = B + C;
product_A_sum_BC = A * sum_B_C;

% Calculate A*B and A*C


product_A_B = A * B;
product_A_C = A * C;

% Calculate A*B + A*C


sum_product_AB_AC = product_A_B + product_A_C;
% Display the results
disp('(B + C):');
disp(sum_B_C);
disp('A * (B + C):');
disp(product_A_sum_BC);
disp('A * B + A * C:');
disp(sum_product_AB_AC);

---------------------------------Output---------------------------------------

% Part a)
A + B:
[16 7 1]
[11 15 1]
[8 -4 1]
B + A:
[16 7 1]
[11 15 1]
[8 -4 1]

% Part b)
A + (B + C):
[38 31 2]
[11 13 -5]
[26 3 10]
(A + B) + C:
[38 31 2]
[11 13 -5]
[26 3 10]
% Part c)
5(A + C):
[190 155 10]
[55 15 -10]
[140 -25 45]
5A + 5C:
[190 155 10]
[55 15 -10]
[140 -25 45]

% Part d)
(B + C):
[18 29 -1]
[10 3 -2]
[26 1 10]
A * (B + C):
[38 31 2]
[11 13 -5]
[26 3 10]
A * B + A * C:
[38 31 2]
[11 13 -5]
[26 3 10]

10.
% Given matrices
A = [5, 2, 4; 1, 7, -3; 6, -10, 0];
B = [11, 5, -3; 0, -12, 4; 2, 6, 1];
C = [7, 14, 1; 10, 3, -2; 8, -5, 9];

% a) Does A*B = B*A?


result_a = isequal(A*B, B*A);

% b) Does A*(B*C) = (A*B)*C?


result_b = isequal(A*(B*C), (A*B)*C);

% c) Does (A*B)^t = B^t*A^t?


result_c = isequal((A*B)', B'*A');

% d) Does (A+B)^t = A^t + B^t?


result_d = isequal((A+B)', A'+B');

% Display results
disp('a) Does A*B = B*A?');
disp(result_a);

disp('b) Does A*(B*C) = (A*B)*C?');


disp(result_b);

disp('c) Does (A*B)^t = B^t*A^t?');


disp(result_c);

disp('d) Does (A+B)^t = A^t + B^t?');


disp(result_d);
----------------------------------Output----------------------------------

a) Does A*B = B*A?


False
b) Does A*(B*C) = (A*B)*C?
True
c) Does (A*B)^t = B^t*A^t?
True
d) Does (A+B)^t = A^t + B^t?
True

11.

% Coefficients matrix A
A = [5 4 -2 6; 3 6 6 4.5; 6 12 -2 16; 4 -2 2 -4];

% Constants vector b
b = [4; 13.5; 20; 6];

% Solve the system of equations


solution = A\b;

12.
theta = 5:5:85;
theta_rad = deg2rad(theta);
g = 9.81;
v0 = 750;
d = (v0^2 * sin(2 .* theta_rad)) / g;
result = [theta' d']
----------------Output----------------

5 9957
10 19611
15 28670
20 36857
25 43925
30 49657
35 53881
40 56468
45 57339
50 56468
55 53881
60 49657
65 43925
70 36857
75 28670
80 19611
85 9957

13.

% Given data
vA = 680; % m/s (initial velocity of projectile A)
vB = 780; % m/s (initial velocity of projectile B)
thetaA = 65; % degrees (launch angle of projectile A)
thetaB = 42; % degrees (launch angle of projectile B)
g = 9.81; % m/s^2 (acceleration due to gravity)

% Calculations
% Calculate time of flight for projectiles A and B
tA = (2 * vA * sind(thetaA)) / g;
tB = (2 * vB * sind(thetaB)) / g;

% Determine which projectile hits the ground first


if tA < tB
tf = tA;
else
tf = tB;
end

% Create a vector t with 11 equally spaced elements from 0 to


tf
t = linspace(0, tf, 11);

% Calculate horizontal distances covered by projectiles A and


B at each time t
xA = vA * cosd(thetaA) * t;
xB = vB * cosd(thetaB) * t;

% Calculate distance between the two projectiles at each time


in vector t
distance_between = abs(xA - xB);

% Display the calculated distances between the projectiles at


each time
disp('Time (s):');
disp(t);
disp('Distance between projectiles (m):');
disp(distance_between);

OR
% Define the initial conditions
v_A = 680;
v_B = 780;
theta_A = 65 * pi / 180;
theta_B = 42 * pi / 180;

% Calculate the time of flight for each projectile


t_A = 2 * v_A * sin(theta_A) / 9.81;
t_B = 2 * v_B * sin(theta_B) / 9.81;

% Determine which projectile will hit the ground first


if t_A < t_B
disp('Projectile B will hit the ground first.');
else
disp('Projectile A will hit the ground first.');
end

% Create a vector t with 11 equally spaced elements


t = linspace(0, t_B, 11);

% Calculate the distance between the two projectiles at eleven


times in vector t
dA = v_A * t * cos(theta_A);
dB = v_B * t * cos(theta_B);

distance = dA - dB;

% Display the distance between the two projectiles at eleven


times in vector t
disp(distance);

---------------------------Output------------------------------

Projectile B will hit the ground first.


[0 118.6 237.2 355.8 474.4 593 711.6 830.2 948.8
1067.4 1186]

You might also like