PS - Lab - Manual - 4th - Sem (1) ANIL
PS - Lab - Manual - 4th - Sem (1) ANIL
Solution:
Installation of Scilab:
1. Visit the official Scilab website: https://www.scilab.org/
2. Navigate to the download section.
3. Choose the appropriate version for your operating system (Windows, macOS, or Linux).
4. Follow the installation instructions provided on the website.
5. Once installed, launch Scilab.
a) Addition of two numbers
a=2
b=3
z=a+b
disp("addition of two number is ", z)
Output:
// Matrix subtraction
function result=matrix_subtraction(A, B)
if size(A) == size(B) then
result = A - B;
else
disp('Error: Matrices must have the same size for subtraction.');
result = [];
end
endfunction
// Matrix multiplication
function result=matrix_multiplication(A, B)
if size(A, 2) == size(B, 1) then
result = A * B;
else
disp('Error: Number of columns in the first matrix must be equal to the number of rows in the
second matrix for multiplication.');
result = [];
end
endfunction
// Matrix transpose
function result=matrix_transpose(A)
result = A';
endfunction
// Example usage
A = [1 2; 3 4];
B = [5 6; 7 8];
disp('Matrix A:');
disp(A);
disp('Matrix B:');
disp(B);
disp('Matrix Addition:');
disp(matrix_addition(A, B));
disp('Matrix Subtraction:');
disp(matrix_subtraction(A, B));
disp('Matrix Multiplication:');
disp(matrix_multiplication(A, B));
Output:
// Matrix multiplication
C = A * B;
disp('Matrix Multiplication (A * B):');
disp(C);
// Scalar multiplication
D = scalar * A;
disp('Scalar Multiplication (scalar * A):');
disp(D);
// Vector multiplication
vector = [1; 2];
E = A * vector;
disp('Vector Multiplication (A * vector):');
disp(E);
Output:
function fact=factorial(n)
// Initialize factorial to 1
fact = 1;
// Compute factorial using a for loop
for i = 1:n
fact = fact * i;
end
// Display the result
disp(fact);
endfunction
// Test the factorial function
factorial(5); // Calculates the factorial of 5 and displays the result
Output:
120
g) Factorial of given number using while loop
function fact=factorial(n)
// Initialize factorial to 1
fact = 1;
Output:
120
i) Plotting a graph
n=[1,2,3,4,5,6,7,8,9,10]
plot(n)
Output:
x = [-5:0.1:5]
y = 3*x.*x - 5*x - 4
plot2d (x, y)
xlabel('x')
ylabel('y')
title('Plot of y =3*x.*x - 5*x - 4')
Output:
EXPERIMENT 2
Solution:
function theoretical_probability_demo()
// Function to demonstrate theoretical probability limits
function prob_heads=calculate_theoretical_probability(num_tosses)
// Function to calculate the theoretical probability of getting heads
// Main program
theoretical_probability_demo();
Output:
EXPERIMENT 3
Solution:
// Clear all variables, console, and close all windows
clear all;
clc;
close;
// Add legend
legend('mu=0, sigma=1', 'mu=0, sigma=0.5', 'mu=0, sigma=2', 'mu=1, sigma=1', 'mu=1, sigma=0.5',
'mu=1, sigma=2', 'mu=-1, sigma=1', 'mu=-1, sigma=0.5', 'mu=-1, sigma=2');
// Define x values
x = linspace(0, 5, 100); // Define x values from 0 to 5 with 100 points
// Create subplot
subplot(2, 1, 2);
// Plot exponential distributions
for lambda = lambda_values
y = lambda * exp(-lambda * x);
plot(x, y);
hold on;
end
xlabel('x');
ylabel('Probability Density');
title('Exponential Distributions');
legend('lambda=0.5', 'lambda=1', 'lambda=2');
grid on;
hold off;
Output:
EXPERIMENT 4
Solution:
// Sample data (number of heads in 10 coin tosses)
data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
Output:
EXPERIMENT 5
// Define the range of values for the random variable (e.g., number of events)
x_values = 0:10; // Adjust the range as needed
// Compute the probability mass function (PMF) for the Poisson distribution
pmf = exp(-lambda) * lambda .^ x_values ./ factorial(x_values);
Output:
EXPERIMENT 6
Aim: Fitting of normal distribution when parameters are given.
Solution:
// Clear all variables, console, and close all windows
clear all;
clc;
close;
Xbar = sum(V) / n;
S2 = (sum(V2) - (n * (Xbar) ** 2)) / (n - 1);
S = sqrt(S2);
Output:
EXPERIMENT 8
Aim:Fitting of Multiple Linear Regression (MLR) curve through given data set and testing of goo
dness of fit using mean error.
Solution:
// Given data set (X, y)
X = [1 2; 2 3; 3 4; 4 5; 5 6]; // Example data with two features (independent variables)
y = [2; 3; 4; 5; 6]; // Example response variable (dependent variable)
Output:
EXPERIMENT 9
Aim: Solve a Transportation problem of three variables.
Solution:
// Define supply, demand, and transportation costs
supply = [20; 30; 40]; // Supply from sources
demand = [25; 35; 30]; // Demand at destinations
cost_matrix = [3, 4, 6;
5, 7, 2;
1, 8, 3]; // Transportation costs
// Define the transportation problem
problem = struct();
problem.sense = 'min'; // Minimize the total transportation cost
problem.x0 = ones(3, 3); // Initial guess
problem.lb = zeros(3, 3); // Lower bounds for variables
problem.ub = inf(3, 3); // Upper bounds for variables
problem.A = ones(6, 9); // Constraint matrix
problem.b = [supply; demand]; // Constraint vector
// Solve the transportation problem using optimization
[x, total_cost] = optimization(cost_matrix(:), problem);
// Reshape the solution to matrix form
transport_matrix = reshape(x, 3, 3);
// Display the transportation matrix and total cost
disp('Transportation Matrix:');
disp(transport_matrix);
disp(['Total Cost: ' num2str(total_cost)]);
Output:
Transportation Matrix:
0. 0. 20.
0. 30. 0.
25. 5. 0.
Total Cost: 365.
EXPERIMENT 10
Aim: Solve an Assignment problem of three variables.
Solution:
// Define the cost matrix
cost_matrix = [10, 20, 30;
15, 25, 35;
12, 24, 36];
// Define the assignment problem
problem = struct();
problem.sense = 'min'; // Minimize the cost
problem.x0 = ones(3, 3); // Initial guess
problem.lb = zeros(3, 3); // Lower bounds for variables
problem.ub = ones(3, 3); // Upper bounds for variables
problem.A = ones(1, 9); // Constraint matrix
problem.b = [1]; // Constraint vector
// Solve the assignment problem using optimization
[x, cost] = optimization(cost_matrix(:), problem);
// Reshape the solution to matrix form
assignment_matrix = reshape(x, 3, 3);
// Display the assignment matrix and total cost
disp('Assignment Matrix:');
disp(assignment_matrix);
disp(['Total Cost: ' num2str(cost)]);
Output:
Assignment Matrix:
0. 0. 1.
0. 1. 0.
1. 0. 0.
Total Cost: 66.