MATLAB code for tasks
give matlab code
Write a program to solve linear equations:
● 2x+3y−z=7
● 4x−y+5z=3
● −6x+2y+z=−5
b) Write a MATLAB program to check whether two
strings are anagrams
of each other. (e.g., "listen" and "silent" are anagrams)
5+5 CO1,CO3 BTL2,BTL3
2.
a) Write a user-defined anonymous function in
MATLAB, with one
positive integer input argument and one output
argument to compute the
nth Fibonacci number.
b) Write a matlab program to perform following
operation using 4x4
matrix. Assume any numbers
i. Scalar Operations of Matrices
ii. Transpose of a Matrix
iii. Concatenating Matrices
iv. Matrix Multiplication
v. Inverse of a Matrix
use functions in each
Sure! I’ll provide a complete MATLAB code for all your tasks, using functions
wherever needed. I’ll break it down task by task.
1(a) Solve Linear Equations
Equations:
diff
2x + 3y - z = 7
4x - y + 5z = 3
1/10
-6x + 2y + z = -5
matlab
function solve_linear_equations()
% Coefficient matrix
A = [2 3 -1;
4 -1 5;
-6 2 1];
% Right-hand side vector
B = [7; 3; -5];
% Solve for x, y, z
X = A\B;
disp('Solution for [x; y; z] is:');
disp(X);
end
Call this function with:
matlab
solve_linear_equations()
1(b) Check if Two Strings are Anagrams
matlab
function check_anagram(str1, str2)
if length(str1) ~= length(str2)
disp('Not anagrams');
return;
end
if isequal(sort(lower(str1)), sort(lower(str2)))
disp('Strings are anagrams');
else
disp('Strings are not anagrams');
2/10
end
end
Call example:
matlab
check_anagram('listen','silent')
check_anagram('hello','world')
2(a) Fibonacci Number Using Anonymous Function
matlab
function nth_fibonacci()
fib = @(n) arrayfun(@(k) fibonacci_recursive(k), 1:n, 'UniformOutput', true);
n = input('Enter a positive integer n: ');
fprintf('Fibonacci number F(%d) is: %d\n', n, fibonacci_recursive(n));
end
function f = fibonacci_recursive(n)
if n == 1
f = 0;
elseif n == 2
f = 1;
else
f = fibonacci_recursive(n-1) + fibonacci_recursive(n-2);
end
end
2(b) Matrix Operations on a 4x4 Matrix
matlab
function matrix_operations()
A = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16];
B = [16 15 14 13; 12 11 10 9; 8 7 6 5; 4 3 2 1];
% i. Scalar operation
3/10
scalar = 5;
disp('Scalar multiplication of A:');
disp(A*scalar);
% ii. Transpose
disp('Transpose of A:');
disp(A');
% iii. Concatenation
disp('Concatenate A and B horizontally:');
disp([A B]);
disp('Concatenate A and B vertically:');
disp([A; B]);
% iv. Matrix multiplication
disp('Matrix multiplication A*B:');
disp(A*B);
% v. Inverse of A (use pinv for non-invertible matrix)
disp('Inverse of A:');
disp(pinv(A));
end
Call this function with:
matlab
matrix_operations()
✅ This covers all your questions with functions and anonymous functions where
required.
If you want, I can also combine all tasks into a single MATLAB script with menus so
you can run any part interactively.
Do you want me to do that?
use user input
4/10
Got it 👍 You want the MATLAB programs to take user input instead of hardcoding
values.
I’ll rewrite each part with input() so you can enter equations, strings, numbers, or
matrices at runtime.
1(a) Solve Linear Equations with User Input
matlab
function solve_linear_equations()
% User input coefficient matrix (3x3)
A = input('Enter the 3x3 coefficient matrix A: ');
% User input RHS vector (3x1)
B = input('Enter the 3x1 RHS vector B: ');
% Solve linear system
X = A\B;
disp('Solution [x; y; z] is:');
disp(X);
end
Example run:
matlab
solve_linear_equations()
% Enter A = [2 3 -1; 4 -1 5; -6 2 1]
% Enter B = [7; 3; -5]
1(b) Check if Two Strings are Anagrams
matlab
function check_anagram()
str1 = input('Enter first string: ','s');
str2 = input('Enter second string: ','s');
5/10
if length(str1) ~= length(str2)
disp('Not anagrams');
return;
end
if isequal(sort(lower(str1)), sort(lower(str2)))
disp('Strings are anagrams');
else
disp('Strings are not anagrams');
end
end
2(a) Fibonacci using Anonymous Function
matlab
function nth_fibonacci()
n = input('Enter a positive integer n: ');
% Anonymous function that calls recursive Fibonacci
fib = @(k) fibonacci_recursive(k);
fprintf('Fibonacci number F(%d) is: %d\n', n, fib(n));
end
function f = fibonacci_recursive(n)
if n == 1
f = 0;
elseif n == 2
f = 1;
else
f = fibonacci_recursive(n-1) + fibonacci_recursive(n-2);
end
end
2(b) Matrix Operations (User Input)
matlab
6/10
function matrix_operations()
% User input 4x4 matrix
A = input('Enter a 4x4 matrix A: ');
B = input('Enter another 4x4 matrix B: ');
scalar = input('Enter a scalar value: ');
% i. Scalar operation
disp('Scalar multiplication of A:');
disp(A*scalar);
% ii. Transpose
disp('Transpose of A:');
disp(A');
% iii. Concatenation
disp('Concatenate A and B horizontally:');
disp([A B]);
disp('Concatenate A and B vertically:');
disp([A; B]);
% iv. Matrix multiplication
disp('Matrix multiplication A*B:');
disp(A*B);
% v. Inverse of A
if det(A) ~= 0
disp('Inverse of A:');
disp(inv(A));
else
disp('Matrix A is singular, using pseudo-inverse instead:');
disp(pinv(A));
end
end
👉 Now each program uses user input.
Do you want me to combine all of these into one single menu-driven MATLAB
script (so you can choose option 1, 2, 3, 4)?
7/10
give code for palindrome
Got it 👍 You want a MATLAB program to check whether a given string (or number) is
a palindrome.
Here’s a simple function using user input:
MATLAB Code – Palindrome Checker
matlab
function check_palindrome()
str = input('Enter a string or number: ','s'); % take input as string
% Convert to lowercase and remove spaces (optional for sentences)
cleanStr = lower(regexprep(str,'\s+',''));
% Check palindrome by comparing with its reverse
if strcmp(cleanStr, fliplr(cleanStr))
disp('It is a palindrome');
else
disp('It is not a palindrome');
end
end
✅ Example Runs
csharp
>> check_palindrome
Enter a string or number: madam
It is a palindrome
>> check_palindrome
Enter a string or number: 12321
It is a palindrome
>> check_palindrome
8/10
Enter a string or number: hello
It is not a palindrome
👉 Do you want me to also extend this so it can check multiple inputs (like a list of
words/numbers) in one run?
give matlab code for factorial
Perfect 👍 You want MATLAB code to calculate the factorial of a number using user
input.
I’ll give you two versions: iterative and recursive.
1. Factorial (Iterative Method)
matlab
function factorial_iterative()
n = input('Enter a positive integer: ');
if n < 0
disp('Factorial is not defined for negative numbers');
return;
end
fact = 1;
for i = 1:n
fact = fact * i;
end
fprintf('Factorial of %d is %d\n', n, fact);
end
2. Factorial (Recursive Method)
matlab
9/10
function factorial_recursive_main()
n = input('Enter a positive integer: ');
if n < 0
disp('Factorial is not defined for negative numbers');
return;
end
fprintf('Factorial of %d is %d\n', n, factorial_recursive(n));
end
function f = factorial_recursive(n)
if n == 0 || n == 1
f = 1;
else
f = n * factorial_recursive(n-1);
end
end
✅ Example Runs
ruby
>> factorial_iterative
Enter a positive integer: 5
Factorial of 5 is 120
>> factorial_recursive_main
Enter a positive integer: 6
Factorial of 6 is 720
Do you want me to also show a one-line MATLAB anonymous function for factorial
(like factorial = @(n) prod(1:n) ) for quick use?
10/10