MATLAB - Chp-3 Ex PDF
MATLAB - Chp-3 Ex PDF
```matlab
% Define the vectors u and v as MATLAB vectors
u = [4, 9, -5];
v = [-3, 6, -7];
2.
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.
% Given constants
g = 9.81; % acceleration due to gravity in m/s^2
% Calculate the depth of the well for each time using element-
wise calculations
d = 0.5 * g * t.^2;
----------------------------------Output-----------------------------------------
Time (s):
1 2 3 4 5 6 7 8 9 10
Depth (m):
4.9050 19.6200 44.1450 78.4800 122.6350 176.6100
240.4050 314.0200 397.4550 490.7100
4.
% Define the vectors x and y
x = [2, 4, 6, 8, 10];
y = [3, 6, 9, 12, 15];
---------------------------------Output------------------------------------------
Values of z:
33.3929 267.0056 587.6491 1282.0287 2847.5522
5.
------------------------------------Output-------------------------------------
Values of T:
[164.20043282 46.39237005 26.18890142
17.79441838]
6.
Values of y:
[2.718281828459045
2.593742460089938
2.704813829432384
2.709453232511283
2.710558581469458
2.710741261316918
2.710743777872953
2.710743979387769]
Value of e: 2.718281828459045
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;
% Display results
disp(['Sum for n = 100: ', num2str(sum_a)]);
disp(['Approximation for n = 100: ',
num2str(approximation_a)]);
disp(['Sum for n = 1000: ', num2str(sum_b)]);
disp(['Approximation for n = 1000: ',
num2str(approximation_b)]);
disp(['Sum for n = 10000: ', num2str(sum_c)]);
disp(['Approximation for n = 10000: ',
num2str(approximation_c)]);
-------------------------(OR-Alternate program)--------------------------
disp('n = 1,000:');
disp(sum_n1000);
disp('n = 10,000:');
disp(sum_n10000);
-------------------------------Output------------------------------------------
n = 100:
1.6439340668482264
n = 1,000:
1.6448340718480652
n = 10,000:
1.6449340668480652
8.
disp('n = 500:');
disp(sum_n500);
disp('vs.');
disp(ln_2);
disp('n = 5,000:');
disp(sum_n5000);
disp('vs.');
disp(ln_2);
---------------------------------Output----------------------------------------
n = 50:
0.6907394138314922
vs.
0.6931471805599453
n = 500:
0.693147179391284
vs.
0.6931471805599453
n = 5,000:
0.693147180538616
vs.
0.6931471805599453
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;
% 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;
---------------------------------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];
% Display results
disp('a) Does A*B = B*A?');
disp(result_a);
----------------------------------Output----------------------------------
% 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];
-----------------------------Output--------------------------
Solution:
x = 0.75
y = 1.5
z = -1.5
w = 1.25
12.
% Define the firing angles in degrees
theta = 5:5:85;
----------------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;
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;
distance = dA - dB;
---------------------------Output------------------------------