PROGRAMME : B.E/B.
TECH (ECE,EEE,CSE, CSE SPEC, CHEM, BIOTECH)
COURSE CODE : SECB4002 BATCH:2023-2027 SEM: V
COURSE NAME : SOFTWARE TOOLS FOR ENGINEERING APPLICATIONS
ANSWER KEY FOR CAE I
1. What happens when a variable is assigned a new value in MATLAB?Interpret with an
example (CO1)
When a variable is assigned a new value, the previous value is overwritten. MATLAB allows
dynamic typing, so the variable's type can also change based on the new value.
Example:
a = 5;
a = 'Hello'; % now 'a' is a string
2. State the difference between row and column vectors with a MATLAB example. (CO1)
A row vector has elements arranged in a single row:
A = [1 2 3]
A column vector has elements in a single column:
B = [1; 2; 3]
3. List any four arithmetic operators in MATLAB in order of their precedence. (CO2)
Power operator (^)
Unary plus and minus (+, -)
Multiplication and division (*, /, .*, ./)
Addition and subtraction (+, -)
4. Use a MATLAB linspace command to generate 7 equally spaced values between 0 and
30. (CO2)
linspace(0, 30, 7)
This returns: [0 5 10 15 20 25 30]
5. Compare the functions fgetl and fgets used in FTP management. (CO3)
fgetl: Reads a line from a file but removes the newline character.
fgets: Reads a line from a file and retains the newline character.
6. Recall how debugging is performed using breakpoints. (CO3)
In MATLAB, breakpoints can be set by clicking the left margin of a line in the Editor. During
execution, MATLAB pauses at that line, allowing the programmer to inspect variable values
and step through the code line by line.
7. Interpret the MATLAB code using try and catch to perform division by zero. (CO3)
try
a = 5;
b = 0;
c = a / b;
catch
disp('Error: Division by zero');
end
This code prevents the program from crashing by catching the division-by-zero error and
displaying a message.
PART B
8. Identify the difference between built-in and user-defined functions in MATLAB. List two
examples of each. Create a user-defined function to calculate the area of a circle and
explain. (CO1)
Difference Between Built-in and User-defined Functions
Built-in functions are pre-defined and provided by MATLAB. Users can directly use them
without defining them.
User-defined functions are written by users to perform custom operations not available in
built-in functions.
Examples
Built-in functions:
sqrt() – Calculates square root
mean() – Computes the average of an array
User-defined functions (examples):
area_circle() – Computes area of a circle
sum_vector() – Computes sum of elements in a vector (user-defined version)
User-defined Function to Calculate Area of a Circle
Function Code:
function A = area_circle(r)
A = pi * r^2;
end
Explanation
The function area_circle accepts a radius r as input.
It uses the formula A = π × r² to compute the area.
The result is stored in output variable A.
This function must be saved in a separate .m file with the same name as the function
(area_circle.m).
To call the function:
result = area_circle(5); % Output will be area of circle with radius 5
[Link] between cell arrays and structures in MATLAB. Infer how the data is stored
and retrieved from cell arrays and structures. CO1
Cell Arrays:
Store heterogeneous data types (numbers, strings, arrays).
Accessed using curly braces {} for content and parentheses () for assignment.
Example:
C = {10, 'hello', [1 2 3]};
val = C{2}; % returns 'hello'
Structures:
Store heterogeneous data using named fields.
Accessed using dot notation.
Example:
[Link] = 'John';
[Link] = 25;
[Link] = [85 90 95];
val = [Link](2); % returns 90
Data Storage:
Cell array stores data in indexed positions.
Structure stores data in fields with labels.
Data Retrieval:
Cell: C{1}, C{3}(2)
Struct: [Link], [Link](1)
Inference:
Use cell arrays when data is positional.
Use structures when data is labeled or named.
10. Explain the concept of array arithmetic operations in MATLAB. Distinguish
between element-wise and matrix operations. Provide suitable examples. CO2
Array Arithmetic Operations in MATLAB:
o MATLAB supports operations on arrays, such as addition, subtraction,
multiplication, division, and exponentiation.
o These operations can be done as matrix operations (based on linear algebra
rules) or element-wise operations (performed on corresponding elements).
Matrix Operations:
o Follow rules of linear algebra.
o Examples:
A = [1 2; 3 4];
B = [5 6; 7 8];
C = A * B; % matrix multiplication
D = A ^ 2; % matrix power
Element-wise Operations:
o Performed on each corresponding element.
o Use dot (.) before the operator: .*, ./, .^
o Examples:
A = [1 2; 3 4];
B = [5 6; 7 8];
C = A .* B; % element-wise multiplication
D = A .^ 2; % element-wise squaring
Distinction:
o Matrix operations require size compatibility as per algebra rules.
o Element-wise operations require arrays of the same size and operate
individually on elements.
Conclusion:
o Use matrix operations for linear algebraic computations.
o Use element-wise operations for point-by-point calculations.
[Link] a program that accepts student marks for six subjects to assign an overall grade
based on average marks using Nested else-if statements (CO3).
% Accept marks for 6 subjects
marks = zeros(1, 6);
for i = 1:6
marks(i) = input(sprintf('Enter marks for subject %d: ', i));
end
% Calculate average
avg = mean(marks);
% Assign grade using nested else-if
if avg >= 90
grade = 'A';
elseif avg >= 75
grade = 'B';
elseif avg >= 60
grade = 'C';
elseif avg >= 50
grade = 'D';
else
grade = 'F';
end
12. Design a MATLAB program to compute the roots of a quadratic equations of the
form ax2 + bx + c = 0 using if-else if-else conditional structure CO3
% Input coefficients
a = input('Enter coefficient a: ');
b = input('Enter coefficient b: ');
c = input('Enter coefficient c: ');
% Calculate discriminant
d = b^2 - 4*a*c;
% Use if-elseif-else to determine the nature of roots
if d > 0
% Real and distinct roots
x1 = (-b + sqrt(d)) / (2*a);
x2 = (-b - sqrt(d)) / (2*a);
fprintf('Roots are real and distinct:\n');
fprintf('x1 = %.2f\n', x1);
fprintf('x2 = %.2f\n', x2);
elseif d == 0
% Real and equal roots
x = -b / (2*a);
fprintf('Roots are real and equal:\n');
fprintf('x = %.2f\n', x);
else
% Complex roots
realPart = -b / (2*a);
imagPart = sqrt(-d) / (2*a);
fprintf('Roots are complex and imaginary:\n');
fprintf('x1 = %.2f + %.2fi\n', realPart, imagPart);
fprintf('x2 = %.2f - %.2fi\n', realPart, imagPart);
end
13. Write a user-defined function in MATLAB named multtable that accepts two input
arguments: rows and columns. The function should return a matrix containing the
multiplication table with the specified number of rows and columns. Explain the logic used
in the function to construct the multiplication table and show the output of the function
call multtable(3,5) and interpret the result. (CO3)
Explanation of Logic
The function takes two input arguments: rows and columns.
It initializes an empty matrix using zeros(rows, columns) for preallocation to improve
performance.
Two nested loops are used:
o The outer loop (i) runs from 1 to number of rows.
o The inner loop (j) runs from 1 to number of columns.
Each element outmat(i,j) is computed as the product of i * j, i.e., the row number
times the column number.
This fills the matrix with a standard multiplication table format.
Function Code
function outmat = multtable(rows, columns)
% Creates a multiplication table matrix with given rows and columns
% Preallocate matrix
outmat = zeros(rows, columns);
% Fill the matrix using nested loops
for i = 1:rows
for j = 1:columns
outmat(i,j) = i * j;
end
end
end
Output and Interpretation
Function Call:
multtable(3,5)
Output:
ans =
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15