1.
Assignment
2. What is the difference between array and matrix?
3. What are the five advantages and disadvantage between MATLAB and conventional
programming languages?
4. Generate and validate 3x3 Magic Matrix.
5. Difference between function and script?
Control Flow and Decision Making: Conditional Statements and Logical Operations
1. Question: Write a MATLAB script that checks if a given number is even or odd.
2. Question: Create a MATLAB script that takes a student's grade as input and displays
whether the student has passed or failed. Assume the passing grade is 50.
3. Question: Write a MATLAB script that determines if a given year is a leap year.
Loops and Iterative Structures
4. Question: Write a MATLAB script using a for loop to calculate the sum of the first
20 natural numbers.
5. Question: Create a MATLAB script using a while loop to find the factorial of a
given number.
6. Question: Write a MATLAB script to generate the first 10 terms of the Fibonacci
sequence using a for loop.
Vectorization and Array Operations
7. Question: Write a MATLAB script to add two vectors of length 5 element-wise.
8. Question: Create a MATLAB script that multiplies each element of a 3x3 matrix by 3
using vectorized operations.
9. Question: Write a MATLAB script that finds the element-wise maximum of two
matrices of the same size.
MATLAB Functions and Scripting
10. Question: Write a MATLAB function that takes an array as input and returns the
mean and standard deviation of the array.
Control Flow and Decision Making: Conditional Statements and Logical Operations
1. Solution:
matlab
Copy code
% Check if a number is even or odd
number = input('Enter a number: ');
if mod(number, 2) == 0
disp('The number is even.');
else
disp('The number is odd.');
end
2. Solution:
matlab
Copy code
% Check if the student has passed or failed
grade = input('Enter the student''s grade: ');
if grade >= 50
disp('The student has passed.');
else
disp('The student has failed.');
end
3. Solution:
matlab
Copy code
% Check if a year is a leap year
year = input('Enter a year: ');
if mod(year, 4) == 0
if mod(year, 100) == 0
if mod(year, 400) == 0
disp('The year is a leap year.');
else
disp('The year is not a leap year.');
end
else
disp('The year is a leap year.');
end
else
disp('The year is not a leap year.');
end
Loops and Iterative Structures
4. Solution:
matlab
Copy code
% Calculate the sum of the first 20 natural numbers
sum = 0;
for i = 1:20
sum = sum + i;
end
disp(['The sum of the first 20 natural numbers is: ', num2str(sum)]);
5. Solution:
matlab
Copy code
% Calculate the factorial of a given number using a while loop
number = input('Enter a number: ');
factorial = 1;
i = 1;
while i <= number
factorial = factorial * i;
i = i + 1;
end
disp(['Factorial of ', num2str(number), ' is: ',
num2str(factorial)]);
6. Solution:
matlab
Copy code
% Generate the first 10 terms of the Fibonacci sequence
n = 10;
fibonacci = zeros(1, n);
fibonacci(1) = 1;
fibonacci(2) = 1;
for i = 3:n
fibonacci(i) = fibonacci(i-1) + fibonacci(i-2);
end
disp('The first 10 terms of the Fibonacci sequence are:');
disp(fibonacci);
Vectorization and Array Operations
7. Solution:
matlab
Copy code
% Add two vectors element-wise
A = [1, 2, 3, 4, 5];
B = [6, 7, 8, 9, 10];
C = A + B;
disp('The element-wise addition of the vectors is:');
disp(C);
8. Solution:
matlab
Copy code
% Multiply each element of a 3x3 matrix by 3
A = [1, 2, 3; 4, 5, 6; 7, 8, 9];
B = A * 3;
disp('The matrix after element-wise multiplication by 3 is:');
disp(B);
9. Solution:
matlab
Copy code
% Find the element-wise maximum of two matrices
A = [1, 4, 3; 7, 5, 6; 9, 8, 2];
B = [3, 2, 5; 6, 7, 4; 8, 1, 9];
C = max(A, B);
disp('The element-wise maximum of the matrices is:');
disp(C);
MATLAB Functions and Scripting
10. Solution:
matlab
Copy code
% Function to calculate mean and standard deviation of an array
function [meanVal, stdVal] = calculateStats(array)
meanVal = mean(array);
stdVal = std(array);
end
% Using the function
data = [1, 2, 3, 4, 5];
[meanVal, stdVal] = calculateStats(data);
disp(['Mean: ', num2str(meanVal)]);
disp(['Standard Deviation: ', num2str(stdVal)]);
These questions and solutions will help students understand and apply the concepts of control
flow, loops, vectorization, and functions in MATLAB.