0% found this document useful (0 votes)
2 views

Lab_01_solutions

The document contains solutions to various exercises in a lab assignment for IE 507, including mathematical operations, matrix manipulations, and plotting functions. It covers topics such as generating data points, calculating determinants, ranks, and eigenvalues, as well as creating plots with LaTeX titles and legends. Additionally, it includes functions for solving quadratic equations and swapping matrix rows.

Uploaded by

indrani.das064
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)
2 views

Lab_01_solutions

The document contains solutions to various exercises in a lab assignment for IE 507, including mathematical operations, matrix manipulations, and plotting functions. It covers topics such as generating data points, calculating determinants, ranks, and eigenvalues, as well as creating plots with LaTeX titles and legends. Additionally, it includes functions for solving quadratic equations and swapping matrix rows.

Uploaded by

indrani.das064
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/ 5

IE 507 - Lab 1 Solution

August 1, 2024

Exercise 1: (1 marks)

d = pi/100 : pi/100 : pi;


d.*d
d.^2
w= 1 : (4-1)/9 : 4

Exercise 2 (1 marks)

clc
x = pi/2 : pi/18 : pi;
sin(x)./x

Exercise 3 (2 marks: 0.5 marks for every operation)

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)

Exercise 4 (3 marks: 1 marks each for part 1, 2 and 4)


Part 1

1
% Define the number of data points
n = 100;

% Define the interval


x_start = 0;
x_end = 1;

% Calculate the increment


increment = (x_end - x_start) / (n - 1);

% Generate 100 points using the colon operator


x = x_start:increment:x_end;

% Define the function


y = exp(-x) .* cos(6 * pi * x);

% Create the plot


figure;
plot(x, y, ’LineWidth’, 2);

% Set the title and labels with LaTeX interpreter


title(’Plot of $e^{-x} \cos(6\pi x)$’, ’Interpreter’, ’latex’);
xlabel(’x’);
ylabel(’y’);
grid on;

Part 2
% Define the number of data points
n = 100;

% Define the interval


x_start = 0;
x_end = 1;

% Calculate the increment


increment = (x_end - x_start) / (n - 1);

% Generate 100 points using the colon operator


x = x_start:increment:x_end;

% Define the function


y = exp(-x) .* cos(6 * pi * x);

% Create the plot

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}$’);

% Plot the third function


plot(x, y2, ’g:’, ’LineWidth’, 2, ’DisplayName’, ’$-e^{-x}$’);

% Set the title and labels with LaTeX interpreter


title(’Plot of $e^{-x} \cos(6\pi x)$, $e^{-x}$, and $-e^{-x}$’, ’Interpreter’, ’late
xlabel(’x’);
ylabel(’y’);

% Add a legend
legend(’Interpreter’, ’latex’);

% Enable grid
grid on;

% Release the hold on the plot


hold off;
part 4: In the interval [0,1]; curves of e−x ∗cos(6pix) and e−x touches at 4 points and that
of−e−x and (e−x )cos(6pix) at 3 points. Also e−x cos(6pix) always lie between two because
cos function has range -1 to 1. please

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)

disp("Choose a such that 1<=a<=n");


return
elseif 1 > b || b > s(1)
disp("Choose b such that 1<=b<=n");
return
end
c = A(a,:);
d = A(b,:);
A(b,:) = c;
A(a,:) = d;
end

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

% Assign the last row elements (n+1, 1:n)


m(n+1, 1:n) = d2;

% Assign the last column elements (1:n, n+1)


m(1:n, n+1) = d2’;

end

You might also like