MATLAB Programming Solutions
1. Write a program to demonstrate the use of different types of variables.
a = 10;
b = 'H';
c = "Hello World";
d = true;
% Displaying types
fprintf('Value: %d, Type: ', a); disp(class(a));
fprintf('Value: %s, Type: ', b); disp(class(b));
fprintf('Value: %s, Type: ', c); disp(class(c));
fprintf('Value: %d, Type: ', d); disp(class(d));
Output:
Value: 10, Type: double
Value: H, Type: char
Value: Hello World, Type: string
Value: 1, Type: logical
2. Write a program to demonstrate the use of elementary math built-in
functions.
x = 25;
y = 10;
disp(['Square root of 25: ', num2str(sqrt(x))]);
disp(['Exponential (e^1): ', num2str(exp(1))]);
disp(['Natural Log (ln) of 10: ', num2str(log(y))]);
disp(['Log base 10 of 10: ', num2str(log10(y))]);
disp(['Cosine of pi: ', num2str(cos(pi))]);
Output:
Square root of 25: 5
Exponential (e^1): 2.7183
Natural Log (ln) of 10: 2.3026
Log base 10 of 10: 1
Cosine of pi: -1
3. Write a program to demonstrate the use of various arithmetic
operators.
a = 15;
b = 4;
disp(['Addition: ', num2str(a + b)]);
disp(['Subtraction: ', num2str(a - b)]);
disp(['Multiplication: ', num2str(a * b)]);
disp(['Division: ', num2str(a / b)]);
disp(['Power (a^b): ', num2str(a ^ b)]);
disp(['Modulus (Remainder): ', num2str(mod(a, b))]);
Output:
Page 1 of 8
Addition: 19
Subtraction: 11
Multiplication: 60
Division: 3.75
Power (a^b): 50625
Modulus (Remainder): 3
4. Write a program to demonstrate the use of various relational and
logical operators.
x = 10;
y = 20;
disp(['Is x equal to y? ', num2str(x == y)]);
disp(['Is x not equal to y? ', num2str(x ~= y)]);
disp(['Is x < y AND x > 5? ', num2str(x < y && x > 5)]);
disp(['Is x > y OR y > 15? ', num2str(x > y || y > 15)]);
Output:
Is x equal to y? 0
Is x not equal to y? 1
Is x < y AND x > 5? 1
Is x > y OR y > 15? 1
5. Write a program to create a menu-driven program using switch-case
statement.
choice = 2;
a = 10; b = 5;
disp('1. Add');
disp('2. Subtract');
switch choice
case 1
disp(['Result: ', num2str(a + b)]);
case 2
disp(['Result: ', num2str(a - b)]);
otherwise
disp('Invalid choice');
end
Output:
1. Add
2. Subtract
Result: 5
6. Write a program to perform basic operations on one-dimensional /
multi-dimensional array.
vec = [1, 2, 3, 4, 5];
disp('Sum of vector:');
disp(sum(vec));
arr = [1, 2; 3, 4];
Page 2 of 8
disp('Element at row 2, col 1:');
disp(arr(2,1));
Output:
Sum of vector:
15
Element at row 2, col 1:
3
7. Write a program to perform basic operations on a matrix.
A = [1, 2; 3, 4];
B = [1, 0; 0, 1]; % Identity matrix
disp('Matrix Addition:');
disp(A + B);
disp('Matrix Multiplication (A * B):');
disp(A * B);
disp('Transpose of A:');
disp(A');
Output:
Matrix Addition:
2 2
3 5
Matrix Multiplication (A * B):
1 2
3 4
Transpose of A:
1 3
2 4
8. Write a program to create and use sub-matrices of a given matrix.
M = [10, 20, 30; 40, 50, 60; 70, 80, 90];
subM = M(1:2, 1:2);
disp('Original Matrix:');
disp(M);
disp('Sub-matrix (Rows 1-2, Cols 1-2):');
disp(subM);
Output:
Original Matrix:
10 20 30
40 50 60
70 80 90
Sub-matrix (Rows 1-2, Cols 1-2):
10 20
40 50
Page 3 of 8
9. Write a program to demonstrate the use of if and if-else statement.
num = -5;
if num > 0
disp('Number is Positive');
elseif num == 0
disp('Number is Zero');
else
disp('Number is Negative');
end
Output:
Number is Negative
10. Write a program to demonstrate the use of while loop.
count = 1;
disp('Counting:');
while count <= 3
disp(count);
count = count + 1;
end
Output:
Counting:
1
2
3
11. Write a program to demonstrate the use of for loop.
disp('Square numbers:');
for i = 1:4
disp(i^2);
end
Output:
Square numbers:
1
4
9
16
12. Write a program to demonstrate the use of nested loops.
disp('Matrix coordinates (i, j):');
for i = 1:2
for j = 1:2
fprintf('(%d, %d) ', i, j);
end
fprintf('\n'); % New line
end
Page 4 of 8
Output:
Matrix coordinates (i, j):
(1, 1) (1, 2)
(2, 1) (2, 2)
13. Write a program to sort a vector in ascending / descending order.
v = [5, 1, 9, 3];
asc = sort(v, 'ascend');
desc = sort(v, 'descend');
disp('Ascending:'); disp(asc);
disp('Descending:'); disp(desc);
Output:
Ascending:
1 3 5 9
Descending:
9 5 3 1
14. Write a program to create and use user-defined functions.
result = calculateArea(5);
disp(['Area of circle: ', num2str(result)]);
function area = calculateArea(radius)
area = 3.14159 * radius * radius;
end
Output:
Area of circle: 78.5398
15. Write a program to swap elements of a vector using function.
v = [10, 20, 30, 40];
disp('Before Swap:'); disp(v);
v = swapVec(v, 2, 4);
disp('After Swapping index 2 and 4:'); disp(v);
function vec = swapVec(vec, idx1, idx2)
temp = vec(idx1);
vec(idx1) = vec(idx2);
vec(idx2) = temp;
end
Output:
Before Swap:
10 20 30 40
After Swapping index 2 and 4:
10 40 30 20
Page 5 of 8
16. Write a program to create and use sub-functions.
total = mainCalc(3, 4);
disp(['Result: ', num2str(total)]);
function c = mainCalc(a, b)
c = addSquares(a, b);
end
function s = addSquares(x, y)
s = (x^2) + (y^2);
end
Output:
Result: 25
17. Write a program to create and access strings.
str1 = "Hello";
str2 = "MATLAB";
fullStr = str1 + " " + str2;
disp('Concatenated String:');
disp(fullStr);
disp(['Length of string: ', num2str(strlength(fullStr))]);
Output:
Concatenated String:
Hello MATLAB
Length of string: 12
18. Write a program to plot a 2-D plot with given specifications.
x = 0:0.1:2*pi;
y = sin(x);
figure;
plot(x, y, '-r', 'LineWidth', 2);
title('Sine Wave');
xlabel('Time');
ylabel('Amplitude');
grid on;
disp('Plot generated.');
Output:
Plot generated.
(A window pops up showing a red sine wave on a grid).
19. Write a program to plot a 3-D plot with given specifications.
[X, Y] = meshgrid(-2:0.1:2, -2:0.1:2);
Z = X .* exp(-X.^2 - Y.^2);
figure;
Page 6 of 8
surf(X, Y, Z);
title('3D Surface Plot');
disp('3D Plot generated.');
Output:
3D Plot generated.
(A window pops up showing a multi-colored 3D hill/valley surface).
20. Write a program to solve a given differential equation.
syms y(t)
eqn = diff(y,t) == -2*y;
sol = dsolve(eqn);
disp('General Solution:');
disp(sol);
Output:
General Solution:
C1*exp(-2*t)
21. Write a program to perform differentiation of given equation.
syms x
f = x^3 + 2*x^2 + 5;
df = diff(f);
disp('Original:'); disp(f);
disp('Derivative:'); disp(df);
Output:
Original:
x^3 + 2*x^2 + 5
Derivative:
3*x^2 + 4*x
22. Write a program to perform integration of given equation.
syms x
f = 2*x;
integral_f = int(f);
disp('Integral of 2x:');
disp(integral_f);
Output:
Integral of 2x:
x^2
23. Write a program to solve a given polynomial.
Page 7 of 8
coeffs = [1, -5, 6];
r = roots(coeffs);
disp('Roots of x^2 - 5x + 6:');
disp(r);
Output:
Roots of x^2 - 5x + 6:
3
2
24. Write a program to find Eigen values and Eigen vector.
A = [2, 0; 0, 3];
[V, D] = eig(A);
disp('Eigenvalues (Diagonal D):');
disp(diag(D));
disp('Eigenvectors (Columns of V):');
disp(V);
Output:
Eigenvalues (Diagonal D):
2
3
Eigenvectors (Columns of V):
1 0
0 1
25. Write a program to demonstrate try-catch block.
a = [10, 20];
try
disp('Attempting to access index 5...');
val = a(5); % This doesn't exist
catch
disp('Error caught: Index exceeds array bounds.');
end
Output:
Attempting to access index 5...
Error caught: Index exceeds array bounds.
Page 8 of 8