Name:
Enrolment No:
Odd Semester End Term Examination, January 2025
Faculty of Engineering, SAMM
Department of Mechanical Engineering
B.Tech
Course Code: MEE1006/ME1006 Course: MATLAB for Engineers Semester: I
Time: 03 hrs. Max. Marks: 80
Instructions: * All questions are compulsory * Missing data, if any, may be assumed suitably * Calculator is not allowed.
SECTION A (Max. Marks 10)
S.No. Description Marks CO
Q. A1 Choose the MATLAB command for finding out the derivative of the polynomial p? 2 1
a. der(p)
b. polyder(p)
c. derivative(p)
d. None of these
Q. A2 How many times will the loop execute? Coose the correct option. 2 1, 2
for i = 1:0.5:5
end
a. 9 times
b. 10 times
c. 11 times
d. 12 times
Q. A3 What will be the result of 5 < 3 | 7 > 6 in MATLAB? 2 2, 3
a. 1
b. 0
c. true
d. false
Q. A4 What is the output of the following MATLAB code? 2 1, 2
p = [1 2 1];
v = polyval(p, 2);
disp(v);
Solution:
v=9
Q. A5 What will be the output of the following MATLAB code? 2 1, 3
Page 1 of 7
A=[3; 6; 9; 12; 15];
B=A’;
disp(B)
Solution:
B=[3 6 9 12 15]
SECTION B (Max. Marks 30)
Q. B1 Write a MATLAB code to create the following matrices O and P: 6 1, 2
1 1 1 1 2 3
𝑂 = [4 8 10], 𝑃 = [41 51 61]
9 18 27 36 18 9
Write a code to find a matrix ‘Q’ by addition of matrix ‘P’ and transpose of ‘O’, also
write a code to delete the third row from the obtained matrix ‘Q’.
Solution: [2 marks for each operation: creation, addition and deletion]
% Define matrices O and P
O = [1 1 1; 4 8 10; 9 18 27];
P = [1 2 3; 41 51 61; 36 18 9];
% Compute Q by adding P and the transpose of O
Q = P + O';
% Delete the third row from Q
Q_modified = Q(3, :) = [];
Q. B2 Write a MATLAB code to plot both the functions 𝑦 = 𝑠𝑖𝑛(𝑥) and 𝑦 = 𝑐𝑜𝑠(𝑥) on the 6 1, 2
same graph for the values of x ranging from 0 to 2π. Label the axes as ‘y’ and ‘x’ with
a title of the plot as ‘Sine and Cosine Functions’. Also write a code to show the sine
and cosine functions with red and blue lines respectively.
Solution: [2 marks for each operation: defining the functions, plots and labeling etc.]
% Define x values from 0 to 2*pi
x = 0:0.01:2*pi;
% Calculate y values for sin(x) and cos(x)
y1 = sin(x);
Page 2 of 7
y2 = cos(x);
% Create the plot
figure;
plot(x, y1, 'r'); % Red line for sin(x)
hold on;
plot(x, y2, 'b'); % Blue line for cos(x)
% Add labels, title, and legend
xlabel('x (radians)');
ylabel('y');
title('Sine and Cosine Functions');
Q. B3 Write a MATLAB code to plot the function, 𝑓(𝑥) = 𝑥 2 cos (1 + 𝑥 2 ) for ‘x’ in the 6 2
range [0, 2𝜋] using an anonymous function.
Solution: [2 marks for each operation: defining the anonymous function, x & y and the
plot]
f = @(x) (x.^2).*cos(1+x.^2) ; % Define the function
x = linspace(0, 2*pi, 100); % Generate x values
y = f(x); % Compute y values
plot(x, y);
Q. B4 Using the MATLAB built-in functions (zeros, ones, eye), create the following matrix 6 1, 4
0 0 0 1 1
0 0 0 1 1
1 0 0 0 0
𝐴= 0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
[0 0 0 0 1]
Solution: [2 marks for each operation: Use of ‘zeros’, ‘ones’, and ‘eye’ commands
correctly]
% Define the size of the matrix
Page 3 of 7
rows = 7;
cols = 5;
% Initialize the matrix A with zeros
A = zeros(rows, cols);
% Fill specific parts of the matrix using built-in functions
A(1:2, 4:5) = ones(2, 2); % Fill the first two rows and last two columns with ones
A(3, 1) = 1; % Set element at (3,1) to 1
A(4:7, :) = eye(4); % Insert a 4x4 identity matrix starting from row 4
Q. B5 Write a MATLAB code to create a 5×5 matrix where the (𝑖, 𝑗)th element is 𝑖 2 + 𝑗 2. 6 2, 4
Also perform the following operations:
a. Find the sum of all elements in the matrix.
b. Extract a submatrix consisting of the first three rows and first three columns
Solution: [3 marks till the use of for loop, 1.5 marks each to find out the sum and the
submatrix]
% Initialize the size of the matrix
n = 5;
% Create the matrix where A(i, j) = i^2 + j^2
A = zeros(n); % Initialize an n x n matrix with zeros
for i = 1:n
for j = 1:n
A(i, j) = i^2 + j^2;
end
end
% Calculate the sum of all elements in the matrix
sumA = sum(A(:)); % Sum all elements in the matrix
% Extract the submatrix consisting of the first 3 rows and first 3 columns
subMatrix = A(1:3, 1:3);
Page 4 of 7
SECTION-C (Max. Marks 20)
Q. C1 Using MATLAB, find the roots of two polynomials, 𝑃1 (𝑥) = 8𝑥 7 − 5𝑥 4 + 2𝑥 2 − 10 1, 2
7 and 𝑃2 (𝑥) = 3𝑥 7 − 5𝑥 3 − 𝑥 + 3, add the roots of both polynomials and assume
it roots of a new polynomial. Find the polynomial coefficient vector for added roots.
Write a code to further plot the obtained polynomial value for x ranging from -2 to 2
with an interval of 0.2 with black color and dashed line. Label the axes and mark
title as ‘New Polynomial’.
Solution: [2 marks for each operation: defining the polynomials, finding roots,
obtaining the coefficients of a new polynomial, plot, and labeling etc.]
p1 = [8 0 0 -5 0 2 0 -7]
p2 = [3 0 0 0 -5 0 -1 3]
r1 = roots(p1)
r2 = roots(p2)
r = r1+r2
coeff = poly(r)
x = [-2:0.2:2]
plot(x,polyval(coeff,x),'k-')
xlabel('x')
ylabel('y')
title('New polynomial')
Q. C2 Construct an anonymous function to compute the equation given below in MATLAB. 10 2, 3
Use the anonymous function to find the values of 𝑓(𝑥) in the range 0 ≤ 𝑥 ≤ 3 with a
step size of 0.1. Also write a code to obtain the results in the form of a ‘Table’ having
values of 𝑥 and 𝑓(𝑥).
𝑓(𝑥) = 𝑥 3 − 𝑥 − 2
Solution: [2 marks for each operation: defining the anonymous function, defining the
range of x, computation of corresponding values of f(x), and obtaining the table]
% Define the anonymous function
f = @(x) x.^3 - x - 2;
% Define the range of x values with a step size of 0.1
x = 0:0.1:3;
Page 5 of 7
% Compute the values of f(x)
fx = f(x);
% Combine x and f(x) into a table
resultsTable = table(x', fx', 'VariableNames', {'x', 'f(x)'});
SECTION-D (Max. Marks 20)
Q. D1 You are studying the growth of a bacterial population over time, which follows an 10 3, 4
exponential growth pattern. You are given the following data:
Time (hours) 0 1 2 3 4 5
Population 5 10 20 40 80 160
Use MATLAB to fit an exponential curve and predict the population at t=6 hours.
Solution: [2.5 marks for each operation: defining the data points, use of polyfit,
obtaining A and B, and estimation at t=6 hours]
% 1. Given data
t = [0, 1, 2, 3, 4, 5]; % Time (hours)
P = [5, 10, 20, 40, 80, 160]; % Population
% 2. Fit a linear polynomial to the transformed data
p = polyfit(t, ln_P, 1); % Linear fit (degree 1)
% 3. Extract the parameters A and B
B = p(1); % B (slope)
ln_A = p(2); % ln(A) (intercept)
A = exp(ln_A); % Revert the logarithmic transformation to get A
% 4. Predict the population at t = 6 hours
population_at_6hrs = A * exp(B * 6);
Q. D2 Write a MATLAB script to analyze the cooling of an object using Newton’s Law of 10 3, 4
Cooling. The formula is 𝑻(𝒕) = 𝑻𝒆𝒏𝒗 + (𝑻𝒐 − 𝑻𝒆𝒏𝒗 ) ⋅ 𝒆−𝒌𝒕 , where 𝑻𝒐 is the initial
temperature (100 °C), 𝑻𝒆𝒏𝒗 is the environmental temperature (20 °C), ‘k’ is the
cooling constant (0.02), and ‘t’ is time in seconds (0 sec). Use a while loop to compute
and display the temperature every second until it drops below 5 °C above the
environment.
Solution: [2 marks each for defining the values and initial conditions, 3 marks each for
use of while loop and display of temperatures]
Page 6 of 7
T0 = 100; % Initial temperature in Celsius
Tenv = 20; % Environmental temperature in Celsius
k = 0.02; % Cooling constant
t = 0; % Time in seconds
T = T0; % Initial temperature setting
while T > Tenv + 5
T = Tenv + (T0 - Tenv) * exp(-k * t);
disp('Temp’, T);
t = t + 1;
end
disp('Cooling completed.');
Page 7 of 7