Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Plot No.

2, Sector 17-A, Yamuna Expressway,


Greater Noida, Gautam Buddh Nagar, U.P., India

School of Basic Sciences


(Division of Mathematics)

Lab Report
B. Tech- II Sem
Winter: 2023-24
Engineering Mathematics-II(Lab)
(C1UC222B)

Submitted by: Submitted to:


PANKAJ KUMAR
Name: AMAN KUMAR ////////////////////////////
Batch:....……………..
2023-2024

23SCSE1180379
Adm No:.................
S.No. List of Experiments
1. Revision of the Scilab: Overview, Basic syntax, Mathematical Operators, Predefined constants,
Built in functions. Conditional Statements, Loops, Matrix, and Its applications.

2. Write a SCILAB –CODE for Basic Vector Calculations, Finding Norm, Angle between two
Vectors, Unit Vector.

3. Write a SCILAB –CODE to Find Divergence using Scilab and verifying Divergence Theorem.

4. Write a SCILAB –CODE to Find Curl using Scilab and Verifying Stokes Theorem.

5. Write a SCILAB –CODE to Check LI and LD of Vectors.

6. Write a SCILAB –CODE to Verify Rank-Nullity Theorem.

7. Write a SCILAB –CODE to find Matrix associated with linear Transformations and their
corresponding operations.

8. Write a SCILAB –CODE to check Orthogonal and Orthonormal Vectors, Gramm Schmidt
Orthogonalization process.

9. Write a SCILAB –CODE to Plot Direction fields of first order differential equations.

10. Write a SCILAB –CODE to Solve Linear ODE with different initial conditions using Scilab.
11. Write a SCILAB –CODE to Solve Linear simultaneous differential equations with initial
conditions using Scilab.

12. Write a SCILAB –CODE to Solve PDE with boundary conditions using scilab.
S. Experiment Date Signature
No. (Performed) (with date)
1. Revision of the Scilab: Overview, Basic syntax,
Mathematical Operators, Predefined constants, Built in
functions. Conditional Statements, Loops, Matrix, and Its
applications.

2. Write a SCILAB –CODE for Basic Vector Calculations,


Finding Norm, Angle between two Vectors, Unit Vector.

3. Write a SCILAB –CODE to Find Divergence using Scilab


and verifying Divergence Theorem.

4. Write a SCILAB –CODE to Find Curl using Scilab and


Verifying Stokes Theorem.

5. Write a SCILAB –CODE to Check LI and LD of Vectors.

6. Write a SCILAB –CODE to Verify Rank-Nullity Theorem.

7. Write a SCILAB –CODE to find Matrix associated with


linear Transformations and their corresponding
operations.

8. Write a SCILAB –CODE to check Orthogonal and


Orthonormal Vectors, Gramm Schmidt Orthogonalization
process.

9. Write a SCILAB –CODE to Plot Direction fields of first


order differential equations.

10. Write a SCILAB –CODE to Solve Linear ODE with


different initial conditions using Scilab.

11. Write a SCILAB –CODE to Solve Linear simultaneous


differential equations with initial conditions using Scilab.

12. Write a SCILAB –CODE to Solve PDE with boundary


conditions using scilab.
AMAN KUMAR
23SCSE1180379

EXPERIMENT-1
Objective:

Revision of the Scilab: Overview, Basic syntax, Mathematical Operators, Predefined


constants, Built in functions, nditional Statements, Loops, Matrix, and Its applications.

Write a SCILAB code to find the solution of following problems:

(i) Create a row vector (matrix) with 3 elements.


Source code-

rowVector = [1, 2, 3];


disp(rowVector)

Out-put-

1. 2. 3.

(ii) Create a column vector(matrix) with 4 elements


Source code-

columnVector = [4; 5; 6; 7];


disp(columnVector)

Out-put-

4.
5.
6.
7.
(iii) Find the addition of matrices A and B where:

Source code-

A = [1, 3, 6;
5, 0, 2;
7, 4, 1];
B = [0, 5, 1;
2, 1, 7;
21, 41, 23];
sumMatrix = A + B;
disp(sumMatrix)

Out-put-

1. 8. 7.
7. 1. 9.
28. 45. 24.
1. Write a SCILAB code to check whether the given number is even or odd by
using if- else statement

Source code-
n = 7;
if modulo(n, 2) == 0 then
disp(n , " is even");
else
disp(n , " is odd");
end

Out-put-

7.

" is odd"

2. Write a SCILAB code to print the sum of first N natural numbers using for-loop.

Source code-
n = 10;
sum = 0;
for i = 1:n
sum = sum + i;
end
disp("The sum of first " + string(n) + " natural numbers is " + string(sum));

Out-put-

"The sum of first 10 natural numbers is 55"


3. Write a SCILAB code to print the table of any given number using while-
loop.

Source Code-

n=5
i = 1;
while i <= 10
disp(string(n) + " * " + string(i) + " = " + string(n*i));
i = i + 1;
end

Out-put-

"5 * 1 = 5"
"5 * 2 = 10"
"5 * 3 = 15"
"5 * 4 = 20"
"5 * 5 = 25"
"5 * 6 = 30"
"5 * 7 = 35
"5 * 8 = 40"
"5 * 9 = 45"
"5 * 10 = 50"
4 Write a SCILAB code to find the Volume and total surface area of cylinder by using
function (where r=2, h=6).

Source code-

PI = 3.14159;
r = 2;
h = 6;
vol = PI * r^2 * h;
disp("Volume of Cylinder = " + string(vol));

tsurf_ar = (2 * PI * r * h) + (2 * PI * r^2);
disp("Total Surface Area of Cylinder = " + string(tsurf_ar));

cursurf_ar = 2 * PI * r * h;
disp("Curved Surface Area of Cylinder = " + string(cursurf_ar));

Out-put-

"Volume of Cylinder = 75.39816"

"Total Surface Area of Cylinder = 100.53088"

"Curved Surface Area of Cylinder = 75.39816"


AMAN KUMAR
23SCSE1180379

EXPERIMENT-2
Objective:
Write a SCILAB –CODE for Basic Vector Calculations, Finding Norm, Angle between two
Vectors, Unit Vector.

1. Create two-row vectors a and b such that the following operations are defined and
hence find: (i) 2a-3b, (ii)2(transpose a)-3(transpose b)

Code-

a = [1, 2, 3, 4, 5];
b = [6, 7, 8, 9, 10];

result1 = 2*a - 3*b;


result2 = 2*a' – 3*b'; //Aridaman Singh 23SCSE1410165

disp(result1, "Result of 2a - 3b:");


disp(result2, "Result of 2(transpose a) - 3(transpose b):");

output-

-16. -17. -18. -19. -20.

"Result of 2a - 3b:"

-16.

-17.

-18.

-19.

-20.

"Result of 2(transpose a) - 3(transpose b):"


2. Find the angle between the following pair of vectors

(i) (0,0),(1,1) (ii) (1,2), (0,1) (iii) (0,1,0), (1,2,1) (iv) (1,2,3), (2,3,4)

Code-

function angle=calculate_angle(v1, v2)


// Calculate the dot product of the vectors
dot_product = v1 * v2'; //Aridaman Singh 23SCSE1410165
// Calculate the magnitude of the vectors
mag_v1 = sqrt(v1 * v1');
mag_v2 = sqrt(v2 * v2');
// Calculate the cosine of the angle
cos_angle = dot_product / (mag_v1 * mag_v2);
// Calculate the angle in radians
angle = acosd(cos_angle); // acosd gives the angle in degrees
endfunction

// Define the vectors


vectors = [[0 0; 1 1],[1 2; 0 1],[0 1 0; 1 2 1],[1 2 3; 2 3 4]];
// Calculate and display the angles between the vectors
for i = 1:size(vectors, "r")
v1 = vectors(i, 1);
v2 = vectors(i, 2);
angle = calculate_angle(v1, v2);
disp("The angle between vectors " + string(v1) + " and " + string(v2) + "
is: " + string(angle) + " degrees");
end

Out-put-

"The angle between vectors 0 and 0 is: Nan degrees"

"The angle between vectors 1 and 1 is: 0 degrees"


3. Draw the arrows for the following vectors:

(i) (1,2) (ii) (2,5) (iii) (0,7) (iv) (1,2,3) (v) (-1,0,1) (vi) (0,1,-1)

Code-
function draw2dArrows(vectors)

for i = 1:size(vectors, "r")


x = vectors(i, 1);
y = vectors(i, 2);
plot2d([0 x], [0 y], style=-1);
xstring(x, y, string(vectors(i, :)));
end
endfunction
function draw3dArrows(vectors)

for i = 1:size(vectors, "r")


x = vectors(i, 1); Prashant Kumar Srivastava 23SCSE1410191
y = vectors(i, 2);
z = vectors(i, 3);
plot3d([0 x], [0 y], [0 z], style=-1);
xstring(x, y, string(vectors(i, :)));
end
endfunction
vectors2d = [1 2; 2 5; 0 7];
vectors3d = [1 2 3; -1 0 1; 0 1 -1];
draw2dArrows(vectors2d);
draw3dArrows(vectors3d);

Output-
4. Draw the multiple arrows for the following vectors:

(i) x=(0,3) , y=(0,1) (ii) ) x=(0,5) , y=(0,2), z=(0,2)

Code-

// Define the vectors


x1 = [0, 3];
y1 = [0, 1];

x2 = [0, 5];
y2 = [0, 2];
z2 = [0, 2];

// Plot the arrows for the first set of vectors


figure
plot([0, x1(2)], [0, y1(2)], 'r->')
title('Arrows for Vectors x=(0,3) and y=(0,1)')
xlabel('x')
ylabel('y') Prashant Kumar Srivastava 23SCSE1410191

// Plot the arrows for the second set of vectors


figure
plot([0, x2(2)], [0, y2(2)], 'r->')
hold on
plot([0, z2(2)], [0, z2(2)], 'b->')
title('Arrows for Vectors x=(0,5), y=(0,2), and z=(0,2)')
xlabel('x')
ylabel('y')

Out-put-

(i)- (ii)-
AMAN KUMAR
23SCSE1180379

EXPERIMENT-3
Objective:
Write a SCILAB –CODE to Plot scalar and vector point function and their gradient and
Divergence.

1. Plot Scalar field and gradient of function

CODE-
// Define the function
function z=f(x, y)
z = x .* exp(-(x.^2 + y.^2));
endfunction

// Define the gradient of the function


function [dx, dy]=gradf(x, y)
dx = (1 - 2*x.^2) .* exp(-(x.^2 + y.^2));
dy = -2*x.*y .* exp(-(x.^2 + y.^2));
endfunction

// Generate x and y values


x = -2:0.1:2;
y = -2:0.1:2;

// Generate meshgrid
[X, Y] = meshgrid(x, y);

// Compute Z values
Z = f(X, Y);

// Compute gradient
[DX, DY] = gradf(X, Y);

// Plot the scalar field


scf(0);
surf(X, Y, Z);
title('Scalar field of the function');

// Plot the gradient field


scf(1);
quiver(X, Y, DX, DY);
title('Gradient field of the function');

Aridaman Singh 23SCSE1410165


OUT-PUT-
2. Plot Scalar field and gradient of function

CODE-

// Define the function


function z=f(x, y)
z = x.^2 .* y.^2;
endfunction

// Create a grid for x and y values


x = linspace(-2, 2, 50);
y = linspace(-2, 2, 50);
[X, Y] = ndgrid(x, y);

// Calculate the scalar field


Z = f(X, Y);

// Plot the scalar field


scf(0); // Create a new graphic window for scalar field plot
surf(X, Y, Z); // Plotting the surface
xlabel('x');
ylabel('y');
zlabel('f(x, y)');
title('Scalar Field');

// Calculate the gradient


[FX, FY] = grad(Z);

// Plot the gradient field


scf(1); // Create another graphic window for gradient plot
quiver(X, Y, FX, FY); // Plotting the gradient vectors
xlabel('x');
ylabel('y');
title('Gradient Field'); Aridaman Singh 23SCSE1410165

OUT-PUT-
3. Plot Vector field and Divergence of function

CODE-
function [u, v]=vectorField(x, y)
r = sqrt(x.^2 + y.^2);
u = -x ./ (r.^3);
v = -y ./ (r.^3);
endfunction

// Create a grid of points


x = linspace(-5, 5, 20);
y = linspace(-5, 5, 20);
[X, Y] = ndgrid(x, y);

// Calculate the vector field values


[u, v] = vectorField(X, Y);

// Plot the vector field


clf;
champ(X, Y, u, v); // Quiver plot for vector field

// Calculate the divergence of the vector field


divU = diff(u, 1, 2); // Partial derivative with respect to x
divV = diff(v, 1, 1); // Partial derivative with respect to y
divergence = divU + divV;

// Plot the divergence


clf;
contourf(X, Y, divergence, 20); // Contour plot for divergence
colorbar();

// Display the results


disp("Vector field (U-component):");
disp(u);
disp("Vector field (V-component):");
disp(v);
disp("Divergence of the vector field:");
disp(divergence); Aridaman Singh 23SCSE1410165

OUT-PUT-
4. Plot Vector field and Divergence of function

Code-
x = -5:0.2:5;
y = -5:0.2:5;

// Create a meshgrid for plotting


[X,Y] = ndgrid(x,y);

// Define the components of the vector field


Fx = sin(Y);
Fy = cos(X);

// Calculate divergence
divF = derivative(sin(Y)) + derivative(cos(X));

// Plotting Vector Field using quiver function


quiver(X,Y,Fx,Fy);

// Displaying Divergence on console


disp(divF, "Divergence of Function:"); Aridaman Singh 23SCSE1410165
EXPERIMENT-5 AMAN KUMAR
23SCSE1180379

Objective:

Write a SCILAB –CODE to Check LI and LD of Vectors.

1.Check whether the vectors V1= (2,-5,3), V2=(1,-3,2),V3=(2,-4,-1), V4=(1,-5,7) are linearly
dependent or independent. INPUT:

M = [2, -5, 3;
1, -3, 2;
2, -4, -1;
1, -5, 7];
if rank(M) == size(M, 1) then
disp("The vectors are linearly independent.");
else
disp(“The vectors are linearly dependent.”);
end
Aridaman Singh 23SCSE1410165

OUTPUT:

"The vectors are linearly dependent."


2.Check whether the et of vectors {1+x,x+x2,1+x2}are linearly dependent or independent. INPUT:

v1 = [1,0,1];
v2 = [1, 0, 1];
v3 = [0, 1, 1];
A = [v1; v2; v3];
r = rank(A)
if (r == size(A, 1)) then
disp("The vectors are linearly independent.");
else
disp("The vectors are linearly dependent.");
end

OUTPUT:
"The vectors are linearly dependent."

//Aridaman Singh 23SCSE1410165//


AMAN KUMAR
23SCSE1180379

EXPERIMENT-7
Objective:
Write a SCILAB –CODE to find Matrix associated with linear Transformations
and their corresponding operations.

Find the Matrix associated with following linear transformations

(A)

Where basis of domain are {(1, 2), (3,5)}and basis of co-domain {(1,3), (2,7)}

Source Code-
domain_basis = [1, 3; 2, 5];
codomain_basis = [1, 2; 3, 7];
function [result]=T(x, y)
result = [x - y; x + y];
endfunction

transformation_matrix = [];
for i = 1:2
transformed_vector = T(domain_basis(1,i), domain_basis(2,i));
coefficients = codomain_basis \ transformed_vector;
transformation_matrix = [transformation_matrix, coefficients];
end
disp(transformation_matrix); Aridaman Singh 23SCSE1410165

Out-Put-

-13. -30.

6. 14.
(B)

Where basis of domain are {(1,1,0), (-1,0,1),(1,-2,3)}and basis of co-domain {(1,1,0), (-


1,0,1),(1,-2,3)}

Source Code-

domain_basis = [1 -1 1; 1 0 -2; 0 1 3];


codomain_basis = [1 -1 1; 1 0 -2; 0 1 3];
function [result]=T(x, y, z)
result = [x + z; 2*x - y + z; -2*y + 2*z];
endfunction

transformation_matrix = [];
for i = 1:3
transformed_vector = T(domain_basis(1,i), domain_basis(2,i),
domain_basis(3,i));
coefficients = codomain_basis \ transformed_vector;
transformation_matrix = [transformation_matrix, coefficients];
end
disp(transformation_matrix); Aridaman Singh 23SCSE1410165

Out-Put-

0.3333333 0. 9.3333333

-1. 0.5 6.5

-0.3333333 0.5 1.1666667


AMAN KUMAR
23SCSE1180379

EXPERIMENT-8
Objective:
Write a SCILAB –CODE to check Orthogonal and Orthonormal Vectors, Gramm Schmidt
Orthogonalization process.

Q. Find an orthogonal set corresponding to the given sets


(a) B={(1,2),(1,-1)}
(b) B={(1,1,1),(1,0,1)}
(c ) B={(1,2,3),(2,0,3),(1,4,2)}

Sorce Code-

function Q=gram_schmidt(V)
[m, n] = size(V);
Q = zeros(m, n);
for j = 1:n
v = V(:,j);
for i = 1:j-1
rij = Q(:,i)'*V(:,j);
v = v - rij*Q(:,i);
end
rjj = norm(v);
Q(:,j) = v / rjj;
end
endfunction

// Define the given sets


B1 = [1 1; 2 -1];
B2 = [1 1; 1 0; 1 1];
B3 = [1 2 1; 2 0 4; 3 3 2];

// Apply the Gram-Schmidt process


Q1 = gram_schmidt(B1);
Q2 = gram_schmidt(B2);
Q3 = gram_schmidt(B3);

// Display the results


disp('Orthogonal set for B1:'), disp(Q1);
disp('Orthogonal set for B2:'), disp(Q2);
disp('Orthogonal set for B3:'), disp(Q3); Aridaman Singh 23SCSE1410165
Output-
"Orthogonal set for B1:"
0.4472136 0.8944272
0.8944272 -0.4472136

"Orthogonal set for B2:"


0.5773503 0.4082483
0.5773503 -0.8164966
0.5773503 0.4082483

"Orthogonal set for B3:"


0.2672612 0.581728 0.7682213
0.5345225 -0.7528245 0.3841106
0.8017837 0.3079737 -0.5121475

You might also like