The Site is under maintenance!..Test link

Linear Algebra and Discrete Mathematics Practical

Web Technology Practical

1. Matrices and Gaussian Elimination:


Multiplication and transpose of matrix using Scilab.
Inverses of matrix in Scilab without using any inbuilt package.
Inverses of matrix in Scilab using any inbuilt package like numpy.
Linear equation with n unknowns using Gauss Elimination Method using Scilab.

SciLab
// Define matrices
A = [1 2; 3 4];
B = [5 6; 7 8];

// Matrix Multiplication
C = A * B;

// Transpose of Matrix A
At = A';
disp(C, "Matrix Multiplication A * B:");
disp(At, "Transpose of Matrix A:");



SciLab
// Define a 2x2 matrix
A = [4 7; 2 6];

// Calculate Inverse manually (using formula for 2x2 matrix)
detA = A(1,1)*A(2,2) - A(1,2)*A(2,1);
invA = (1/detA) * [A(2,2) -A(1,2); -A(2,1) A(1,1)];

disp(invA, "Inverse of A:");


SciLab
// Inbuilt function inv() for matrix inversion
A = [4 7; 2 6];
invA = inv(A);
disp(invA, "Inverse of A using inv():");
				  

SciLab
// Gaussian Elimination to solve Ax = b
A = [2 -1 1; 3 3 9; 3 3 5];
b = [8; -8; -12];

// Augmented matrix
Ab = [A b];

// Gaussian elimination
n = size(A, 1);
for i = 1:n-1
    for j = i+1:n
        factor = Ab(j,i)/Ab(i,i);
        Ab(j,:) = Ab(j,:) - factor*Ab(i,:);
    end
end

// Back substitution
x = zeros(n,1);
for i = n:-1:1
    x(i) = (Ab(i,end) - Ab(i,1:n)*x)/Ab(i,i);
end

disp(x, "Solution of the system using Gaussian Elimination:");


2. Vector:


Addition, subtraction, multiplication anddivision of vector using Scilab.
Dot product & cross product of vector using Scilab
Visualising vector Linear Transformations using Scilab.

SciLab
// Define vectors
v1 = [2, 3, 5];
v2 = [4, 6, 8];

// Addition
v_add = v1 + v2;

// Subtraction
v_sub = v1 - v2;

// Multiplication (element-wise)
v_mul = v1 .* v2;

// Division (element-wise)
v_div = v1 ./ v2;

disp(v_add, "Vector Addition:");
disp(v_sub, "Vector Subtraction:");
disp(v_mul, "Element-wise Multiplication:");
disp(v_div, "Element-wise Division:");


SciLab
// Dot Product
dot_prod = v1 * v2';

// Cross Product
cross_prod = cross(v1, v2);

disp(dot_prod, "Dot Product:");
disp(cross_prod, "Cross Product:");

				  
SciLab (page3.html)
// Define vectors
v1 = [2; 3];
v2 = [1; 4];

// Define transformation matrix
T = [2 -1; 1 2];

// Apply transformation
v1_trans = T * v1;
v2_trans = T * v2;

// Plot original and transformed vectors
clf;
plot([0 v1(1)], [0 v1(2)], '-r', 'LineWidth', 2);
plot([0 v2(1)], [0 v2(2)], '-g', 'LineWidth', 2);
plot([0 v1_trans(1)], [0 v1_trans(2)], '--r', 'LineWidth', 2);
plot([0 v2_trans(1)], [0 v2_trans(2)], '--g', 'LineWidth', 2);
xgrid();
legend("v1", "v2", "T * v1", "T * v2");


3. Matrices Transformation


Computes the orthonormal vectors using the GS algorithm using Scilab.
Projections and Least Squares using Scilab.
Fast Fourier Transform using Scilab.

SciLab
// Gram-Schmidt Process for Orthonormal Vectors
function Q = gram_schmidt(A)
    [n, m] = size(A);
    Q = zeros(n, m);
    for i = 1:m
        v = A(:, i);
        for j = 1:i-1
            v = v - (Q(:, j)' * A(:, i)) * Q(:, j);
        end
        Q(:, i) = v / norm(v);  // Normalize
    end
endfunction

// Example
A = [1 1; 1 0; 1 -1];
Q = gram_schmidt(A);
disp(Q, "Orthonormal Vectors using Gram-Schmidt:");


SciLab
// Projections and Least Squares
A = [1 1; 1 2; 1 3];
b = [1; 2; 2];

// Compute projection matrix
P = A * inv(A' * A) * A';

// Project b onto the column space of A
proj_b = P * b;

// Solve using least squares
x_ls = inv(A' * A) * A' * b;

disp(proj_b, "Projection of b:");
disp(x_ls, "Least Squares Solution:");


SciLab
// Fast Fourier Transform (FFT)
x = [1, 2, 3, 4, 5, 6, 7, 8];
X = fft(x);
disp(X, "FFT of x:");


4. Determonant of matrix:


Finding determinant of matrix in Scilab without using any inbuilt package.
Finding determinant of matrix in Scilab using any inbuilt package.

SciLab
// Function to compute determinant without inbuilt function (using recursion)
function det_A = determinant(A)
    [n, m] = size(A);
    if n ~= m then
        error("Matrix must be square.");
    end
    if n == 1 then
        det_A = A(1, 1);
    elseif n == 2 then
        det_A = A(1, 1) * A(2, 2) - A(1, 2) * A(2, 1);
    else
        det_A = 0;
        for j = 1:n
            det_A = det_A + (-1)^(1+j) * A(1, j) * determinant(A(2:$, [1:j-1 j+1:$]));
        end
    end
endfunction

// Example
A = [3 2 1; 1 0 2; 4 5 6];
det_A = determinant(A);
disp(det_A, "Determinant without inbuilt function:");


SciLab
// Finding determinant using inbuilt function
A = [3 2 1; 1 0 2; 4 5 6];
det_A = det(A);  // Inbuilt function 'det'
disp(det_A, "Determinant using inbuilt function:");


5. Eigen Vectors & Vectors:


Compute the eigenvalues and right eigen vectors of a given square array using Scilab.
Program to test diagonalizable matrix using Scilab.

SciLab
// Eigenvalues and Eigenvectors
A = [4 2; 1 3];

// Compute eigenvalues and right eigenvectors
[evectors, evalues] = spec(A);  // 'spec' function returns both eigenvalues and eigenvectors

disp(evalues, "Eigenvalues:");
disp(evectors, "Right Eigenvectors:");


SciLab
// Check if matrix is diagonalizable
A = [4 2; 1 3];

// Compute eigenvalues and eigenvectors
[evectors, evalues] = spec(A);

// Check if the matrix of eigenvectors is invertible (determinant is non-zero)
det_P = det(evectors);

if det_P ~= 0 then
    disp("Matrix is diagonalizable.");
else
    disp("Matrix is not diagonalizable.");
end


6. 6. Testing of Matrices:


Tests for Positive Definitenes using Scilab.
Singular Value Decomposition using Scilab.
The Finite Element Method using Scilab. (Only Demonstration).

SciLab
// Test for Positive Definiteness
function is_pos_def = test_positive_definite(A)
    evalues = spec(A);  // Get eigenvalues
    if all(evalues > 0) then
        is_pos_def = %T;  // True, matrix is positive definite
    else
        is_pos_def = %F;  // False, matrix is not positive definite
    end
endfunction

// Example
A = [2 -1 0; -1 2 -1; 0 -1 2];
result = test_positive_definite(A);
if result then
    disp("Matrix is Positive Definite.");
else
    disp("Matrix is not Positive Definite.");
end


SciLab
// Singular Value Decomposition (SVD)
A = [3 1 1; -1 3 1];

// Perform SVD
[U, S, V] = svd(A);  // SVD decomposition

disp(U, "U Matrix:");
disp(S, "S Matrix (Singular Values):");
disp(V, "V Matrix:");


SciLab
// Finite Element Method (FEM) Demonstration for 1D problem
function fem_demo()
    n = 4;  // Number of elements
    h = 1/n;  // Element length
    K = zeros(n+1, n+1);  // Stiffness matrix
    F = zeros(n+1, 1);    // Load vector

    // Assemble stiffness matrix and load vector
    for i = 1:n
        K(i:i+1, i:i+1) = K(i:i+1, i:i+1) + [1 -1; -1 1]/h;
        F(i:i+1) = F(i:i+1) + [1; 1] * h / 2;  // Assuming constant force f(x) = 1
    end

    % Boundary conditions (Dirichlet)
    K(1, :) = 0;
    K(1, 1) = 1;
    F(1) = 0;

    % Solve system
    U = K\F;  // Solution vector

    // Display result
    disp(U, "Solution U at nodes:");
endfunction

// Run demonstration
fem_demo();


7. Simplex Method:


Simplex Method using Scilab. (Only Demonstration).

SciLab
// Simplex Method in Scilab

// Define objective function coefficients (maximize Z = 3x1 + 2x2)
c = [-3; -2];  // Use negative for maximization

// Define the inequality constraint matrix and RHS
A = [2 1; 2 3; 3 1];
b = [18; 42; 24];

// Simplex solution using Scilab's linprog function
[x, fval, info] = linprog(c, A, b, [], [], [0; 0], []);

// Display solution
disp("Optimal values of variables (x1, x2):");
disp(x);

disp("Maximum value of objective function Z:");
disp(-fval);  // Remember to flip sign for maximization
				    
				  

8. Dual Problem Solving:


The Dual Problem using Scilab. (Only Demonstration)

SciLab
// Dual Problem in Scilab

// Primal problem: Maximize Z = 4x1 + 3x2
// Dual problem: Minimize W = 8y1 + 5y2

// Coefficients for dual problem (minimization)
c = [8; 5];  // Objective function for dual

// Coefficient matrix and bounds for dual constraints
A = [-2 -2; -3 -1];
b = [-4; -3];

// Solve using Scilab's linprog for minimization
[y, fval, info] = linprog(c, A, b, [], [], [0; 0], []);

disp("Optimal values of dual variables (y1, y2):");
disp(y);

disp("Minimum value of dual objective function W:");
disp(fval);


9. Network Modelling:


Implementing Network Models using Scilab. (Only Demonstration)

SciLab
// Network Modelling in Scilab (Dijkstra's Algorithm)

// Adjacency matrix for the graph (5 nodes)
G = [
    0 10 20 0 0;
    10 0 5 16 0;
    20 5 0 10 0;
    0 16 10 0 12;
    0 0 0 12 0
];

// Dijkstra’s Algorithm
function dist = dijkstra(G, start)
    n = size(G, 1);
    visited = zeros(n, 1);
    dist = ones(n, 1) * inf;
    dist(start) = 0;

    for i = 1:n
        [minDist, u] = min(dist .* (~visited)); // Find the minimum unvisited node
        if minDist == inf then break; end

        visited(u) = 1;
        for v = 1:n
            if G(u, v) > 0 & visited(v) == 0 then
                newDist = dist(u) + G(u, v);
                if newDist < dist(v) then
                    dist(v) = newDist;
                end
            end
        end
    end
endfunction

// Example: Find shortest path from node 1
shortest_distances = dijkstra(G, 1);
disp("Shortest distances from node 1:");
disp(shortest_distances);



10. Application:


Implementing Game Theory using Scilab. (Only Demonstration)

SciLab
// Game Theory in Scilab (Prisoner's Dilemma)

// Payoff matrix for both players
payoff_matrix = [
    [(-1, -1), (-3, 0)];
    [(0, -3), (-2, -2)]
];

// Strategy names: (Cooperate, Defect)
disp("Payoff Matrix:");
disp(payoff_matrix);

// Example: Player 1 chooses to Cooperate (1), Player 2 chooses to Defect (2)
player1_choice = 1; // 1 for Cooperate, 2 for Defect
player2_choice = 2;

result = payoff_matrix(player1_choice, player2_choice);
disp("Result (Player 1, Player 2):");
disp(result);




© Bsc Data science. All rights reserved. Developed by Jago Desain