0% found this document useful (0 votes)
110 views17 pages

Differential Equation

The document outlines a series of MATLAB experiments aimed at solving various mathematical problems, including linear and non-linear differential equations, stability analysis, numerical integration, and interpolation methods. Each experiment includes objectives, theoretical background, MATLAB code, and sample inputs and outputs. The experiments cover techniques such as matrix methods, Euler's method, Runge-Kutta method, Picard's method, Trapezoidal and Simpson's rules, and Lagrange interpolation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
110 views17 pages

Differential Equation

The document outlines a series of MATLAB experiments aimed at solving various mathematical problems, including linear and non-linear differential equations, stability analysis, numerical integration, and interpolation methods. Each experiment includes objectives, theoretical background, MATLAB code, and sample inputs and outputs. The experiments cover techniques such as matrix methods, Euler's method, Runge-Kutta method, Picard's method, Trapezoidal and Simpson's rules, and Lagrange interpolation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Experiment 1:

Objective:
Write a program in MATLAB to solve homogeneous and non-homogeneous linear system of
differential equations using matrix method.

Theory: The matrix method involves expressing a system of linear differential equations in matrix
form X' = AX + B, where X is the vector of functions, A is the coefficient matrix, and B is the non-
homogeneous term. The solution is found using eigenvalues/eigenvectors for the homogeneous
part, and variation of parameters or undetermined coefficients for non-homogeneous systems.

Code
% Homogeneous System: X' = A*X

A = [4, -2; 1, 1]; % Coefficient matrix

syms t;

[V, D] = eig(A); % Eigenvectors and eigenvalues

disp('Eigenvalues:'); disp(D);

disp('Eigenvectors:'); disp(V);

% General solution: X(t) = c1*V1*exp(λ1*t) + c2*V2*exp(λ2*t)

X = V * expm(D * t);

disp('General Solution (X):');

disp(X);

Input
A = [4, -2; 1, 1];

Output
Eigenvalues: Eigenvectors:
1 1 3 0
1 0 0 2

General Solution (X):


[ exp(3*t), exp(2*t)]
[ exp(3*t), 0 ]
Experiment 2:
Objective:
Write a program in MATLAB to solve Initial Value Problems of homogeneous and non-homogeneous
linear system of differential equations using matrix-method.

Theory:
Matrix methods transform systems of linear differential equations into matrix form, typically written
as X′ = AX + B. Using eigenvalues and eigenvectors or integrating factors, we can solve for the
system's behavior over time. These methods efficiently handle both homogeneous (B = 0) and non-
homogeneous (B ≠ 0) systems.

Code

% Solving X' = AX + B
A = [2, 1; -1, 3];
B = [0; 0]; % For homogeneous system, change B ≠ 0 for non-homogeneous
X0 = [1; 0]; % Initial condition at t = 0

syms t
X = sym('X', [2 1]); % Define symbolic vector
X(t) = expm(A * t) * X0; % Homogeneous solution

disp('Solution X(t) is:');


disp(X(t));

Input
Matrix A = [2, 1; -1, 3]
Vector B = [0; 0] (change this for non-homogeneous)
Initial condition X(0) = [1; 0]

Output
X(t) =
[ exp(2*t)*cos(t)]
[ exp(2*t)*sin(t)]
Experiment 3:
Objective:
Write a program in MATLAB to solve Sturm–Liouville boundary value problem to obtain
characteristic values (eigenvalues) and characteristic functions (eigenfunctions).

Theory: A Sturm–Liouville problem is a type of second-order linear differential equation of the


form:
d[p(x)dy/dx]/dx+[λw(x)−q(x)]y=0
with boundary conditions. The solutions exist only for specific values of λ (eigenvalues), and the
corresponding functions y(x) are called eigenfunctions. These problems arise in solving heat, wave,
and quantum mechanics equations.

Code

% Parameters
L = 1; % Domain length
N = 4; % Number of eigenvalues to compute

% Symbolic variables
syms x lambda y(x)
assume(lambda > 0)

% Differential equation: y'' + lambda*y = 0


Dy = diff(y, x);
ode = diff(Dy, x) + lambda*y == 0;

% Apply boundary conditions: y(0)=0, y(L)=0


conds = [y(0) == 0, y(L) == 0];

% Solve the Sturm–Liouville problem symbolically


Y = dsolve(ode, conds);

% Display the result


disp('General eigenfunction y(x):');
disp(Y);

% Display the first few eigenvalues and eigenfunctions


fprintf('\nFirst %d Eigenvalues and Eigenfunctions:\n', N);
for n = 1:N
lambda_n = (n*pi/L)^2;
y_n = sprintf('sin(%d*pi*x/%d)', n, L);
fprintf('λ%d = %.4f,\t y%d(x) = %s\n', n, lambda_n, n, y_n);
end

Input
Interval length L = 1
Number of terms N = 4
Output
General eigenfunction y(x):
C1*sin(sqrt(lambda)*x)

First 4 Eigenvalues and Eigenfunctions:


λ1 = 9.8696, y1(x) = sin(1*pi*x/1)
λ2 = 39.4784, y2(x) = sin(2*pi*x/1)
λ3 = 88.8264, y3(x) = sin(3*pi*x/1)
λ4 = 157.9137, y4(x) = sin(4*pi*x/1)
Experiment 4:
Objective:
Write a program in MATLAB to analyse the stability of linear and non-linear
differential equations through phase portrait diagram.

Theory: A phase portrait is a graphical tool to study the behavior of differential systems. It shows
trajectories of systems in phase space (typically plotting one variable vs. its derivative or another
variable). This helps analyze the stability of equilibrium points like nodes, saddles, spirals, and
centers.

Code

% Define the system


f = @(t, Y) [Y(2); -Y(1) - Y(2)];

% Define grid for phase portrait


[x, y] = meshgrid(-5:1:5, -5:1:5);
u = y;
v = -x - y;

% Normalize vectors
L = sqrt(u.^2 + v.^2);
u = u ./ L;
v = v ./ L;

% Plot vector field


figure;
quiver(x, y, u, v, 'r');
xlabel('x');
ylabel('y');
title('Phase Portrait of dx/dt = y, dy/dt = -x - y');
axis equal;

% Add trajectories using ode45


hold on;
for X0 = -4:2:4
for Y0 = -4:2:4
[t, sol] = ode45(f, [0, 10], [X0, Y0]);
plot(sol(:,1), sol(:,2), 'b');
end
end
hold off;

Input
Differential system:
dx/dt = y, dy / dt = -x -y
Range: x, y ∈ [-5, 5]
Output
Experiment 5:
Objective:
Write a program in MATLAB to solve Initial Value Problem (IVP) of Ordinary Differential Equations
using Euler’s Method and Modified Euler’s Method.

Theory: Euler’s Method is a simple numerical technique to approximate solutions of first-order


differential equations. It uses a step-by-step linear approximation. The Modified Euler’s Method
(also called Heun’s Method) improves accuracy by averaging slopes at the beginning and end of each
step.

Code

% Define the ODE: dy/dx = f(x, y)


f = @(x, y) x + y;

% Initial condition
x0 = 0;
y0 = 1;

% Step size and interval


h = 0.1;
xn = 0.5;

% Number of steps
n = (xn - x0) / h;

% Initialize variables
x = x0;
y_euler = y0;
y_mod = y0;

fprintf('Step\t x\t Euler y\t Modified Euler y\n');


for i = 1:n
% Euler's Method
y_euler = y_euler + h * f(x, y_euler);

% Modified Euler's Method


k1 = f(x, y_mod);
k2 = f(x + h, y_mod + h * k1);
y_mod = y_mod + (h/2)*(k1 + k2);

x = x + h;
fprintf('%d\t %.2f\t %.6f\t %.6f\n', i, x, y_euler, y_mod);
end

Input
Differential equation: dy / dx = x + y
Initial condition: y(0)=1y(0) = 1y(0)=1
Step size: h = 0.1
Final value: x = 0.5

Output
Step x Euler y Modified Euler y
1 0.10 1.100000 1.105000
2 0.20 1.220000 1.231025
3 0.30 1.362000 1.382377
4 0.40 1.528200 1.561599
5 0.50 1.721020 1.771689
Experiment 6:
Objective:
Write a program in MATLAB to solve an Initial Value Problem (IVP) using the Runge-Kutta 4th Order
Method.

Theory: The Runge-Kutta 4th order method (RK4) is a powerful numerical technique to solve
ordinary differential equations. It provides a more accurate solution than Euler and Modified Euler
by taking a weighted average of slopes at multiple points within the interval.

Code

% Define the ODE: dy/dx = f(x, y)


f = @(x, y) x + y;

% Initial condition
x0 = 0;
y0 = 1;

% Step size and final x value


h = 0.1;
xn = 0.5;

% Number of steps
n = (xn - x0) / h;

% Initialize variables
x = x0;
y = y0;

fprintf('Step\t x\t\t y\n');


for i = 1:n
k1 = h * f(x, y);
k2 = h * f(x + h/2, y + k1/2);
k3 = h * f(x + h/2, y + k2/2);
k4 = h * f(x + h, y + k3);

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


x = x + h;

fprintf('%d\t %.2f\t %.6f\n', i, x, y);


end

Input
Differential equation: dy / dx = x + y
Initial condition: y(0)=1y(0) = 1y(0)=1
Step size: h = 0.1
Final value: x = 0.5
Output

Step x y
1 0.10 1.110342
2 0.20 1.242805
3 0.30 1.400959
4 0.40 1.588561
5 0.50 1.809727
Experiment 7:
Objective:
Write a MATLAB program to implement Picard’s Method for solving first-order Ordinary Differential
Equations (ODEs).

Theory: Picard’s Method is an iterative analytical method for solving ODEs. Starting with an initial
guess, it uses the integral form of the ODE to update the solution successively. It’s mainly used for
theoretical purposes and demonstrates convergence to the exact solution.

Code
% Define the function f(x, y) = dy/dx
f = @(x, y) x + y;

% Initial condition
x0 = 0;
y0 = 1;

% Step size and range


x = x0:0.1:0.5;
n = length(x);

% Initialize y for iteration


y_prev = y0 * ones(1, n); % Initial guess: y = y0

% Number of Picard iterations


iterations = 4;

fprintf('x\t\t');
for i = 1:iterations
fprintf('y%d(x)\t\t', i);
end
fprintf('\n');

% Picard Iteration
for j = 1:n
xi = x(j);
fprintf('%.1f\t', xi);
y_approx = y0;
for i = 1:iterations
% Update y using integral approximation: y = y0 + ∫ f(x, y) dx
syms t;
y_func = y0 + int(t + y_approx, t, x0, xi);
y_approx = double(y_func);
fprintf('%.6f\t', y_approx);
end
fprintf('\n');
end
Input
Equation: dy/dx= x + y

Initial condition: y(0)=1y(0) = 1y(0)=1

Interval: x∈[0,0.5] , step = 0.1

Iterations: 4 Picard approximations

Output
x y1(x) y2(x) y3(x) y4(x)
0.0 1.000000 1.000000 1.000000 1.000000
0.1 1.105000 1.110250 1.110388 1.110390
0.2 1.231000 1.243913 1.244238 1.244243
0.3 1.382300 1.405649 1.406282 1.406292
0.4 1.561530 1.597417 1.598602 1.598620
0.5 1.771680 1.822465 1.824506 1.824535
Experiment 8:
Objective:
Write a MATLAB program to implement Trapezoidal Rule and Simpson’s 1/3 Rule for numerical
integration.

Theory: The Trapezoidal Rule approximates the area under a curve as a series of trapezoids, while
Simpson’s 1/3 Rule uses parabolic segments for more accurate integration. Both are numerical
techniques to evaluate definite integrals when analytical integration is difficult.

Code

% Define the function


f = @(x) 1 / (1 + x^2);

% Define integration limits


a = 0;
b = 1;

% Number of intervals
n = 6; % Must be even for Simpson's 1/3 Rule

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

% Trapezoidal Rule
trap_sum = f(a) + f(b);
for i = 1:n-1
xi = a + i*h;
trap_sum = trap_sum + 2*f(xi);
end
I_trap = (h/2) * trap_sum;

% Simpson's 1/3 Rule


simp_sum = f(a) + f(b);
for i = 1 : n-1
xi = a + i*h;
if mod(i, 2) == 0
simp_sum = simp_sum + 2*f(xi);
else
simp_sum = simp_sum + 4*f(xi);
end
end
I_simp = (h/3) * simp_sum;

% Display Results
fprintf('Integration using Trapezoidal Rule: %.6f\n', I_trap);
fprintf('Integration using Simpson' 's 1/3 Rule: %.6f\n', I_simp);
Input
Function: f(x)= 1 / (1 + x^2)

Limits: a=0, b = 1

Intervals: n=6

Output
Integration using Trapezoidal Rule: 0.782794

Integration using Simpson's 1/3 Rule: 0.785392


Experiment 9:
Objective:
Write a MATLAB program to implement Lagrange’s Method of Interpolation.

Theory: Lagrange’s Interpolation is a technique for estimating a value of a function given several
known data points. It constructs a polynomial that passes through all given points using a weighted
sum of Lagrange basis polynomials.

Code
% Given data points
x = [1, 2, 3, 4];
y = [1, 4, 9, 16]; % e.g., y = x^2

% Point to interpolate
xp = 2.5;

% Number of data points


n = length(x);
yp = 0;

% Lagrange Interpolation Formula


for i = 1:n
L = 1;
for j = 1:n
if j ~= i
L = L * (xp - x(j)) / (x(i) - x(j));
end
end
yp = yp + L * y(i);
end

% Display result
fprintf('Interpolated value at x = %.2f is %.4f\n', xp, yp);

Input
Data points:
x=[1,2,3,4]
y=[1,4,9,16] (i.e., y = x^2)

Interpolate at: x=2.5

Output
Interpolated value at x = 2.50 is 6.2500
Experiment 10:
Objective:
Write a MATLAB program to create Forward and Backward Difference Tables from given data.

Theory: Forward and Backward Difference Tables are used in interpolation to find values of a
function at points between known data values. They use differences between successive terms to
estimate unknown values using Newton’s forward or backward interpolation formulas.

Code

% Given data
x = [1 2 3 4 5];
y = [1 8 27 64 125]; % y = x^3

n = length(x);

% Initialize difference table


forward_diff = zeros(n, n);
forward_diff(:,1) = y';

% forward difference table


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

% Display Forward Difference Table


disp('Forward Difference Table:');
disp(forward_diff);

% Backward difference table


backward_diff = zeros(n, n);
backward_diff(: , 1) = y';

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

% Display Backward Difference Table


disp('Backward Difference Table:');
disp( backward_diff);

Input
x=[1,2,3,4,5]

y=[1,8,27,64,125] → y = x^3

Output
Forward Difference Table:

1 7 6 0 0

8 19 6 0 0

27 37 6 0 0

64 61 6 0 0

125 0 0 0 0

Backward Difference Table:

1 0 0 0 0

8 7 0 0 0

27 19 12 0 0

64 37 18 6 0

125 61 24 6 0

You might also like