Vani Daa
Vani Daa
12825502722
PROGRAM -01
AIM:-Exercises to implement the basic matrix operations in Scilab.
THEORY:-
Matrices
A matrix is a rectangular array of numbers arranged in rows and columns. Matrices are widely used in
various fields such as mathematics, physics, engineering, and computer science. The size of a matrix
is represented as m × n, where m is the number of rows and n is the number of columns.
Types of Matrices:
1. Square Matrix: A matrix where the number of rows is equal to the number of columns.
2. Diagonal Matrix: A square matrix with nonzero elements only on the diagonal.
3. Identity Matrix: A diagonal matrix with all diagonal elements equal to 1.
4. Zero Matrix: A matrix with all elements equal to zero.
5. Transpose of a Matrix: Obtained by swapping rows and columns.
6. Inverse of a Matrix: If A is a square matrix, its inverse A⁻¹ satisfies A * A⁻¹ = I.
7. Symmetric Matrix: A matrix that is equal to its transpose (A = Aᵀ).
8. Skew-Symmetric Matrix: A matrix where Aᵀ = -A.
Basic Matrix Operations:
Addition: Matrices of the same order can be added by adding corresponding elements.
Subtraction: Similar to addition but with subtraction.
Multiplication: Two matrices can be multiplied if the number of columns of the first matrix
matches the number of rows of the second.
Transpose: Rows and columns are interchanged.
Determinant: A scalar value that can be computed only for square matrices.
Inverse: Exists if the determinant is non-zero.
Introduction to Scilab
Scilab is an open-source software for numerical computation, similar to MATLAB. It is widely used
in scientific computing, engineering, and data analysis. It supports matrix operations, visualization,
and programming constructs like loops and conditionals.
EXAMPLE:-
1.Creating a Matrix
A = [1 2 3; 4 5 6; 7 8 9]
B = [9 8 7; 6 5 4; 3 2 1];
C = A + B;
D = A - B;
3. Matrix Multiplication
E = A * B;
4. Transpose of a Matrix
F = A';
5. Determinant of a Matrix
detA = det(A);
6. Inverse of a Matrix
invA = inv(A);
OUTPUT
PROGRAM -02
Vani
12825502722
THEORY:-
Eigenvalues and eigenvectors play a crucial role in linear algebra, particularly in solving systems of
equations, stability analysis, and transformations. In Scilab, a powerful open-source computational
software, we can efficiently compute eigenvalues and eigenvectors of a matrix using built-in
functions.
Definition
Given a square matrix AAA of order n×nn \times nn×n, an eigenvalue λ\lambdaλ and its
corresponding eigenvector vvv satisfy the equation:
Av=λvA v = \lambda vAv=λv
where vvv is a nonzero vector.
Eigenvalue and Eigenvector Computation in Scilab
Scilab provides the spec() function to compute eigenvalues and spec(A, "v") to compute both
eigenvalues and eigenvectors.
Syntax:
o lambda = spec(A) → Computes the eigenvalues of matrix AAA.
o [V, D] = spec(A, "v") → Returns eigenvectors in VVV and a diagonal matrix DDD
with eigenvalues.
Scilab simplifies eigenvalue and eigenvector calculations, making it a useful tool for mathematical
and engineering applications. Understanding these concepts is essential for solving various problems
in linear algebra, physics, and machine learning.
CODE:-
A = [6 2 1; 2 3 1; 1 1 1];
[lambda, V] = spec(A);
disp("Eigenvalues:");
disp(lambda);
disp("Eigenvectors:");
Vani
12825502722
disp(V);
OUTPUT:-
PROGRAM -03
Vani
12825502722
THEORY: The Gauss Elimination method is a direct method for solving a system of linear
equations. It involves transforming the system's augmented matrix into an upper triangular form and
then performing back-substitution to find the unknowns.
The Gauss-Jordan method is an extension of Gauss Elimination. It reduces the augmented matrix to
row echelon form and then to reduced row echelon form by making all elements above and below the
main diagonal zero.
The Gauss-Seidel method is an iterative technique for solving linear systems. It improves the
approximation of the solution by using previously computed values in subsequent iterations.
CODE:-
. Gauss Elimination Method
// Define the augmented matrix
A = [2, 3, 1, 1;
4, 1, 2, 2;
3, 2, 3, 3];
// Perform Gauss elimination
[n, m] = size(A);
for i = 1:n-1
for j = i+1:n
factor = A(j, i) / A(i, i);
A(j, :) = A(j, :) - factor * A(i, :);
end
end
// Back substitution
x = zeros(n, 1);
x(n) = A(n, m) / A(n, n);
for i = n-1:-1:1
x(i) = (A(i, m) - A(i, i+1:n) * x(i+1:n)) / A(i, i);
end
// Display the result
disp(x, "Solution using Gauss Elimination:");
OUTPUT:-
Vani
12825502722
Gauss-Jordan Method
// Define the augmented matrix
A = [2, 3, 1, 1; 4, 1, 2, 2; 3, 2, 3, 3];
// Perform Gauss-Jordan elimination
[n, m] = size(A);
for i = 1:n
// Make the diagonal element 1
A(i, :) = A(i, :) / A(i, i);
// Make the other elements in the column 0
for j = 1:n
if i != j
A(j, :) = A(j, :) - A(j, i) * A(i, :);
Vani
12825502722
end
end
end
// The solution is in the last column
x = A(:, m);
// Display the result
disp(x, "Solution using Gauss-Jordan:");
OUTPUT:-
. Gauss-Seidel Method
// Define the coefficients matrix and the constants vector
Vani
12825502722
A = [2, 3, 1;
4, 1, 2;
3, 2, 3];
b = [1; 2; 3];
// Initial guess
x = zeros(3, 1);
n = size(A, 1);
tolerance = 1e-6;
max_iterations = 100;
iterations = 0;
// Gauss-Seidel iteration
do
x_old = x; // Store the old values
for i = 1:n
sum1 = A(i, 1:i-1) * x(1:i-1);
sum2 = A(i, i+1:n) * x_old(i+1:n);
x(i) = (b(i) - sum1 - sum2) / A(i, i);
end
iterations = iterations + 1;
until (norm(x - x_old) < tolerance) | (iterations >= max_iterations)
OUTPUT:-
Vani
12825502722
PROGRAM -04
AIM:- . Exercises to implement the associative, commutative and distributive property in a
matrix in Scilab.
THEORY:-
In mathematics, the associative, commutative, and distributive properties are fundamental properties
of operations. When working with matrices, these properties can be applied to matrix addition and
multiplication.
1. Associative Property:
For addition: ((A + B) + C = A + (B + C))
For multiplication: ((A \cdot B) \cdot C = A \cdot (B \cdot C))
2. Commutative Property:
For addition: (A + B = B + A)
For multiplication: (A \cdot B = B \cdot A) (Note: This property does not hold for
matrix multiplication in general.)
3. Distributive Property:
For addition over multiplication: (A \cdot (B + C) = A \cdot B + A \cdot C)
CODE:-
// Define three matrices
A = [1 2; 3 4];
B = [5 6; 7 8];
C = [9 10; 11 12];
// Associative Property
LHS_add = (A + B) + C; // (A + B) + C
RHS_add = A + (B + C); // A + (B + C)
LHS_mul = (A * B) * C; // (A * B) * C
RHS_mul = A * (B * C); // A * (B * C)
// Commutative Property
comm_add = A + B - (B + A); // Should be zero matrix
// Multiplication is NOT generally commutative
// Distributive Property
LHS_dis = A * (B + C);
RHS_dis = (A * B) + (A * C);
// Display results
Vani
12825502722
OUTPUT:-
PROGRAM -05
Vani
12825502722
CODE:-
// Define a matrix
A = [2 4 -2; 1 3 1; 3 7 3];
OUTPUT:-
Vani
12825502722
PROGRAM:-06
Vani
12825502722
AIM:- Exercises to plot the functions and to find its first and second derivatives in Scilab.
THEORY:
1. Derivatives
The first derivative f′(x)f′(x) of a function f(x)f(x) represents its instantaneous rate
of change (slope of the tangent line).
The second derivative f′′(x)f′′(x) measures the curvature (how the slope itself
changes).
2. Numerical Differentiation
Since Scilab computes derivatives numerically (not symbolically), we use finite difference
approximations:
First derivative (Central Difference):
f′(x)≈f(x+h)−f(x−h)2hf′(x)≈2hf(x+h)−f(x−h)
Second derivative:
f′′(x)≈f(x+h)−2f(x)+f(x−h)h2f′′(x)≈h2f(x+h)−2f(x)+f(x−h)
where hh is a small step size (e.g., 0.0010.001).
CODE:-
// Define the function
function y = f(x)
y = x.^3 - 2*x.^2 + sin(x);
endfunction
// First derivative (central difference)
function y = derivative(f, x, h)
y = (f(x + h) - f(x - h)) / (2 * h);
endfunction
// Second derivative
function y = second_derivative(f, x, h)
y = (f(x + h) - 2*f(x) + f(x - h)) / (h^2);
endfunction
// Generate x values
x = linspace(-2, 3, 500);
h = 0.001;
// Compute derivatives
Vani
12825502722
df = derivative(f, x, h);
d2f = second_derivative(f, x, h);
// Plot all
clf(); // Clear previous plots
plot(x, f(x), 'b-', 'LineWidth', 2);
plot(x, df, 'r--', 'LineWidth', 2);
plot(x, d2f, 'g-.', 'LineWidth', 2);
xlabel("x");
ylabel("y");
title("Function and its Derivatives");
legend(["f(x)", "f''(x)", "f''''(x)"]);
OUTPUT
PROGRAM:-07
AIM: Exercises to present the data as a frequency table in SPSS.
Vani
12825502722
Theory:-
Frequency tables are fundamental tools in statistical analysis that display how often different
values or categories occur within a dataset. They serve several important purposes:
1. Data Organization: Frequency tables systematically organize raw data into
meaningful categories
2. Pattern Identification: They help identify patterns, outliers, and the distribution of
values
3. Descriptive Statistics: Provide basic counts and percentages that summarize the data
4. Data Quality Check: Allow researchers to verify data entry and identify potential
errors
Types of Frequency Tables
1. Simple Frequency Table: Shows counts for each unique value
2. Grouped Frequency Table: Groups continuous data into intervals (bins)
3. Cumulative Frequency Table: Shows running totals of frequencies
4. Relative Frequency Table: Displays proportions or percentages rather than counts
CODE:-
OUTPUT:
OUTPUT:
OUTPUT:
PROGRAM:-08
AIM: Exercises to find the outliers in a dataset in SPSS.
THEORY:
Vani
12825502722
An outlier is a data point that significantly differs from other observations in a dataset. Outliers
can occur due to:
Measurement errors (instrument malfunctions, data entry mistakes)
Natural variability (rare but valid extreme values)
Data processing issues (incorrect merging or filtering)
2. Importance of Outlier Detection
Improves model accuracy (outliers can skew statistical analyses)
Enhances data quality (helps identify errors)
Supports robust decision-making (avoids misleading conclusions)
o μμ = mean
o σσ = standard deviation
CODE:
data = [12, 15, 18, 22, 24, 25, 27, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 150];
// Example with an outlier (150)
mean_val = mean(data);
std_dev = stdev(data);
z_scores = (data - mean_val) / std_dev;
threshold = 2.5;
outliers = data(abs(z_scores) > threshold);
// Display results
disp("Z-Score Outlier Detection:");
Vani
12825502722
disp("--------------------------");
disp("Mean: " + string(mean_val));
disp("Std Dev: " + string(std_dev));
disp("Outliers (Z > " + string(threshold) + "): " + string(outliers));
OUTPUT:
PROGRAM:-09
AIM: Exercises to find the most risky project out of two mutually exclusive projects in
SPSS.
THEORY:
1. Risk Assessment for Mutually Exclusive Projects
Vani
12825502722
When comparing two mutually exclusive projects, we need quantitative measures to evaluate their
relative riskiness. Key concepts include:
Mutually Exclusive Projects: Selection of one project automatically excludes the other
Risk Measurement: Volatility of returns determines project riskiness
Decision Criteria: Higher volatility → Higher risk
CODE:
projectA = [1.5, 0.8, 1.2, -0.5, 2.1, 1.8, 0.3, 1.1, 2.4, -1.2, 1.9, 2.6];
projectB = [2.3, -1.2, 4.5, 3.2, -2.1, 5.6, 1.2, -3.4, 6.7, 2.3, -4.5, 7.8];
for i = 2:length(cumulative)
if cumulative(i) > peak then
peak = cumulative(i);
end
drawdown = peak - cumulative(i);
if drawdown > max_drawdown then
max_drawdown = drawdown;
end
end
OUTPUT:-
Vani
12825502722
PROGRAM:-10
AIM: Exercises to draw a scatter diagram, residual plots, outliers leverage and influential
data points in R.
THEORY:
Vani
12825502722
CODE:
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15]';
y = [2.1, 3.8, 5.2, 7.9, 10.1, 12.5, 13.7, 16.2, 18.1, 20.3, 5.0]';
x(11) = 15;
y(11) = 5;
// 1. Scatter diagram
scf(0);
clf();
plot(x, y, 'bo');
xtitle("Scatter Diagram", "X Variable", "Y Variable");
xgrid(color("gray"), 1, 7);
[X, Y] = reglin(x, y);
y_pred = X(1) + X(2)*x;
// Plot regression line on scatter plot
plot(x, y_pred, 'r-');
legend(["Data points"; "Regression line"]);
// 2. Residual plots
residuals = y - y_pred;
// Residuals vs Fitted values
scf(1);
clf();
plot(y_pred, residuals, 'bo');
xtitle("Residuals vs Fitted Values", "Fitted Values", "Residuals");
xgrid(color("gray"), 1, 7);
replot([min(y_pred), max(y_pred)], [0, 0], 'k--'); // Reference line at 0
// 3. Calculate leverage (hat values)
n = length(x);
p = 2; // number of parameters (intercept + slope)
X_matrix = [ones(n,1), x];
hat_matrix = X_matrix * inv(X_matrix'*X_matrix) * X_matrix';
h = diag(hat_matrix); // leverage values
Vani
12825502722
disp(high_leverage);
disp("Influential points (high Cook's distance):");
disp(high_influence);
OUTPUT:
Vani
12825502722
Vani
12825502722