Lab_01_solutions
Lab_01_solutions
August 1, 2024
Exercise 1: (1 marks)
Exercise 2 (1 marks)
clc
x = pi/2 : pi/18 : pi;
sin(x)./x
clear All
A = [1 7 3; 4 0 6; 2 5 -1];
B =[1 7 3
4 0 6
2 5 -1];
C=A*B;
D=A*B*C;
fprintf(’Determinant\n’);
det(D)
fprintf(’Rank \n’);
rank(D)
fprintf(’Inverse\n’);
inv(D)
fprintf(’Eigenvalues\n’);
eig(D)
1
% Define the number of data points
n = 100;
Part 2
% Define the number of data points
n = 100;
2
figure;
plot(x, y, ’LineWidth’, 2,’DisplayName’,’$e^{-x}*cos(6pix)$’);
xlabel(’x’);
ylabel(’y’);
grid on;
hold on;
y1 = exp(-x);
y2 = -exp(-x);
% Plot the second function
plot(x, y1, ’r--’, ’LineWidth’, 2, ’DisplayName’, ’$e^{-x}$’);
% Add a legend
legend(’Interpreter’, ’latex’);
% Enable grid
grid on;
Exercise 5:
secroot(1,0,-1)
secroot(1,-2,-1)
secroot(3,4,5)
MatrixSwap([1,2,3;4,5,6;7 8 9],1,2);
%5a)
function [r1, r2] = secroot(a, b, c)
% Find Determinant
3
Det = b^2 - 4 * a * c;
if Det < 0
r1 = (-b + i * sqrt(-Det)) / (2 * a);
r2 = (-b - i * sqrt(-Det)) / (2 * a);
disp("The two roots are complex conjugates");
elseif Det == 0
r1 = -b / (2 * a);
r2 = -b / (2 * a);
disp("There are two repeated roots");
else
r1 = (-b + sqrt(Det)) / (2 * a);
r2 = (-b - sqrt(Det)) / (2 * a);
disp("The two roots are real");
end
end
%5b)
function [A] = MatrixSwap(A,a,b)
s = size(A);
if ne(s(1),s(2))
disp("Choose a square matrix");
return
elseif 1 > a || a > s(1)
Exercise 6:
n = 3;
A = ArrowMatrix(n);
disp(A);
function m = ArrowMatrix(n)
% Create a vector for the main diagonal
d = 1:n+1;
4
% Assign the diagonal elements from (1,1) to (n,n)
m = diag(d);
% Create the vector for the last row and last column
d2 = 2 * (1:n);
end