Practical 7-10 BSc VI Sem_240527_180417

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

Christ Church College Kanpur

B.Sc. Sem VI
Practical (Using MATLAB)

Practical 7. To write programs for implementation of the Lagrange


interpolation in MATLAB.
Program:
function lagrange_interpolation(x, y, xx)
n = length(x) - 1; % Degree of the polynomial
% Initialize the Lagrange polynomial
P = zeros(size(xx));
% Compute Lagrange basis polynomials and interpolate
for i = 1:n+1
% Compute the Lagrange basis polynomial
Li = ones(size(xx));
for j = 1:n+1
if j ~= i
Li = Li .* (xx - x(j)) / (x(i) - x(j));
end
end
% Add the contribution of the current data point
P = P + y(i) * Li;
end
% Plot the original data points and the interpolated polynomial
plot(x, y, 'o', xx, P, '-')
xlabel('x')
ylabel('y')
title('Lagrange Interpolation')
legend('Data Points', 'Interpolated Polynomial')
end
%We can use this function by passing your data points x and y, and the points xx at which
%we want to interpolate:

% Example usage
x = [0, 1, 2, 3, 4];
y = [1, 2, 1, 0, 1];
xx = linspace(0, 4, 100); % Interpolation points
lagrange_interpolation(x, y, xx);
Note: This will plot the original data points and the interpolated polynomial. We can adjust
the data points and interpolation points as needed for our specific problem.

Practical 8. To write programs for implementation of the Simpson's 1/3 rule for
numerical integration in MATLAB.
Program:
function integral_value = simpsons_rule(f, a, b, n)
% Check if n is even
if mod(n, 2) ~= 0

error('Number of subintervals (n) must be even for Simpson''s rule.');


end

% Step size
h = (b - a) / n;

% Initialize sum
sum_odd = 0;
sum_even = 0;

% Calculate sum of odd-indexed and even-indexed terms


for i = 1:n-1
x = a + i * h;
if mod(i, 2) == 0
sum_even = sum_even + f(x);
else
sum_odd = sum_odd + f(x);
end
end

% Calculate integral value using Simpson's rule formula


integral_value = (h / 3) * (f(a) + 4 * sum_odd + 2 * sum_even + f(b));
end

% We can use this function by passing the function f(x), the limits of integration a and b, and

%the number of subintervals n:

% Example usage
f = @(x) sin(x); % Define the function
a = 0; % Lower limit of integration
b = pi; % Upper limit of integration
n = 100; % Number of subintervals
integral_value = simpsons_rule(f, a, b, n);

fprintf('Approximated value of integral: %.6f\n', integral_value);


Note: We can Replace f = @(x) sin(x) with your own function, adjust the limits of
integration a and b. Also, we can choose an appropriate number of subintervals n based on
the required accuracy.

Practical 9. To find Eigenvalues of the following Matrix by Power Method in


MATLAB:

 4 1 0 1
 1 3  1 0 
 
 0  1 2  1
 
1 0 1 4 

Program:
% Define the matrix A
A = [4, -1, 0, 1;
-1, 3, -1, 0;
0, -1, 2, -1;
1, 0, -1, 4];
% Define the initial guess for the eigenvector
v = [1; 1; 1; 1];
% Set the number of iterations
num_iterations = 100;
% Power method iteration
for i = 1:num_iterations
% Multiply A by the current eigenvector
Av = A * v;
% Calculate the magnitude of Av
eigenvalue = norm(Av);
% Normalize Av to get the next eigenvector approximation
v = Av / eigenvalue;
end
disp('Dominant eigenvalue:');
disp(eigenvalue);
disp('Eigenvector:');
disp(v);

Output:
Dominant eigenvalue:
5.278413609496445

Eigenvector:
0.708049776672887
-0.258904520246109
-0.118158193818664
0.646275950645789
Note: In this example, we have a 4x4 matrix A. We initialize an arbitrary guess for the
eigenvector v. Then, in the iteration loop, we repeatedly multiply A by the current
eigenvector approximation, normalize the result to obtain the next approximation of the
eigenvector, and update the eigenvalue estimate. After a sufficient number of iterations,
eigenvalue will converge to the dominant eigenvalue of A, and v will converge to its
corresponding eigenvector.

Practical 10. To write a program to find solution and plot its graph for the
following second-order ordinary differential equation (ODE) using the fourth-
order Runge-Kutta method in MATLAB:

+2 + 2𝑦 = sin (𝑡)

with initial conditions: 𝑦(0) = 1, (0) = 0 .

Program:
% Define the ODE functions
f1 = @(t, y, v) v;
f2 = @(t, y, v) sin(t) - 2*v - 2*y;

% Define the initial conditions


y0 = 1;
v0 = 0;

% Define the time span


tspan = [0, 10];

% Define the step size


h = 0.1;

% Number of steps
N = (tspan(2) - tspan(1)) / h;

% Initialize arrays to store t, y, and v values


t = zeros(1, N+1);
y = zeros(1, N+1);
v = zeros(1, N+1);
% Set initial values
t(1) = tspan(1);
y(1) = y0;
v(1) = v0;

% Runge-Kutta 4th order method


for i = 1:N
k1y = h * f1(t(i), y(i), v(i));
k1v = h * f2(t(i), y(i), v(i));

k2y = h * f1(t(i) + h/2, y(i) + k1y/2, v(i) + k1v/2);


k2v = h * f2(t(i) + h/2, y(i) + k1y/2, v(i) + k1v/2);

k3y = h * f1(t(i) + h/2, y(i) + k2y/2, v(i) + k2v/2);


k3v = h * f2(t(i) + h/2, y(i) + k2y/2, v(i) + k2v/2);

k4y = h * f1(t(i) + h, y(i) + k3y, v(i) + k3v);


k4v = h * f2(t(i) + h, y(i) + k3y, v(i) + k3v);

y(i+1) = y(i) + (k1y + 2*k2y + 2*k3y + k4y) / 6;


v(i+1) = v(i) + (k1v + 2*k2v + 2*k3v + k4v) / 6;
t(i+1) = t(i) + h;
end

% Plot the solution


plot(t, y);
xlabel('t');
ylabel('y');
title('Solution of the second-order ODE using RK4');

Note: 1. We can convert this second-order ODE into a system of two first-order ODEs and
2
𝑑 𝑦
then solve it using the fourth-order Runge-Kutta method. Let 𝑣 = , so that = . Then
𝑑𝑡2
we have:

= 𝑣 and = sin(t)−2v−2y.

2. This code defines the system of first-order ODEs, then implements the fourth-order Runge-
Kutta method to compute the solution at each time step. Finally, it plots the solution y(t).

You might also like