Matlab Questions CO1 and CO2
Matlab Questions CO1 and CO2
Matlab Questions CO1 and CO2
4. Write a line of MATLAB code that calculates the product of x and y and
displays the result.
6. Write a for loop in MATLAB that calculates the square of each integer from
1 to 10.
7. What command would you use to access help for a specific function, such
as plot, in MATLAB?
Answer:
% Define variables
a = 20; % Integer
b = 8.5; % Floating-point number
Answer:
% Define variables
a = 20; % Integer
b = 8.5; % Floating-point number
Answer:
% Define variables
a = 20;
b = 8.5;
% Update values
a = a + 10;
b = b * 2;
% Display updated values
disp(['Updated value of a: ', num2str(a)]);
disp(['Updated value of b: ', num2str(b)]);
12. Write a program to find the complex conjugate of a complex variable
c and display the result.
Answer:
% Display result
disp(['Complex conjugate of c: ', num2str(c_conjugate)]);
13. Write a program to toggle the boolean variable d and display its new
value.
Answer
14. Write a program to find and display the square root of the updated
variable a, the exponential of the updated variable b, and the absolute
value of the complex variable c.
Answer:
% Define variables
a = 30; % Updated value of a
b = 17; % Updated value of b
c = 5 + 3i; % Complex number
% Display results
disp(['Square root of a: ', num2str(sqrtA)]);
disp(['Exponential of b: ', num2str(expB)]);
disp(['Absolute value of c: ', num2str(absC)]);
% Display results
disp('Element-wise addition of arrays:')
disp(sumArray)
disp('Element-wise subtraction of arrays:')
disp(diffArray)
disp('Element-wise multiplication of arrays:')
disp(productArray)
disp('Element-wise division of arrays:')
disp(quotientArray)
disp('Element-wise squaring of array1:')
disp(powerArray)
16. Write a program to create two 2x2 matrices, matrix1 and matrix2, and perform
matrix multiplication and element-wise multiplication on them. Display the results.
% Define matrices
matrix1 = [1 2; 3 4]; % 2x2 Matrix
matrix2 = [5 6; 7 8]; % 2x2 Matrix
% Matrix operations
matrixProduct = matrix1 * matrix2;
elementWiseMatrixProduct = matrix1 .* matrix2;
% Display results
disp('Matrix multiplication of matrix1 and matrix2:')
disp(matrixProduct)
disp('Element-wise multiplication of matrices:')
disp(elementWiseMatrixProduct)
17. Write a program to compute and display the transpose, determinant, and inverse of
matrix1.
% Define a matrix
matrix1 = [1 2; 3 4]; % 2x2 Matrix
% Transpose of matrices
transposeMatrix1 = matrix1.'; % Transpose of matrix1
% Display results
disp('Transpose of matrix1:')
disp(transposeMatrix1)
disp(['Determinant of matrix1: ', num2str(detMatrix1)])
disp('Inverse of matrix1:')
disp(inverseMatrix1)
18. Write a program to calculate and display the mean of elements in array1, the
maximum element in array2, the sum of elements in array1, and the cumulative sum
of elements in array1.
% Define arrays
array1 = [1 2 3 4]; % Row vector
array2 = [5 6 7 8]; % Row vector
% Display results
disp(['Mean of elements in array1: ', num2str(meanArray1)])
disp(['Maximum element in array2: ', num2str(maxArray2)])
disp(['Sum of elements in array1: ', num2str(sumArray1)])
disp('Cumulative sum of elements in array1:')
disp(cumsumArray1)
% Display results
disp('Element-wise squaring of array1:')
disp(powerArray)
20. Write a program to create and manipulate two 2x2 matrices, including their
transpose, determinant, and inverse, displaying all relevant results.
% Define matrices
matrix1 = [1 2; 3 4]; % 2x2 Matrix
matrix2 = [5 6; 7 8]; % 2x2 Matrix
% Display results
disp('Transpose of matrix1:')
disp(transposeMatrix1)
disp(['Determinant of matrix1: ', num2str(detMatrix1)])
disp('Inverse of matrix1:')
disp(inverseMatrix1)
% Display results
disp(['Integer variable: ', num2str(intVar)])
disp(['Floating-point variable: ', num2str(floatVar)])
disp(['Complex variable: ', num2str(complexVar)])
disp(['Boolean variable: ', num2str(boolVar)])
disp(['String variable: ', strVar])
22. Define a structure in MATLAB to hold mixed data types such as name, age,
salary, and employment status. Write a program to initialize the structure and display
the contents.
% Define variables
x = 12; % Integer
y = 3.75; % Floating-point number
z = 7 + 2i; % Complex number
% Display results
disp(['Sum of x and y: ', num2str(sumXY)])
disp(['Difference between x and the real part of z: ', num2str(diffXZ)])
disp(['Product of x and y: ', num2str(prodXY)])
disp(['Quotient of x and y: ', num2str(quotXY)])
disp(['Square of x: ', num2str(powerX)])
25. Update the values of the variables (increment and decrement), compute the
complex conjugate of the complex variable, and use built-in functions to calculate the
natural logarithm, exponential, and magnitude. Display the updated values and results.
% Define variables
x = 12; % Integer
y = 3.75; % Floating-point number
z = 7 + 2i; % Complex number