0% found this document useful (0 votes)
43 views22 pages

MATLAB - Chp-3 Ex PDF

This document contains examples of calculating dot products, element-wise operations, matrix operations, solving systems of linear equations, and projectile motion calculations in MATLAB. It demonstrates various MATLAB functions and operations on vectors and matrices through examples.

Uploaded by

Falak Sher
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views22 pages

MATLAB - Chp-3 Ex PDF

This document contains examples of calculating dot products, element-wise operations, matrix operations, solving systems of linear equations, and projectile motion calculations in MATLAB. It demonstrates various MATLAB functions and operations on vectors and matrices through examples.

Uploaded by

Falak Sher
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

1.

### Method 1 (Matrix Multiplication):

% Define the vectors u and v as row and column vectors,


respectively
u = [4, 9, -5]; % Row vector
v = [-3; 6; -7]; % Column vector

% Calculate the dot product using matrix multiplication


dotProductMatrix = u * v;

% Display the dot product calculated using matrix


multiplication
disp('Dot Product using Matrix Multiplication:');
disp(dotProductMatrix);
```

### Method 2 (Using the dot function):

```matlab
% Define the vectors u and v as MATLAB vectors
u = [4, 9, -5];
v = [-3, 6, -7];

% Calculate the dot product using the dot function


dotProduct = dot(u, v);

% Display the dot product calculated using the dot function


disp('Dot Product using dot function:');
disp(dotProduct);

2.

% Define the vector of x values


x = [-2.5, -2, -1.5, -1, -0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3];

% Calculate the corresponding y values using element-wise


calculations
y = x.^3 .* (x.^2 + 1).^3;

% Display the values of x and y


disp('Values of x:');
disp(x);
disp('Values of y:');
disp(y);
---------------------------------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.
% Given constants
g = 9.81; % acceleration due to gravity in m/s^2

% Create a vector of time values from 1 to 10 seconds


t = 1:10; % This creates a vector [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

% Calculate the depth of the well for each time using element-
wise calculations
d = 0.5 * g * t.^2;

% Display the values of t and corresponding depths d


disp('Time (s):');
disp(t);
disp('Depth (m):');
disp(d);

----------------------------------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];

% Calculate z using the given expression


z = (x.*y + y./x) ./ (x + y).^(y - x) + 12.^(x./y);

% Display the calculated values of z


disp('Values of z:');
disp(z);

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

Values of z:
33.3929 267.0056 587.6491 1282.0287 2847.5522

5.

% Define the scalars h and k, and vectors x, y, and z


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];

% Calculate T using the given formula (element-wise


calculations)
T = (x .* y .* z) ./ ((h + k).^(k/5)) + (k .* exp((z ./ x) + y)) ./
(z.^h);

% Display the calculated values of T


disp('Values of T:');
disp(T);

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

Values of T:
[164.20043282 46.39237005 26.18890142
17.79441838]

6.

% Define the vector n with the given elements


n = [1, 10, 100, 500, 1000, 2000, 4000, 8000];

% Calculate y using the given formula for each element of n


y = (1 + 1 ./ n) .^ n;

% Calculate the value of e using exp(1)


e_value = exp(1);

% Display the calculated values of y and the value of e


disp('Values of y:');
disp(y);
disp(['Value of e: ', num2str(e_value)]);

% Calculate the absolute difference between y and the value


of e
absolute_difference = abs(y - e_value);

% Display the absolute differences


disp('Absolute Differences between y and e:');
disp(absolute_difference);

The” num2str “function converts a numeric value to a string


representation.
----------------------------------Output-----------------------------------------

Values of y:
[2.718281828459045
2.593742460089938
2.704813829432384
2.709453232511283
2.710558581469458
2.710741261316918
2.710743777872953
2.710743979387769]

Value of e: 2.718281828459045

Absolute Differences between y and e:


[0.000000000000000
0.000048468370892
0.000038051007643
0.000038682052236
0.000023247016600
0.000004759962158
0.000003050584087
0.000002850569126
The num2str function converts a numeric value to a string
representation.

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

% Formula for the sum of the infinite series


sum_of_series = pi^2 / 6;

% Calculate the sum of the series for n = 100


n100 = 1:100;
sum_n100 = sum(1 ./ n100.^2);

% Calculate the sum of the series for n = 1,000


n1000 = 1:1000;
sum_n1000 = sum(1 ./ n1000.^2);

% Calculate the sum of the series for n = 10,000


n10000 = 1:10000;
sum_n10000 = sum(1 ./ n10000.^2);

% Display the results


disp('n = 100:');
disp(sum_n100);

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.

% Function to calculate the sum of the series


sum_of_series = @(n) 0.5 * (log(2 * n + 2) - log(2 * n + 1));

% Calculate the sum of the series for n = 50


n50 = 0:50;
sum_n50 = sum(1 ./ ((2 * n50 + 1) .* (2 * n50 + 2)));

% Calculate the sum of the series for n = 500


n500 = 0:500;
sum_n500 = sum(1 ./ ((2 * n500 + 1) .* (2 * n500 + 2)));

% Calculate the sum of the series for n = 5,000


n5000 = 0:5000;
sum_n5000 = sum(1 ./ ((2 * n5000 + 1) .* (2 * n5000 + 2)));

% Calculate the value of ln 2


ln_2 = log(2);

% Compare the values obtained to ln 2


disp('n = 50:');
disp(sum_n50);
disp('vs.');
disp(ln_2);

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;

% 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;

% Display the solution


disp('Solution:');
disp(['x = ', num2str(solution(1))]);
disp(['y = ', num2str(solution(2))]);
disp(['z = ', num2str(solution(3))]);
disp(['w = ', num2str(solution(4))]);

-----------------------------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;

% Convert the firing angles to radians


theta_rad = deg2rad(theta);

% Calculate the distance traveled by the


projectile
g = 9.81; % m/s^2
v0 = 750; % m/s
d = (v0^2 * sin(2 * theta_rad)) / g;

% Create a 17 × 2 matrix to store the


results
results = zeros(length(theta), 2);
results(:, 1) = theta;
results(:, 2) = round(d);

% Display the results


disp(results);

----------------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