Soft Computing Lab Labs

Download as odt, pdf, or txt
Download as odt, pdf, or txt
You are on page 1of 8

Experiment No.

2
Write a Program to Performing Mathematical Operations
>>
% input for numbers
num1 = input('Enter the first number: ');
num2 = input('Enter the second number: ');

% options for arithmetic operations


disp('Choose an arithmetic operation:');
disp('1. Addition (+)');
disp('2. Subtraction (-)');
disp('3. Multiplication (*)');
disp('4. Division (/)');

% user's choice
choice = input('Enter your choice (1-4): ');

% Selected operation
if choice == 1
result = num1 + num2;
disp(['Result of Addition: ', num2str(result)]);
elseif choice == 2
result = num1 - num2;
disp(['Result of Subtraction: ', num2str(result)]);
elseif choice == 3
result = num1 * num2;
disp(['Result of Multiplication: ', num2str(result)]);
elseif choice == 4
if num2 ~= 0
result = num1 / num2;
disp(['Result of Division: ', num2str(result)]);
else
disp('Error: Division by zero is not allowed.');
end
else
disp('Invalid choice. Please select a number between 1 and 4.');
end

RESULT ——————————————————————————————

Enter the first number:


5
Enter the second number:
7
Choose an arithmetic operation:
1. Addition (+)
2. Subtraction (-)
3. Multiplication (*)
4. Division (/)
Enter your choice (1-4):
3
Result of Multiplication: 35
>>
Experiment No. 3
Write a Program to Performing Matrix Operations

>> % MATLAB Program for Matrix Operations


% Get user input for two matrices
disp('Enter the first matrix:');
matrix1 = input(''); % e.g., [1 2; 3 4]
disp('Enter the second matrix:');
matrix2 = input(''); % e.g., [5 6; 7 8]
% Display options for matrix operations
disp('Choose a matrix operation:');
disp('1. Addition');
disp('2. Subtraction');
disp('3. Multiplication');
disp('4. Transpose');
% Get the user's choice
choice = input('Enter your choice (1-4): ');
% Perform the selected operation
if choice == 1
% Matrix Addition
if size(matrix1) == size(matrix2)
result = matrix1 + matrix2;
disp('Result of Addition:');
disp(result);
else
disp('Error: Matrices must have the same dimensions for addition.');
end
elseif choice == 2
% Matrix Subtraction
if size(matrix1) == size(matrix2)
result = matrix1 - matrix2;
disp('Result of Subtraction:');
disp(result);
else
disp('Error: Matrices must have the same dimensions for subtraction.');
end
elseif choice == 3
% Matrix Multiplication
if size(matrix1, 2) == size(matrix2, 1)
result = matrix1 * matrix2;
disp('Result of Multiplication:');
disp(result);
else
disp('Error: Number of columns in the first matrix must equal the number of rows in the second
matrix.');
end
elseif choice == 4
% Transpose of the matrices
disp('Transpose of the first matrix:');
disp(matrix1');
disp('Transpose of the second matrix:');
disp(matrix2');
else
disp('Invalid choice. Please select a number between 1 and 4.');
end
Enter the first matrix:
[1 2; 3 4]
Enter the second matrix:
[6 2; 3 7]
Choose a matrix operation: 1. Addition 2. Subtraction 3. Multiplication 4. Transpose Enter your
choice (1-4):
1
Result of Addition: 7 4 6 11
>>
Experiment No. 4
Write a Program for Direct Plotting of Trigonometric Functions

>>
% Define the range of values for x (in radians)
x = linspace(-2*pi, 2*pi, 1000); % 1000 points from -2π to 2π
% Calculate the trigonometric values
y_sin = sin(x); % Sine values
y_cos = cos(x); % Cosine values
y_tan = tan(x); % Tangent values
% Plot the functions
figure;
hold on;
plot(x, y_sin, 'b', 'LineWidth', 1.5); % Plot sine in blue
plot(x, y_cos, 'r', 'LineWidth', 1.5); % Plot cosine in red
plot(x, y_tan, 'g', 'LineWidth', 1.5); % Plot tangent in green
% Set axis limits to manage tangent asymptotes
ylim([-10 10]); % Limit y-axis to avoid extreme values of tangent
% Add labels and title
xlabel('x (radians)');
ylabel('Function value');
title('Trigonometric Functions: Sine, Cosine, and Tangent');
legend('sin(x)', 'cos(x)', 'tan(x)');
% Display the grid
grid on;
hold off;
>>

RESULT---------------------------------------------------------------------------------------

Experiment no. 4
Write a Program in to demonstrates union, intersection, and complement operations:
Experiment no. 5
Write a Program in to demonstrates union, intersection, and complement operations:

% Define two sets


A = [1, 2, 3, 4, 5];
B = [4, 5, 6, 7, 8];

% Define the universal set (for complement operation)


U = 1:10;

% Union of A and B
union_AB = union(A, B);
disp('Union of A and B:');
disp(union_AB);

% Intersection of A and B
intersection_AB = intersect(A, B);
disp('Intersection of A and B:');
disp(intersection_AB);

% Complement of A relative to U
complement_A = setdiff(U, A);
disp('Complement of A relative to U:');
disp(complement_A);
Experiment no. 6
Write a Program to implement De Morgan's Theorem

% Define two sets


A = [1, 2, 3, 4, 5];
B = [4, 5, 6, 7, 8];
% Define the universal set (for complement operation)
U = 1:10;
% Compute the complement of sets A and B relative to U
complement_A = setdiff(U, A);
complement_B = setdiff(U, B);
% Apply De Morgan's first law: (A ∪ B)' = A' ∩ B'
union_AB = union(A, B);
complement_union_AB = setdiff(U, union_AB); % (A ∪ B)'
intersection_complements = intersect(complement_A, complement_B); % A' ∩ B'
% Apply De Morgan's second law: (A ∩ B)' = A' ∪ B'
intersection_AB = intersect(A, B);
complement_intersection_AB = setdiff(U, intersection_AB); % (A ∩ B)'
union_complements = union(complement_A, complement_B); % A' ∪ B'
% Display the results
disp('De Morgan''s First Law: (A ∪ B)'' = A'' ∩ B''');
disp('Complement of (A ∪ B):');
disp(complement_union_AB);
disp('Intersection of A'' and B'':');
disp(intersection_complements);
disp('De Morgan''s Second Law: (A ∩ B)'' = A'' ∪ B''');
disp('Complement of (A ∩ B):');
disp(complement_intersection_AB);
disp('Union of A'' and B'':');
disp(union_complements);

RESULT ------------------------------------------------------------------------------------------

De Morgan's First Law: (A ∪ B)' = A' ∩ B' Complement of (A ∪ B): 9 10 Intersection of A' and B':
9 10 De Morgan's Second Law: (A ∩ B)' = A' ∪ B' Complement of (A ∩ B): 1 2 3 6 7 8 9 10 Union
of A' and B': 1 2 3 6 7 8 9 10
>>
Experiment no. 1
MATLAB and Its Toolboxes

MATLAB is a high-level programming language used for numerical computation, data analysis,
and visualization. It supports matrix operations, plotting, and simulations. Toolboxes are add-ons
that extend MATLAB’s functionality for specialized tasks.

Key MATLAB Toolboxes:


1. Signal Processing Toolbox: For signal analysis, filtering, and spectral analysis (e.g., fft,
filter).
2. Image Processing Toolbox: For image manipulation, enhancement, and segmentation (e.g.,
imfilter, edge).
3. Control System Toolbox: For designing and analyzing control systems (e.g., pid, step).
4. Optimization Toolbox: For solving optimization problems (e.g., linprog, fminunc).
5. Statistics and Machine Learning Toolbox: For statistical analysis and machine learning
(e.g., kmeans, svm).
6. Deep Learning Toolbox: For developing neural networks and deep learning models (e.g.,
trainNetwork).
7. Simulink: For modeling and simulating dynamic systems using a graphical interface.
8. Parallel Computing Toolbox: For parallel and distributed computing (e.g., parfor).
9. Bioinformatics Toolbox: For genomic and biological data analysis (e.g., seqalign).
10.Financial Toolbox: For financial analysis and modeling (e.g., blsprice).

Check Installed Toolboxes:


matlab
Copy code
ver

Conclusion:
Toolboxes add specialized functions to MATLAB, enabling users to perform complex tasks in fields
like signal processing, control systems, machine learning, and more.

You might also like