#Exercise 1
Find the value of y = ln(sinh(exp(54 ^3/ 6*pi )))
>> cube_val = 54^3;
pi_val = pi;
y = log(sinh(exp(cube_val / (6 * pi_val))))
y=
Inf
The issue here is that the exponential function (exp(cube_val / (6 * pi_val))) is returning a very large
value, which is causing the hyperbolic sine function (sinh()) to return Inf.
One way to get a finite value is to use the property of hyperbolic functions that sinh(x) is
approximately equal to (exp(x)/2) for large x. This simplifies the expression to
y = log(exp(cube_val / (6 * pi_val))/2)
Which further simplifies to
y = cube_val / (6 * pi_val) - log(2).
Here’s the modified MATLAB code:
>> cube_val = 54^3;
pi_val = pi;
y = cube_val / (6 * pi_val) - log(2)
y=
8.3530e+03
# Exercise 2
Find the size, and length of following matrices
A=[1 2 3; 4 5 6;7 6 54; 65 23 45]
B=7:1:13.5
# Write A(1:2,2:3) in command window. Write A([1 2],[2 3]). These are different ways
to select a submatrix of a matrix.
#A(1,1)=sin(5); assign a new value to an element of A.
>>A = [1 2 3; 4 5 6; 7 6 54; 65 23 45];
B = 7:1:13.5;
size_A = size(A);
length_A = length(A);
size_B = size(B);
length_B = length(B);
submatrix1 = A(1:2,2:3);
submatrix2 = A([1 2],[2 3]);
A(1,1) = sin(5);
>> size_A
size_A =
4 3
>> length_A
length_A =
>> size_B
size_B =
1 7
>> length_B
length_B =
>> submatrix1
submatrix1 =
2 3
5 6
>> submatrix2
submatrix2 =
2 3
5 6
>> A
A=
-0.9589 2.0000 3.0000
4.0000 5.0000 6.0000
7.0000 6.0000 54.0000
65.0000 23.0000 45.0000
#Exercise 3
A=[2 3; 4 5]; B=[3 4; 6 7];
Find A+B, A*B, A.*B,A/B,A\B, A.^2,A./B
>> A = [2 3; 4 5];
B = [3 4; 6 7];
C = A + B;
D = A * B;
E = A .* B;
F = A / B;
G = A \ B;
H = A .^ 2;
I = A ./ B;
>> C
C=
5 7
10 12
>> D
D=
24 29
42 51
>> E
E=
6 12
24 35
>> F
F=
1.3333 -0.3333
0.6667 0.3333
>> G
G=
1.5000 0.5000
0 1.0000
>> H
H=
4 9
16 25
>> I
I=
0.6667 0.7500
0.6667 0.7143
# Exercise 4
Define the matrices
A=[17 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]
B=[ 2 3 4 5 ; 6 7 8 9 ; 10 11 12 13 ; 14 15 16 17 ]
C=[ 1 2 3 ; 4 5 6 ; 7 8 9 ]
y=[ 4 3 2 1 ]'
Note the transpose ' on the y-vector which makes y a column vector.
a) Compute AB and BA. Is matrix multiplication commutative?
b) Compute AC. Why do you get an error message?
c) Solve the following system of equations:
17x1+2x2+3x3+4x4 = 4
5x1+6x2+7x3+8x4 = 3
9x1+10x2+11x3+12x4 = 2
13x1+14x2+15x3+16x4 = 1
>>A= [17 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16];
B = [2 3 4 5; 6 7 8 9; 10 11 12 13; 14 15 16 17];
C = [1 2 3; 4 5 6; 7 8 9];
y = [4; 3; 2; 1];
AB = A * B;
BA = B * A;
AC = A * C;
end
b = y;
X = A \ b;
>> AB
AB =
132 158 184 210
228 254 280 306
356 398 440 482
484 542 600 658
>> BA
BA =
150 132 146 160
326 260 290 320
502 388 434 480
678 516 578 640
>> AC
Unrecognized function or variable 'AC'.
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix
matches the number of rows in the second matrix. To operate on each element of the matrix
individually,
use TIMES (.*) for elementwise multiplication.
Related documentation
>> X
X=
0
-2.5000
0.0000
2.2500
a) Matrix multiplication is not commutative in general. That is, the order in which matrices are
multiplied can affect the result. So, in general, AB ≠ BA.
b) We get an error message when trying to compute AC because the number of columns in A (which
is 4) does not match the number of rows in C (which is 3). In matrix multiplication, the number of
columns in the first matrix must be equal to the number of rows in the second matrix.
c) The A \ b operation in MATLAB is used to solve the system of linear equations Ax = b for x. The
result X is the solution to the system of equations.