1. What are the different components of computer?
Different components of a computer:
Central Processing Unit (CPU): Executes instructions and processes data.
Memory (RAM): Temporarily stores data and instructions for quick access.
Storage (Hard Drive/SSD): Permanently stores data and programs.
Input Devices: Allow users to input data (e.g., keyboard, mouse).
Output Devices: Display results (e.g., monitor, printer).
Motherboard: Connects all components and allows communication between them.
2. What are variables?
Variables are storage locations in memory with a name associated with them. They hold data that can be changed during program execution. In MATLAB, you can create a variable by simply assigning a value to a name, e.g., x = 10;.
3. Name some built in functions in MATLAB.
sin: Computes the sine of an angle.
cos: Computes the cosine of an angle.
sum: Sums all elements of an array.
mean: Computes the mean of an array.
plot: Creates a 2D plot of data.
4. What are Waveforms in MATLAB?
Waveforms are graphical representations of signals over time. In MATLAB, you can generate and analyze various types of waveforms like sine waves, square waves, and sawtooth waves using built-in functions like sin, square, and sawtooth.
Waveforms are graphical representations of signals over time. In MATLAB, you can generate and analyze various types of waveforms like sine waves, square waves, and sawtooth waves using built-in functions like sin, square, and sawtooth.
5. What are M-files?
M-files are script or function files written in MATLAB. They have a .m extension. Script files contain a sequence of MATLAB commands, while function files define functions that can accept inputs and return outputs.
6. Write the code in MATLAB to add two matrices.
% Define two matricesA = [1, 2; 3, 4];B = [5, 6; 7, 8];% Add the matricesC = A + B;% Display the resultdisp(C); (code-box)
7. Demonstrate arguments and return values in MATLAB with the help of an
example.
In MATLAB, functions can accept input arguments and return output values. Here's an example:
% Define a function to calculate the area of a rectanglefunction area = rectangleArea(length, width)% Calculate the areaarea = length * width;end% Call the function with arguments and display the resultlength = 5;width = 3;result = rectangleArea(length, width);disp(['The area of the rectangle is: ', num2str(result)]); (code-box)
In this example, rectangleArea is a function that takes two input arguments (length and width) and returns the area of the rectangle. The result is displayed using 'disp'.
8. Write a program in MATLAB to find the determinant of a matrix.
% Define a matrixA = [1, 2, 3; 4, 5, 6; 7, 8, 9];% Calculate the determinantdeterminant = det(A);% Display the resultdisp(['The determinant of the matrix is: ', num2str(determinant)]); (code-box)
This code defines a 3x3 matrix 'A', calculates its determinant using the 'det' function, and displays the result.
9. Draw a graph that joins the points (1, 0), (5, 4) and (2, 1).
% Define the pointsx = [1, 5, 2];y = [0, 4, 1];% Plot the points and join them with linesplot(x, y, '-o');% Add labels and titlexlabel('X-axis');ylabel('Y-axis');title('Graph joining the points (1, 0), (5, 4), and (2, 1)');% Display the gridgrid on; (code-box)
This code plots the points (1, 0), (5, 4), and (2, 1) and joins them with lines.
10. Discuss assignment statement in MATLAB with the help of examples.
An assignment statement in MATLAB is used to assign values to variables. The syntax is '
variable = value;'.Example:
% Assign a number to a variablex = 10;% Assign the result of an expression to a variabley = x + 5;% Assign a string to a variablename = 'MATLAB';% Assign a matrix to a variableA = [1, 2; 3, 4]; (code-box)
In these examples, 'x' is assigned the value 10, 'y' is assigned the result of 'x + 5', 'name' is assigned the string 'MATLAB', and 'A' is assigned a 2x2 matrix.
11. Write a program in MATLAB to arrange ‘n’ numbers in descending order.
% Define an array of numbersnumbers = [3, 1, 4, 1, 5, 9, 2];% Sort the numbers in descending ordersortedNumbers = sort(numbers, 'descend');% Display the resultdisp('The numbers in descending order are:');disp(sortedNumbers); (code-box)
This code defines an array of numbers, sorts them in descending order using the '
sort' function with the 'descend' option, and displays the result.12. Discuss ‘if-else’ in MATLAB.
The
if-else statement in MATLAB is used for conditional execution of code. The basic syntax is:if condition% Code to execute if condition is trueelseif another_condition% Code to execute if another_condition is trueelse% Code to execute if all conditions are falseend (code-box)
Example:
% Define a variablex = 10;% Use if-else to check conditionsif x > 0disp('x is positive');elseif x < 0disp('x is negative');elsedisp('x is zero');end (code-box)
In this example, if '
x' is greater than 0, it displays 'x is positive'. If 'x' is less than 0, it displays 'x is negative'. If neither condition is met, it displays 'x is zero'.13. Write a program in MATLAB to demonstrate binary division with the help of an
example.
Binary division can be demonstrated by converting decimal numbers to binary, performing the division, and then converting the result back to decimal. Here's a step-by-step example:
% Define two binary numbers as stringsbinaryNum1 = '1010'; % 10 in decimalbinaryNum2 = '0101'; % 5 in decimal% Convert binary numbers to decimaldecNum1 = bin2dec(binaryNum1);decNum2 = bin2dec(binaryNum2);% Perform division in decimalquotientDec = floor(decNum1 / decNum2);remainderDec = mod(decNum1, decNum2);% Convert the results back to binaryquotientBin = dec2bin(quotientDec);remainderBin = dec2bin(remainderDec);% Display the resultsdisp(['Binary Division: ', binaryNum1, ' / ', binaryNum2]);disp(['Quotient: ', quotientBin, ' (', num2str(quotientDec), ' in decimal)']);disp(['Remainder: ', remainderBin, ' (', num2str(remainderDec), ' in decimal)']); (code-box)
14. Write a program in MATLAB to demonstrate polynomial multiplication with the
help of an example.
% Define two polynomials as coefficient vectorsp1 = [1 2 3]; % Represents 1*x^2 + 2*x + 3p2 = [4 5]; % Represents 4*x + 5% Perform polynomial multiplicationresult = conv(p1, p2);% Display the resultdisp('Polynomial Multiplication:');disp(['P1(x): ', poly2str(p1, 'x')]);disp(['P2(x): ', poly2str(p2, 'x')]);disp(['Result: ', poly2str(result, 'x')]); (code-box)
15. Explain different types of loops in MATLAB with the help of an example.
MATLAB supports
for loops and while loops for repeating a block of code multiple times.- For Loop:
% For loop examplefor i = 1:5disp(['Iteration: ', num2str(i)]);end (code-box)
- While Loop:
% While loop examplei = 1;while i <= 5disp(['Iteration: ', num2str(i)]);i = i + 1;end (code-box)
16. Write a program in MATLAB to read a text file containing integers and search a
particular item from the list using binary search.
First, create a text file named
integers.txt with the following content:% Read integers from the text filefileID = fopen('integers.txt', 'r');integers = fscanf(fileID, '%d');fclose(fileID);% Define the item to search foritem = 11;% Perform binary searchleft = 1;right = length(integers);found = false;while left <= rightmid = floor((left + right) / 2);if integers(mid) == itemfound = true;break;elseif integers(mid) < itemleft = mid + 1;elseright = mid - 1;endend% Display the resultif founddisp(['Item ', num2str(item), ' found at index ', num2str(mid)]);elsedisp(['Item ', num2str(item), ' not found in the list']);end (code-box)
.jpg)
