0% found this document useful (0 votes)
3 views30 pages

LAB 1

The document outlines various laboratory exercises focused on vector calculus and linear transformations, including finding gradients, divergences, and curls, as well as verifying Green's theorem. It also covers the rank-nullity theorem, dimension of vector spaces, and graphical representations of transformations such as horizontal stretch, reflection, rotation, and shear. The exercises include coding implementations and mathematical evaluations to demonstrate the concepts effectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views30 pages

LAB 1

The document outlines various laboratory exercises focused on vector calculus and linear transformations, including finding gradients, divergences, and curls, as well as verifying Green's theorem. It also covers the rank-nullity theorem, dimension of vector spaces, and graphical representations of transformations such as horizontal stretch, reflection, rotation, and shear. The exercises include coding implementations and mathematical evaluations to demonstrate the concepts effectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 30

LAB 1: Finding gradient, divergent, curl and their geometrical interpretation

and Verification of Green’s theorem


1.1 Objectives:
1. to find the gradient of a given scalar function.
2. to find find divergence and curl of a vector function.
3. to evaluate integrals using Green’s theorem.

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

// Define the partial derivatives manually


function dphidx=dphi_dx(x, y, z)
dphidx = 2 * x * y + 2 * z;
endfunction

function dphidy=dphi_dy(x, y, z)
dphidy = x^2;
endfunction

function dphidz=dphi_dz(x, y, z)
dphidz = 2 * x;
endfunction

// Evaluate the gradient at a point, e.g., (x=1, y=2, z=3)


x = 1;
y = 2;
z = 3;

grad = [dphi_dx(x,y,z); dphi_dy(x,y,z); dphi_dz(x,y,z)];


disp("Gradient of phi at (1,2,3):");
disp(grad);

"Gradient of phi at (1,2,3):"

10.

1.

2.

2. To find divergence of F⃗ = x 2yzˆi + y 2zxˆj + z 2xyˆk


// Define each component of F
function Fx=Fx_func(x, y, z)
Fx = x^2 * y * z;
endfunction

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

// Partial derivative of Fx with respect to x: d/dx (x^2 y z) = 2x y z


function dFx_dx=dFx_dx_func(x, y, z)
dFx_dx = 2 * x * y * z;
endfunction
// Partial derivative of Fy with respect to y: d/dy (y^2 z x) = 2y z x
function dFy_dy=dFy_dy_func(x, y, z)
dFy_dy = 2 * y * z * x;
endfunction

// Partial derivative of Fz with respect to z: d/dz (z^2 x y) = 2z x y


function dFz_dz=dFz_dz_func(x, y, z)
dFz_dz = 2 * z * x * y;
endfunction

// Evaluate divergence at a point (e.g., x = 1, y = 2, z = 3)


x = 1;
y = 2;
z = 3;

divF = dFx_dx_func(x,y,z) + dFy_dy_func(x,y,z) + dFz_dz_func(x,y,z);

disp("Divergence of F at (1,2,3):");
disp(divF);

"Divergence of F at (1,2,3):"

36.

3. To find curl of F⃗ = x 2yzˆi + y 2zxˆj + z 2xyˆk


// Define vector field components
function Fx=Fx_func(x, y, z)
Fx = x^2 * y * z;
endfunction

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

// Partial derivatives needed for curl


function dFz_dy=dFz_dy_func(x, y, z)
dFz_dy = z^2 * x;
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

// Evaluate curl at a point (e.g., x=1, y=2, z=3)


x = 1;
y = 2;
z = 3;

curl_x = dFz_dy_func(x,y,z) - dFy_dz_func(x,y,z);


curl_y = -(dFz_dx_func(x,y,z) - dFx_dz_func(x,y,z));
curl_z = dFy_dx_func(x,y,z) - dFx_dy_func(x,y,z);

curlF = [curl_x; curl_y; curl_z];

disp("Curl of F at (1,2,3):");
disp(curlF);

"Curl of F at (1,2,3):"

5.

-16.

9.

Method II:

1. To find gradient of ϕ = x 2yz


// Define the scalar field phi
function phi=phi_func(x, y, z)
phi = x^2 * y * z;
endfunction

// Partial derivatives (components of the gradient)


function dphi_dx=dphi_dx_func(x, y, z)
dphi_dx = 2 * x * y * z;
endfunction

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

// Evaluate at a point, for example (x=1, y=2, z=3)


x = 1;
y = 2;
z = 3;

grad_phi = [dphi_dx_func(x,y,z); dphi_dy_func(x,y,z); dphi_dz_func(x,y,z)];

disp("Gradient of phi at (1,2,3):");


disp(grad_phi);

"Gradient of phi at (1,2,3):"

12.

3.

2.

1.4 Green’s theorem


Statement of Green’s theorem in the plane:
If P(x, y) and Q(x, y) be two continuous functions having continuous partial derivatives in a region R
of the xy-plane, bounded by a simple closed curve C, then

∂ 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;

// Define the integrand: (∂Q/∂x - ∂P/∂y)


deff('z = integrand(x, y)', 'z = -1'); // Since it's constant

// Define integration resolution


N = 100; // number of steps
hx = (x1 - x0)/N;
hy = (y1 - y0)/N;

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

disp("The value of the integral is:");


disp(sum);
out put:

"The value of the integral is:"

-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

disp("The value of the integral is:");


disp(sum);

out put:

"The value of the integral is:"

-0.0496583

LAB 2: Computation of basis and dimension for a vector space and


graphical representation of linear transformation
2.1 Objectives:

1. to verify the Rank nullity theorem of given linear transformation.

2. to compute the dimension of vector space.

3. to represent linear transformations graphically.

2.2 Rank Nullity Theorem


1) Verify the rank-nullity theorem for the linear transformation T : R 3 → R 3 defined by
T(x, y, z) = (x + 4y + 7z, 2x + 5y + 8z, 3x + 6y + 9z).
// Define the matrix A representing the transformation T
A = [1 4 7;
2 5 8;
3 6 9];

// Get the size of the matrix


[nRows, nCols] = size(A);

// Step 1: Compute the rank of A


rankA = rank(A);

// Step 2: Compute the nullity (dimension of null space)


nullity = nCols - rankA;

// 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);

// Compute the rank of A (dimension of subspace)


dim_subspace = rank(A);

// Display the result


disp("Dimension of the subspace spanned by the vectors:");
disp(dim_subspace);

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];

// Transformation matrix for horizontal stretch by factor of 2


T = [2 0;
0 1];

// Apply transformation
v_transformed = T * v;

// Display the result


disp("Original vector:");
disp(v);
disp("Transformed vector (after horizontal stretch):");
disp(v_transformed);

// Plotting
clf();
xgrid();

plot([0, v(1)], [0, v(2)], 'r'); // Original vector in red


plot([0, v_transformed(1)], [0, v_transformed(2)], 'b'); // Transformed vector in blue

// Add legends and labels


legend("Original Vector", "Stretched Vector");
xtitle("Horizontal Stretch Transformation");
xlabel("x-axis");
ylabel("y-axis");

// Set equal scaling for axes


a = gca();
a.isoview = "on";

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;

// Original vector (10, 0)


v = [10; 0];
// Reflection matrix about y-axis
T = [-1 0;
0 1];

// Apply the transformation


v_reflected = T * v;

// Set up the figure


clf();
xgrid();
xtitle("Reflection about the y-axis");

// Plot original vector (red)


plot([0, v(1)], [0, v(2)], 'r-');
// Plot reflected vector (blue)
plot([0, v_reflected(1)], [0, v_reflected(2)], 'b-');

// Add axis labels


xlabel("x-axis");
ylabel("y-axis");

// Keep equal axis scaling


a = gca();
a.isoview = "on";

// 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;

// Original vector (10, 0)


v = [10; 0];
// Rotation matrix for π/2 radians (90 degrees CCW)
T = [0 -1;
1 0];

// Apply the transformation


v_rotated = T * v;

// Set up the figure


clf();
xgrid();
xtitle("Rotation by π/2 radians (90° CCW)");

// Plot original vector (red)


plot([0, v(1)], [0, v(2)], 'r-');
// Plot rotated vector (blue)
plot([0, v_rotated(1)], [0, v_rotated(2)], 'b-');

// Add axis labels


xlabel("x-axis");
ylabel("y-axis");

// Maintain equal scaling


a = gca();
a.isoview = "on";

// 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:

2.3.4 Shear Transformation


Represent the Shear transformation T : R2 → R2 geometrically.
Find the image of (2,3) under shear transformation.
clc;
clear;

// Original vector
v = [2; 3];

// Shear matrix (X-shear with k = 1)


k = 1;
T = [1 k;
0 1];

// Apply transformation
v_sheared = T * v;

// Plotting
clf();
xgrid();
xtitle("Shear Transformation (X-shear, k = 1)");

// Plot original vector in red


plot([0, v(1)], [0, v(2)], 'r-');
// Plot sheared vector in blue
plot([0, v_sheared(1)], [0, v_sheared(2)], 'b-');

// 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:

LAB 3: Visualization in time and frequency domain of standard


functions
3.1 Objectives:

1. to use the standard in-built function of Laplace transform.

2. to graphically plot time and frequency domain of standard functions.

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;

// --- TIME DOMAIN ---


t = 0:0.01:10;
f = sin(2*t);

// Plot Time Domain


scf(0);
plot(t, f, 'b'); // blue curve
xlabel("Time (t)");
ylabel("f(t) = sin(2t)");
title("Time Domain of sin(2t)");
xtitle("Time Domain", "Time (s)", "Amplitude");
legend("sin(2t)", "location", "upper_right");
xgrid();

// --- FREQUENCY DOMAIN (Laplace magnitude on jω axis) ---


omega = 0:0.01:10;
F_mag = 2 ./ (omega.^2 + 4); // corrected magnitude formula

// Plot Frequency Domain


scf(1);
plot(omega, F_mag, 'r'); // red curve
xlabel("Frequency (\omega)");
ylabel("|F(j\omega)|");
title("Frequency Domain (Laplace Magnitude)");
xtitle("Frequency Domain", "Frequency (rad/s)", "Magnitude");
legend("|F(jω)|", "location", "upper_right");
xgrid();

out put:
3.3 Represent the Laplace transform of f(t) = e 2t , both in time and frequency
domains
clc;
clear;

// --- Time Domain: f(t) = e^(2t) ---


t = 0:0.01:2; // Small t-range since e^2t grows fast
f = exp(2*t);

// Plot Time Domain


scf(0);
plot(t, f, 'b'); // Blue line
xlabel("Time (t)");
ylabel("f(t) = e^{2t}");
title("Time Domain of f(t) = e^{2t}");
legend("f(t)", "location", "upper_left");
xgrid();

// --- Frequency Domain: |F(jω)| = 1 / sqrt(4 + ω^2) ---


omega = -20:0.1:20;
F_mag = 1 ./ sqrt(4 + omega.^2); // Magnitude of Laplace Transform

// Plot Frequency Domain


scf(1);
plot(omega, F_mag, 'r'); // Red line
xlabel("Frequency (ω)");
ylabel("|F(jω)|");
title("Frequency Domain of f(t) = e^{2t} (Laplace Magnitude)");
legend("|F(jω)|", "location", "upper_right");
xgrid();

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;

// Standard Laplace Transform display using disp() only

// Example 1: f(t) = 1 → F(s) = 1/s


disp("Laplace Transform of f(t) = 1 is F(s) = 1/s");

// Example 2: f(t) = t → F(s) = 1/s^2


disp("Laplace Transform of f(t) = t is F(s) = 1/s^2");
// Example 3: f(t) = e^(a*t) → F(s) = 1 / (s - a)
a = 2;
disp("Laplace Transform of f(t) = e^(" + string(a) + "*t) is F(s) = 1 / (s - "
+ string(a) + ")");

// Example 4: f(t) = sin(w*t) → F(s) = w / (s^2 + w^2)


w = 3;
disp("Laplace Transform of f(t) = sin(" + string(w) + "*t) is F(s) = " +
string(w) + " / (s^2 + " + string(w) + "^2)");

// Example 5: f(t) = cos(w*t) → F(s) = s / (s^2 + w^2)


disp("Laplace Transform of f(t) = cos(" + string(w) + "*t) is F(s) = s / (s^2
+ " + string(w) + "^2)");

out put: "Laplace Transform of f(t) = 1 is F(s) = 1/s"


"Laplace Transform of f(t) = t is F(s) = 1/s^2"
"Laplace Transform of f(t) = e^(2*t) is F(s) = 1 / (s - 2)"
"Laplace Transform of f(t) = sin(3*t) is F(s) = 3 / (s^2 + 3^2)"
"Laplace Transform of f(t) = cos(3*t) is F(s) = s / (s^2 + 3^2)"

4.3 Inverse Laplace Transform


clc;

clear;

// Standard Inverse Laplace Transform display using disp() only

// Example 1: F(s) = 1/s → f(t) = 1


disp("Inverse Laplace of F(s) = 1/s is f(t) = 1");

// Example 2: F(s) = 1/s^2 → f(t) = t


disp("Inverse Laplace of F(s) = 1/s^2 is f(t) = t");

// Example 3: F(s) = 1 / (s - a) → f(t) = e^(a*t)


a = 2;
disp("Inverse Laplace of F(s) = 1 / (s - " + string(a) + ") is f(t) = e^(" + string(a) + "*t)");

// Example 4: F(s) = w / (s^2 + w^2) → f(t) = sin(w*t)


w = 3;
disp("Inverse Laplace of F(s) = " + string(w) + " / (s^2 + " + string(w) + "^2) is f(t) = sin(" +
string(w) + "*t)");

// Example 5: F(s) = s / (s^2 + w^2) → f(t) = cos(w*t)


disp("Inverse Laplace of F(s) = s / (s^2 + " + string(w) + "^2) is f(t) = cos(" + string(w) + "*t)");

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)"

4.4 Find the Laplace transform of t cos 4t


clc;
clear;

// Given function: f(t) = t * cos(a*t)


a = 4;

// Compute components of the Laplace Transform expression


a_squared = a^2;
numerator = "s^2 - " + string(a_squared);
denominator = "(s^2 + " + string(a_squared) + ")^2";

// Display the result


disp("Laplace Transform of f(t) = t * cos(" + string(a) + "*t) is:");
disp("F(s) = (" + numerator + ") / (" + denominator + ")");

out put:
"Laplace Transform of f(t) = t * cos(4*t) is:"
"F(s) = (s^2 - 16) / ((s^2 + 16)^2)"

4.5 Find the Laplace transform of sin 2t/t


clc;
clear;

// Given function: f(t) = sin(a*t)/t


a = 2;

// Display known Laplace Transform using identity


disp("Laplace Transform of f(t) = sin(" + string(a) + "*t)/t is:");
disp("F(s) = arctan(" + string(a) + " / s)");

out put:
"Laplace Transform of f(t) = sin(2*t)/t is:"
"F(s) = arctan(2 / s)"

LAB 5: Laplace transform of convolution of two functions


5.1 Objectives:
1. to calculate Laplace Transform for convolution of two functions.

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;

// Function to simulate Laplace transform of convolution


function laplace_convolution_transform()
// Define Laplace transforms manually
L_f = "1/(s^2)";
L_g = "1/(s - 1)";

// Multiply the transforms


L_conv = "("+L_f+")*("+L_g+")";

// Display result
disp("Laplace Transform of f*g is: ");
disp(L_conv);
endfunction

// Call the function


laplace_convolution_transform();

out put:

"Laplace Transform of f*g is: "

"(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;

// Define the convolution integral (numerical computation)


function val=convolution_fg(t)
deff('y = integrand(tau)', 'y = tau * exp(t - tau)');
val = intg(0, t, integrand); // Compute convolution f*g at t
endfunction

// Laplace transforms manually


// L{f} = 1/s^2, L{g} = 1/(s - 1), L{f*g} = 1/(s^2(s - 1))
function laplace_convolution_transform()
L_f = "1/(s^2)";
L_g = "1/(s - 1)";
L_conv = "("+L_f+")*("+L_g+")";

disp("Analytical Laplace Transform of f*g is: ");


disp(L_conv);
endfunction

// Compute convolution at a few sample time points


disp("Numerical values of convolution f*g(t) for different t:");
for t = 1:5
result = convolution_fg(t);
mprintf("t = %d, f*g(t) = %f\n", t, result);
end

// Show symbolic Laplace transform


laplace_convolution_transform();

out put:

"Numerical values of convolution f*g(t) for different t:"

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

"Analytical Laplace Transform of f*g is: "

"(1/(s^2))*(1/(s - 1))"

6.Solution of algebraic and transcendental equation by Regula-Falsi and


Newton-Raphson method
6.1 Objectives:
1. to solve algebraic and transcendental equation by Regula-Falsi method.
2. to solve algebraic and transcendental equation by Newton-Raphson method

Regula-Falsi method to solve a transcendental equation


1.1 Obtain a root of the equation x^3 − 2x − 5 = 0 between 2 and 3 by regula-falsi method. Perform
5 iterations.
clear;clc;
//Define the function f(x)
function y=f(x)
y = x^3 - 2*x - 5;
endfunction
// Regula-Falsi Method
function regula_falsi(x0, x1, tol, max_iter)
iter = 0;
while iter < max_iter
// Calculate function values at the initial guesses
f0 = f(x0);
f1 = f(x1);
// Calculate the new approximation using the Regula-Falsi formula
x2 = x1 - f1 * (x1 - x0) / (f1 - f0);
// Print the iteration and the current approximation
disp("Iteration: " + string(iter+1) + " x2 = " + string(x2));
// Check for convergence
if abs(f(x2)) < tol
break;
end
// Update the guesses for the next iteration
if f0 * f(x2) < 0
x1 = x2; // The root is in the interval [x0, x2]
else
x0 = x2; // The root is in the interval [x2, x1]
end
iter = iter + 1;
end
endfunction
// Initial guesses, tolerance, and maximum iterations
x0 = 2;
x1 = 3;
tolerance = 1e-6;
max_iterations = 5;
// Call the function to perform the Regula-Falsi method
regula_falsi(x0, x1, tolerance, max_iterations);

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"

Newton-Raphson method to solve a transcendental equation


1.2 Find a root of the equation 3x = cos x+ 1, near 1, by Newton Raphson method. Perform 5
iterations
clear;clc;
// Define the function f(x)
function y=f(x)
y = 3*x - cos(x) - 1;
endfunction
// Define the derivative f'(x)
function y=df(x)
y = 3 + sin(x);
endfunction
// Newton-Raphson Method
function newton_raphson(x0, tol, max_iter)
iter = 0;
while iter < max_iter
// Calculate function and its derivative at x0
fx = f(x0);
dfx = df(x0);
// Calculate the next approximation using Newton-Raphson formula
x1 = x0 - fx / dfx;
// Print the iteration and the current approximation
disp("Iteration: " + string(iter + 1) + " x1 = " + string(x1));
// Check for convergence (if the change is small enough)
if abs(x1 - x0) < tol
break;
end
// Update x0 for the next iteration
x0 = x1;
iter = iter + 1;
end
// Print the final root after convergence
disp("Root is approximately: " + string(x1));
endfunction
// Initial guess, tolerance, and maximum iterations
x0 = 1; // Initial guess
tolerance = 1e-6; // Tolerance for stopping
max_iterations = 5; // Perform 5 iterations
// Call the Newton-Raphson function
newton_raphson(x0, tolerance, max_iterations)

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"

1.3 Find a root of the equation xe^x = 2, between 0 and 1, by Regula-falsi


method. Correct to 3 decimal places.
// Define the function
function y=f(x)
y = x * exp(x) - 2;
endfunction

// 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);

// Check if the root is found or if the error is within tolerance


if abs(fc) < tol then
break;
end

// Update the interval based on the sign of f(c)


if fa * fc < 0 then
b = c;
fb = fc;
else
a = c;
fa = fc;
end
end

// Return the root


root = c;
endfunction

// 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);

// Display the root found


disp("The root is: ");
disp(root);

output: "The root is: "


0.8524516

1.4 Obtain a real positive root of x^4 − x = 0, near 1, by Newton-Raphson


method. Perform 4 iterations.
// Define the function f(x) = x^4 - x
function y=f(x)
y = x^4 - x;
endfunction

// Define the derivative f'(x) = 4x^3 - 1


function y_prime=df(x)
y_prime = 4 * x^3 - 1;
endfunction

// 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;

// Call the method to find the root


root = newton_raphson(x0, iterations);

// Display the final root found after 4 iterations


disp("The root after 4 iterations is: ");
disp(root);

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:

1. to interpolate using Newton’s Forward interpolation method.

2. to interpolate using Newton’s backward interpolation method.

3. to extrapolate using Newton’s backward interpolation method.

1.Use Newtons forward interpolation to obtain the interpolating polynomial


and hence calculate y(2) for the following: x: 1 3 5 7 9
y: 6 10 62 210 502
// Given data
x = [1, 3, 5, 7, 9];
y = [6, 10, 62, 210, 502];

// Number of data points


n = length(x);

// Step size
h = x(2) - x(1);

// Calculate forward differences


delta = zeros(n, n);
delta(:,1) = y; // First column is the original y values

// Calculate the forward differences


for j = 2:n
for i = 1:(n-j+1)
delta(i,j) = delta(i+1,j-1) - delta(i,j-1);
end
end

// Display the forward difference table


disp(delta);

// Now calculate P(2) using Newton's Forward Formula


x_value = 2;
r= (x_value - x(1)) / h; // Calculate u for P(2)

// Initialize the interpolation result


yo = y(1);

// Calculate the terms of the Newton's Forward Polynomial


term = r; // u term for the first difference

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

// Display the result


disp("Interpolated value at x = 2 is: ");
disp(yo);

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.

2.Use Newtons backward interpolation to obtain the interpolating


polynomial and hence calculate y(8) for the following data:
x: 1 3 5 7 9
y: 6 10 62 210 502
// Given data points
x = [1, 3, 5, 7, 9]; // x values
y = [6, 10, 62, 210, 502]; // y values

// Step 1: Calculate backward differences


n = length(x);
diff_table = zeros(n, n); // Initialize the backward difference table
diff_table(:, 1) = y'; // The last column is just the y values

// Compute the backward differences


for j = 2:n
for i = n:-1:j
diff_table(i,j) = diff_table(i,j-1) - diff_table(i-1,j-1);
end
end

// Display the backward difference table


disp("Backward Difference Table:");
disp(diff_table);

// Step 2: Construct Newton's Backward Interpolation Polynomial


// Initialize the polynomial result (P(x)) and the value to be computed (x =
8)
X = 8; // Value at which we need to calculate y
h = x(2) - x(1); // Common difference
r = (X - x(n)) / h; // Calculate u = (X - xn) / h
yn = y(n); // Start with the last y value (y_n)

// Compute the polynomial incrementally


r_term = r; // u term for the first increment
for k = 2:n
yn = yn + (r_term * diff_table(n,k) / factorial(k-1));
r_term = r_term * (r + (k-1)); // Update u for the next term
end
// Display the result of the interpolation
disp("Interpolated value at x = 8:");
disp(yn);

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.

8.2 Trapezoidal Rule


Evaluate R 5 0 1/1+x^2
// Define the function f(x) = 1 / (1 + x^2)
function y=f(x)
y = 1 / (1 + x^2);
endfunction

// Trapezoidal Rule Implementation


function integral=trapezoidal_rule(a, b, n)
h = (b - a) / n; // Step size
sum = f(a) + f(b); // Start with the first and last terms

// Sum the function values at the intermediate points


for i = 1:n-1
x_i = a + i * h;
sum = sum + 2 * f(x_i); // Add the intermediate terms (multiplied by
2)
end

// Multiply by h/2 to get the final result


integral = (h / 2) * sum;
endfunction

// Define the limits of integration


a = 0; // Lower limit
b = 5; // Upper limit
n = 10; // Number of subintervals (can adjust for more accuracy)

// Call the trapezoidal rule function


integral_value = trapezoidal_rule(a, b, n);

// Display the result


disp("The integral value is: ");
disp(integral_value);

out put:
"The integral value is: "
1.3731041

8.3 Simpson’s 1/3 rd Rule


Evaluate R 5 0 1/1+x^2
// Define the function f(x) = 1 / (1 + x^2)
function y=f(x)
y = 1 / (1 + x^2);
endfunction

// Simpson's 1/3 Rule Implementation


function integral=simpsons_rule(a, b, n)

h = (b - a) / n; // Step size
sum = f(a) + f(b); // Start with f(a) and f(b)

// Sum the function values at the odd indices (multiplied by 4)


for i = 1: 2:n-1
sum = sum + 4 * f(a + i * h);
end

// Sum the function values at the even indices (multiplied by 2)


for i = 2:2:n-2
sum = sum + 2 * f(a + i * h);
end

// Multiply by h/3 to get the final result


integral = (h / 3) * sum;
endfunction

// Define the limits of integration


a = 0; // Lower limit
b = 5; // Upper limit
n = 10; // Number of subintervals (must be even)

// Call the Simpson's Rule function


integral_value = simpsons_rule(a, b, n);
// Display the result
disp('The integral value is: ');
disp(integral_value);

out put:
"The integral value is: "
1.3714540

8.4 Simpson’s 3/8th rule


Evaluate R 6 0 1/1+x^2 dx using Simpson’s 3/8 th rule, taking 6 sub interval
// Define the function f(x) = 1 / (1 + x^2)
function y=f(x)
y = 1 / (1 + x^2);
endfunction

// Simpson's 3/8 Rule Implementation


function integral=simpsons_38_rule(a, b, n)
// Ensure n is divisible

h = (b - a) / n; // Step size
sum = f(a) + f(b); // Start with f(a) and f(b)

// Sum the function values at the odd indices (multiplied by 3)


for i = 1:3:n-1
sum = sum + 3 * f(a + i * h);
end

// Sum the function values at the even indices (multiplied by 3)


for i = 2: 3:n-2
sum = sum + 3 * f(a + i * h);
end

// Sum the function values at the indices divisible by 3 (multiplied by 2)


for i = 3:3:n-3
sum = sum + 2 * f(a + i * h);
end

// Multiply by 3h/8 to get the final result


integral = (3 * h / 8) * sum;
endfunction

// Define the limits of integration


a = 0; // Lower limit
b = 6; // Upper limit
n = 6; // Number of subintervals (must be divisible by 3)

// Call the Simpson's 3/8 Rule function


integral_value = simpsons_38_rule(a, b, n);

// Display the result


disp('The integral value is: ');
disp(integral_value);

out put:
"The integral value is: "
1.3138116

LAB 9: Solution of ODE of first order and first degree by Taylor’s


series and Modified Euler’s method
9.1 Objectives:
1. to solve ODE by Taylor series method.

2. to solve ODE by Modified Euler method.

3. to trace the solution curves.

9.2 Taylor series method to solve ODE


Solve: dy/dx =x^2+y^2 with y(0) = 1 using Taylor series method at x =
0.1,0.2,0.3. up to third degree.
// Taylor series method for dy/dx = x^2 + y^2 with y(0) = 1

// Function to calculate dy/dx = x^2 + y^2


function dydx=f(x, y)
dydx = x^2 + y^2;
endfunction

// Function to calculate the second derivative d^2y/dx^2


function d2ydx2=f2(x, y, dydx)
d2ydx2 = 2*x + 2*y*dydx;
endfunction

// Function to calculate the third derivative d^3y/dx^3


function d3ydx3=f3(x, y, dydx, d2ydx2)
d3ydx3 = 2 + 2*(dydx^2 + y*d2ydx2);
endfunction

// Initialize variables
x0 = 0; // Initial x
y0 = 1; // Initial condition y(0) = 1
h = 0.1; // Step size

// Calculate values at x = 0.1, 0.2, 0.3 using Taylor series


x_values = [0.1, 0.2, 0.3];
y = y0;
dydx = f(x0, y); // First derivative at x = 0
d2ydx2 = f2(x0, y, dydx); // Second derivative at x = 0
d3ydx3 = f3(x0, y, dydx, d2ydx2); // Third derivative at x = 0

// Loop to calculate the solution at each x_value using Taylor series


expansion
for i = 1:length(x_values)
x = x_values(i);

// Taylor series expansion up to the third degree


y_approx = y + dydx * x + (1/2) * d2ydx2 * x^2 + (1/6) * d3ydx3 *
x^3;

// 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"

given dy/dx=1+y/x,y=2 at x=1,find the approximate value y at x=1.4 by taking


step size h=0.2 applying modified euler’s method
// Define the differential equation: dy/dx = 1 + y/x
function dydx=f(x, y)
dydx = 1 + y / x;
endfunction

// 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;

// Apply Modified Euler's method


for i = 1:length(x_values)
// Step 1: Predictor step (Euler's method)
f1 = f(x, y); // Calculate f(x_n, y_n)
y_star = y + h * f1; // Predictor value

// Step 2: Corrector step (Modified Euler's method)


f2 = f(x + h, y_star); // Calculate f(x_n+1, y_star)
y_new = y + (h / 2) * (f1 + f2); // Corrected value of y

// Update values of x and y for the next iteration


x = x + h;
y = y_new;

// Display the results


disp("At x = " + string(x) + ", y = " + string(y_new));
end
"At x = 1.2, y = 2.6166667"

"At x = 1.4, y = 3.2670635"


LAB 10: Solution of ODE of first order and first degree by Runge-
Kutta 4th order method and Milne’s predictor and corrector
method
10.1 Objectives:

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.

10.2 Runge-Kutta 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

// Apply RK4 method


x = x0;
y = y0;

// Perform one step of RK4 to find y(0.2)


k1 = h * f(x, y); // Calculate k1
k2 = h * f(x + h / 2, y + k1 / 2); // Calculate k2
k3 = h * f(x + h / 2, y + k2 / 2); // Calculate k3
k4 = h * f(x + h, y + k3); // Calculate k4

// Update y using the RK4 formula


y_new = y + (1 / 6) * (k1 + 2 * k2 + 2 * k3 + k4);

// Display the result


disp("At x = " + string(x_target) + ", y = " + string(y_new));

"At x = 0.2, y = 1.1672208"


given that dy/dx=x-y^2 and the
daty(0)=0,y(0.2)=0.02'y(0.4)=0.0795,y(0.6)=0.1762.compute y(0.8) by applying milne's
method .
// Define the differential equation
function dydx=f(x, y)
dydx = x - y^2; // dy/dx = x - y^2
endfunction
// Initialize the known values
x0 = 0;
x1 = 0.2;
x2 = 0.4;
x3 = 0.6;

y0 = 0;
y1 = 0.02;
y2 = 0.0795;
y3 = 0.1762;

h = 0.2; // step size

// Step 1: Predict y(0.8) using Adams-Bashforth (4th-order predictor)


f0 = f(x0, y0);
f1 = f(x1, y1);
f2 = f(x2, y2);
f3 = f(x3, y3);

y4_pred = y3 + h/24 * (55*f3 - 59*f2 + 37*f1 - 9*f0);

// Step 2: Correct the prediction using Adams-Moulton (3rd-order corrector)


f4_pred = f(x3 + h, y4_pred); // f(0.8, y4_pred)

y4 = y3 + h/24 * (9*f4_pred + 19*f3 - 5*f2 + f1);

// Output the computed value of y(0.8)


disp("y(0.8) = " + string(y4));

Out put: "y(0.8) = 0.3045695"

You might also like