Matlab Questions CO1 and CO2

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 11

U24MA102 - Mathematics Laboratory

Program 01: Basic Console Operations

1. What function is used to display a welcome message in the MATLAB


console?

2. Write a MATLAB command to define a variable a with the value 5 and a


variable with the value 3 and write a command to find addition and square root
of b and display it.

Program 02: Simple MATLAB Script

3. Explain how you would perform addition of two variables x and y in


MATLAB

4. Write a line of MATLAB code that calculates the product of x and y and
displays the result.

Program 03: Batch Processing with a Script

5. How do you initialize an array in MATLAB to store the squares of integers


from 1 to 10?

6. Write a for loop in MATLAB that calculates the square of each integer from
1 to 10.

Program 04: Using MATLAB Help and Documentation

7. What command would you use to access help for a specific function, such
as plot, in MATLAB?

8. Describe the purpose of the doc command in MATLAB.

Program 05: Variable Creation and Operations


9. Write a program to find the sum, difference, and product of an integer
variable a and a floating-point variable b.

Answer:

% Define variables
a = 20; % Integer
b = 8.5; % Floating-point number

% Calculate sum, difference, and product


sumAB = a + b;
diffAC = a - real(5 + 3i);
productAB = a * b;
% Display results
disp(['Sum of a and b: ', num2str(sumAB)]);
disp(['Difference of a and the real part of c: ', num2str(diffAC)]);
disp(['Product of a and b: ', num2str(productAB)]);
10. Write a program to find the quotient and modulus of the integer
variable a divided by the floating-point variable b, and with respect to 3.

Answer:

% Define variables
a = 20; % Integer
b = 8.5; % Floating-point number

% Calculate quotient and modulus


quotientAB = a / b;
modulusA = mod(a, 3);
% Display results
disp(['Quotient of a and b: ', num2str(quotientAB)]);
disp(['Modulus of a divided by 3: ', num2str(modulusA)]);
11. Write a program to update the value of variable a by incrementing it
by 10 and double the value of variable b. Display both updated values.

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:

% Define a complex variable


c = 5 + 3i;

% Calculate complex conjugate


c_conjugate = conj(c);

% 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

% Define a boolean variable


d = true; % Boolean

% Toggle the boolean value


d = ~d;

% Display updated boolean value


disp(['Toggled value of d: ', num2str(d)]);

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

% Calculate square root, exponential, and absolute value


sqrtA = sqrt(a); % Square root of a
expB = exp(b); % Exponential of b
absC = abs(c); % Absolute value of c

% Display results
disp(['Square root of a: ', num2str(sqrtA)]);
disp(['Exponential of b: ', num2str(expB)]);
disp(['Absolute value of c: ', num2str(absC)]);

Program 06: Array and Matrix Operations


15. Write a program to create two 1-dimensional arrays, array1 and array2, and
perform element-wise operations: addition, subtraction, multiplication, division, and
squaring the elements of array1. Display the results.
% Define arrays
array1 = [1 2 3 4]; % Row vector
array2 = [5 6 7 8]; % Row vector

% Element-wise operations on arrays


sumArray = array1 + array2; % Element-wise addition
diffArray = array1 - array2; % Element-wise subtraction
productArray = array1 .* array2; % Element-wise multiplication
quotientArray = array1 ./ array2; % Element-wise division
powerArray = array1 .^ 2; % Element-wise squaring of array1

% 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

% Determinant and inverse of a matrix


detMatrix1 = det(matrix1); % Determinant of matrix1
inverseMatrix1 = inv(matrix1); % Inverse 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

% Use of built-in functions for arrays


meanArray1 = mean(array1); % Mean of elements in array1
maxArray2 = max(array2); % Maximum element in array2
sumArray1 = sum(array1); % Sum of elements in array1
cumsumArray1 = cumsum(array1); % Cumulative sum of elements in
array1

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

19. Write a program to demonstrate element-wise operations on two 1-dimensional


arrays, including squaring each element of the first array and displaying all results.
% Define arrays
array1 = [1 2 3 4]; % Row vector
array2 = [5 6 7 8]; % Row vector

% Element-wise squaring of array1


powerArray = array1 .^ 2; % Element-wise squaring of array1

% 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

% Transpose, determinant, and inverse of matrix1


transposeMatrix1 = matrix1.'; % Transpose
detMatrix1 = det(matrix1); % Determinant
inverseMatrix1 = inv(matrix1); % Inverse

% Display results
disp('Transpose of matrix1:')
disp(transposeMatrix1)
disp(['Determinant of matrix1: ', num2str(detMatrix1)])
disp('Inverse of matrix1:')
disp(inverseMatrix1)

Program 07: Handling Different Data Types


21. Write a MATLAB program to create and manage different data types, including
an integer, a floating-point number, a complex number, a boolean, and a string.
Display the values of each variable.

% Handling Different Data Types


% This program demonstrates the use of various data types in MATLAB.

% Define an integer variable


intVar = 10; % Integer
% Define a floating-point variable
floatVar = 3.14159; % Floating-point number
% Define a complex number variable
complexVar = 5 + 7i; % Complex number
% Define a boolean variable
boolVar = true; % Boolean value
% Define a string variable
strVar = 'Hello, MATLAB!'; % String

% 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 a structure to hold mixed data types


dataStruct.name = 'Alice'; % String
dataStruct.age = 30; % Integer
dataStruct.salary = 75000.50; % Floating-point
dataStruct.isEmployed = true; % Boolean

% Display structure data


disp('Structure data:')
disp(['Name: ', dataStruct.name])
disp(['Age: ', num2str(dataStruct.age)])
disp(['Salary: $', num2str(dataStruct.salary)])
disp(['Employed: ', num2str(dataStruct.isEmployed)])
23. Create a structure that contains fields for a person's name, age, and salary. Write a
program to initialize the structure and display each field's value. Use appropriate data
types for each field.
% Create a structure for a person's details
personStruct.name = 'Bob'; % String for name
personStruct.age = 28; % Integer for age
personStruct.salary = 58000.75; % Floating-point for salary

% Display structure data


disp('Person structure data:')
disp(['Name: ', personStruct.name])
disp(['Age: ', num2str(personStruct.age)])
disp(['Salary: $', num2str(personStruct.salary)])

Program 08: Variable Manipulation and Operations


24. Write a MATLAB program to define variables of different data types (integer,
floating-point, and complex), and perform basic arithmetic operations such as
addition, subtraction, multiplication, and division. Display the results of these
operations.

% Variable Manipulation and Operations


% This program demonstrates manipulating variables and performing operations in
MATLAB.

% Define variables
x = 12; % Integer
y = 3.75; % Floating-point number
z = 7 + 2i; % Complex number

% Perform basic arithmetic operations


sumXY = x + y; % Addition
diffXZ = x - real(z); % Subtraction using the real part of z
prodXY = x * y; % Multiplication
quotXY = x / y; % Division
powerX = x^2; % Square of x

% 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

% Update variable values


x = x + 5; % Increment x by 5
y = y - 1.25; % Decrement y
z = conj(z); % Compute the complex conjugate of z

% Use built-in functions


logX = log(x); % Natural logarithm of x
expY = exp(y); % Exponential of y
absZ = abs(z); % Magnitude of complex number z

% Display updated results


disp(['Updated value of x: ', num2str(x)])
disp(['Updated value of y: ', num2str(y)])
disp(['Complex conjugate of z: ', num2str(z)])
disp(['Logarithm of updated x: ', num2str(logX)])
disp(['Exponential of updated y: ', num2str(expY)])
disp(['Magnitude of updated z: ', num2str(absZ)])

You might also like