LAB 1
LAB 1
1.2 Method I:
1. To find gradient of ϕ = x 2y + 2xz − 4
// Define the function phi
function val=phi(x, y, z)
val = x^2 * y + 2 * x * z - 4;
endfunction
function dphidy=dphi_dy(x, y, z)
dphidy = x^2;
endfunction
function dphidz=dphi_dz(x, y, z)
dphidz = 2 * x;
endfunction
10.
1.
2.
function Fy=Fy_func(x, y, z)
Fy = y^2 * z * x;
endfunction
function Fz=Fz_func(x, y, z)
Fz = z^2 * x * y;
endfunction
disp("Divergence of F at (1,2,3):");
disp(divF);
"Divergence of F at (1,2,3):"
36.
function Fy=Fy_func(x, y, z)
Fy = y^2 * z * x;
endfunction
function Fz=Fz_func(x, y, z)
Fz = z^2 * x * y;
endfunction
function dFy_dz=dFy_dz_func(x, y, z)
dFy_dz = y^2 * x;
endfunction
function dFz_dx=dFz_dx_func(x, y, z)
dFz_dx = z^2 * y;
endfunction
function dFx_dz=dFx_dz_func(x, y, z)
dFx_dz = x^2 * y;
endfunction
function dFy_dx=dFy_dx_func(x, y, z)
dFy_dx = y^2 * z;
endfunction
function dFx_dy=dFx_dy_func(x, y, z)
dFx_dy = x^2 * z;
endfunction
disp("Curl of F at (1,2,3):");
disp(curlF);
"Curl of F at (1,2,3):"
5.
-16.
9.
Method II:
function dphi_dy=dphi_dy_func(x, y, z)
dphi_dy = x^2 * z;
endfunction
function dphi_dz=dphi_dz_func(x, y, z)
dphi_dz = x^2 * y;
endfunction
12.
3.
2.
∂ Q −∂ P
∮ ( P dx+Qdy )= ∬ ( ∂ x ¿ ∂y
)dxdy . ¿
1. Using Green’s theorem, evaluate ∮ [(x+ 2 y )dx+(x−2 y ) dy ],where c is the region
bounded by coordinate axes and the line x = 1 and y = 1.
// Define the bounds of the region
x0 = 0; x1 = 1;
y0 = 0; y1 = 1;
sum = 0;
for i = 0:N-1
for j = 0:N-1
x = x0 + i*hx;
y = y0 + j*hy;
sum = sum + integrand(x, y) * hx * hy;
end
end
-1.0000000
2.Using Green’s theorem, evaluate ∮ [(xy + y )dx + x dy ],where c is the closed curve bounded by
2 2
y = x and y = x 2.
deff('z = integrand(x, y)', 'z = x - 2*y');
x0 = 0;
x1 = 1;
N = 100; // resolution
hx = (x1 - x0)/N;
sum = 0;
for i = 0:N-1
x = x0 + i*hx;
y_lower = x^2;
y_upper = x;
hy = (y_upper - y_lower)/N;
for j = 0:N-1
y = y_lower + j*hy;
sum = sum + integrand(x, y) * hy * hx;
end
end
out put:
-0.0496583
// Display results
disp("Matrix A:");
disp(A);
disp("Rank of A:");
disp(rankA);
disp("Nullity of A:");
disp(nullity);
disp("Rank + Nullity:");
disp(rankA + nullity);
disp("Number of columns (dimension of domain):");
disp(nCols);
out put
Matrix A:"
1. 4. 7.
2. 5. 8.
3. 6. 9.
"Rank of A:"
2.
"Nullity of A:"
1.
"Rank + Nullity:"
3.
"Number of columns (dimension of domain):"
3.
2) Dimension of Vector Space Find the dimension of subspace spanned by the vectors
(1, 2, 3),(2, 3, 1) and (3, 1, 2).
// Define the vectors as rows of a matrix
A = [1 2 3;
2 3 1;
3 1 2];
// Display matrix A
disp("Matrix A formed by the vectors:");
disp(A);
out put
Matrix A formed by the vectors:"
1. 2. 3.
2. 3. 1.
3. 1. 2.
"Dimension of the subspace spanned by the vectors:"
3.
2.3 Graphical representation of a transformation
2.3.1 Horizontal stretch:
Represent the horizontal stretch transformation T : R2ßR2
geometrically Find the image of vector (10,0) when it is stretched
horizontally by 2 units.
// Horizontal Stretch in R2 using Scilab
clc;
clear;
// Original vector
v = [10; 0];
// Apply transformation
v_transformed = T * v;
// Plotting
clf();
xgrid();
o/p:
2.3.2 Reflection:
Represent the reflection transformation T : R2 → R2
geometrically. Find the image of vector (10,0) when it is reflected
about y axis
clc;
clear;
// Annotate vectors
xstring(v(1), v(2) + 0.5, "Original (10, 0)");
xstring(v_reflected(1), v_reflected(2) + 0.5, "Reflected (-10, 0)");
// Add legend
legend(["Original Vector", "Reflected Vector"]);
o/p:
2.3.3 Rotation:
Represent the rotation transformation T : R2 → R2
geometrically. Find the image of vector (10,0) when it is rotated
by π/2 radians
clc;
clear;
// Annotate vectors
xstring(v(1), v(2) + 0.5, "Original (10, 0)");
xstring(v_rotated(1) + 0.5, v_rotated(2), "Rotated (0, 10)");
// Add legend
legend(["Original Vector", "Rotated Vector"]);
o/p:
// Original vector
v = [2; 3];
// Apply transformation
v_sheared = T * v;
// Plotting
clf();
xgrid();
xtitle("Shear Transformation (X-shear, k = 1)");
// Add labels
xlabel("x-axis");
ylabel("y-axis");
// Equal scaling
a = gca();
a.isoview = "on";
// Annotate vectors
xstring(v(1) + 0.2, v(2) + 0.2, "Original (2, 3)");
xstring(v_sheared(1) + 0.2, v_sheared(2) + 0.2, "Sheared (5, 3)");
// Add legend
legend(["Original Vector", "Sheared Vector"]);
o/p:
3.2 Represent the Laplace transform of f(t) = sin 2t, both in time and frequency domains
// Scilab code to plot time and frequency domain of f(t) = sin(2t)
clc;
clear;
out put:
3.3 Represent the Laplace transform of f(t) = e 2t , both in time and frequency
domains
clc;
clear;
out put:
LAB 4: Computing Laplace transform and inverse Laplace transform
of standard functions
4.1 Objectives:
1. to compute Laplace transform using in-built function.
2. to compute inverse Laplace transform using in-built function
4.2 Laplace Transform
clc;
clear;
clear;
out put:
"Inverse Laplace of F(s) = 1/s is f(t) = 1"
"Inverse Laplace of F(s) = 1/s^2 is f(t) = t"
"Inverse Laplace of F(s) = 1 / (s - 2) is f(t) = e^(2*t)"
"Inverse Laplace of F(s) = 3 / (s^2 + 3^2) is f(t) = sin(3*t)"
"Inverse Laplace of F(s) = s / (s^2 + 3^2) is f(t) = cos(3*t)"
out put:
"Laplace Transform of f(t) = t * cos(4*t) is:"
"F(s) = (s^2 - 16) / ((s^2 + 16)^2)"
out put:
"Laplace Transform of f(t) = sin(2*t)/t is:"
"F(s) = arctan(2 / s)"
convolution f ⊛ g is given by (f ⊛ g)(t) = Z t 0 f(τ )g(t − τ )dτ The Convolution Theorem states that L(f
2. to verify Convolution Theorem for two given functions. Let f(t), g(t) be two functions, then the
⊛ g) = L(f)L(g).
1. Find the Laplace transform of the convolution of the functions f(t) = t and g(t) = e^t .
// Scilab code to compute Laplace transform of convolution of f(t) = t and g(t) = e^t
clc;
clear;
// Display result
disp("Laplace Transform of f*g is: ");
disp(L_conv);
endfunction
out put:
"(1/(s^2))*(1/(s - 1))"
2. Verify the Convolution Theorem for Laplace transform of the functions f(t) = t and g(t) =
e^t
// Scilab code to verify Convolution Theorem for f(t)=t and g(t)=e^t
clc;
clear;
out put:
t = 1, f*g(t) = 0.718282
t = 2, f*g(t) = 4.389056
t = 3, f*g(t) = 16.085537
t = 4, f*g(t) = 49.598150
t = 5, f*g(t) = 142.413159
"(1/(s^2))*(1/(s - 1))"
OUT PUT:
"Iteration: 1 x2 = 2.0588235"
"Iteration: 2 x2 = 2.0812637"
"Iteration: 3 x2 = 2.0896392"
"Iteration: 4 x2 = 2.0927396"
"Iteration: 5 x2 = 2.0938837"
OUT PUT;
"Iteration: 1 x1 = 0.620016"
"Iteration: 2 x1 = 0.6071207"
"Iteration: 3 x1 = 0.6071016"
"Iteration: 4 x1 = 0.6071016"
"Root is approximately: 0.6071016"
// Regula-Falsi method
function root=regula_falsi(a, b, tol)
// Check if the function changes sign at the endpoints
if f(a) * f(b) > 0 then
disp("No sign change, cannot apply Regula-Falsi.");
root = NaN;
return;
end
// Initialize variables
fa = f(a);
fb = f(b);
c = a; // Initial guess
// Iteration loop
while abs(b - a) > tol
// Compute the new point c using the Regula-Falsi formula
c = (a * fb - b * fa) / (fb - fa);
fc = f(c);
// Call the method with the interval [0, 1] and tolerance of 1e-3
a = 0;
b = 1;
tolerance = 1e-3;
root = regula_falsi(a, b, tolerance);
// Newton-Raphson method
function root=newton_raphson(x0, iterations)
x = x0;
for i = 1:iterations
// Compute the next approximation
x = x - f(x) / df(x);
disp("Iteration " + string(i) + ": " + string(x)); // Display iteration
result
end
root = x; // Return the final root approximation
endfunction
// Initial guess near 1 (since the expected root is around 1.856, start from
1)
x0 = 1.5; // You could also start from 1, but 1.5 is a better initial guess
based on the answer
iterations = 4;
out put:
"Iteration 1: 1.215"
"Iteration 2: 1.0588339"
"Iteration 3: 1.0059849"
"Iteration 4: 1.0000705"
"The root after 4 iterations is: "
1.0000705
LAB 7: Interpolation /Extrapolation using Newton’s forward and
backward difference formula
7.1 Objectives:
// Step size
h = x(2) - x(1);
for i = 2:n
yo= yo+ (term * delta(1,i)) / factorial(i-1);
term = term * (r - (i-1)); // Update the term for next iteration
end
out put:
6. 4. 48. 48. 0.
10. 52. 96. 48. 0.
62. 148. 144. 0. 0.
210. 292. 0. 0. 0.
502. 0. 0. 0. 0.
"Interpolated value at x = 2 is: "
5.
out put
Backward Difference Table:"
6. 0. 0. 0. 0.
10. 4. 0. 0. 0.
62. 52. 48. 0. 0.
210. 148. 96. 48. 0.
502. 292. 144. 48. 0.
"Interpolated value at x = 8:"
335.
LAB 8: Computation of area under the curve using Trapezoidal, Simpson’s 1/3
rd and Simpsons 3/8 th rule
8.1 Objectives:
1. to find area under the curve represented by a given function using Trapezoidal rule.
2. to find area under the curve represented by a given function using Simpson’s 1/3 rd rule.
3. to find area under the curve represented by a given function using Simpson’s 3/8 th rule.
4. to find the area below the curve when discrete points on the curve are given.
out put:
"The integral value is: "
1.3731041
h = (b - a) / n; // Step size
sum = f(a) + f(b); // Start with f(a) and f(b)
out put:
"The integral value is: "
1.3714540
h = (b - a) / n; // Step size
sum = f(a) + f(b); // Start with f(a) and f(b)
out put:
"The integral value is: "
1.3138116
// Initialize variables
x0 = 0; // Initial x
y0 = 1; // Initial condition y(0) = 1
h = 0.1; // Step size
// Display results
disp("At x = " + string(x) + ", y = " + string(y_approx));
end
"At x = 0.1, y = 1.1113333"
"At x = 0.2, y = 1.2506667"
"At x = 0.3, y = 1.426"
// Initial conditions
x0 = 1; // Initial value of x
y0 = 2; // Initial value of y
h = 0.2; // Step size
x_values = [1.2, 1.4]; // Values of x for which we need to find y
// Start at x0 = 1, y0 = 2
x = x0;
y = y0;
1. To write a python program to solve first order differential equation using 4th order Runge Kutta
method.
2. To write a python program to solve first order differential equation using Milne’s predictor and
corrector method.
Apply the Runge Kutta method of fourth order to find the solution of dy/dx =
3x+y/2 at y(0.2) taking h = 0.2. Given that y(0) = 1.
// Define the differential equation: dy/dx = 3x + y/2
function dydx=f(x, y)
dydx = 3 * x + y / 2;
endfunction
// Initial conditions
x0 = 0; // Initial value of x
y0 = 1; // Initial value of y
h = 0.2; // Step size
x_target = 0.2; // Target value of x where we need to find y
y0 = 0;
y1 = 0.02;
y2 = 0.0795;
y3 = 0.1762;