Differential Equation
Differential Equation
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
syms t;
disp('Eigenvalues:'); disp(D);
disp('Eigenvectors:'); disp(V);
X = V * expm(D * t);
disp(X);
Input
A = [4, -2; 1, 1];
Output
Eigenvalues: Eigenvectors:
1 1 3 0
1 0 0 2
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
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).
Code
% Parameters
L = 1; % Domain length
N = 4; % Number of eigenvalues to compute
% Symbolic variables
syms x lambda y(x)
assume(lambda > 0)
Input
Interval length L = 1
Number of terms N = 4
Output
General eigenfunction y(x):
C1*sin(sqrt(lambda)*x)
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
% Normalize vectors
L = sqrt(u.^2 + v.^2);
u = u ./ L;
v = v ./ L;
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.
Code
% Initial condition
x0 = 0;
y0 = 1;
% Number of steps
n = (xn - x0) / h;
% Initialize variables
x = x0;
y_euler = y0;
y_mod = y0;
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
% Initial condition
x0 = 0;
y0 = 1;
% Number of steps
n = (xn - x0) / h;
% Initialize variables
x = x0;
y = y0;
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;
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
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
% 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;
% 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
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;
% 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)
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);
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
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
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