Partial Differential Equations (PDEs) in MATLAB Get Certified
Partial Differential Equations (PDEs) involve equations with partial derivatives and are widely used to describe physical phenomena such as heat transfer, fluid dynamics, and wave propagation. MATLAB offers several tools for solving PDEs, including the pdepe solver for one-dimensional problems and the PDE Toolbox for more advanced applications.
Types of PDEs
Here are some common types of PDEs:
- Parabolic PDEs: Represent diffusion processes, such as the heat equation.
- Hyperbolic PDEs: Represent wave propagation, such as the wave equation.
- Elliptic PDEs: Represent steady-state phenomena, such as Laplace’s equation.
Solving PDEs in MATLAB
Let’s explore how to solve different PDEs using MATLAB with examples.
1. Heat Equation
The heat equation is a parabolic PDE given by:
\[ \frac{\partial u}{\partial t} = \alpha \frac{\partial^2 u}{\partial x^2} \]
% Solving the heat equation
m = 0; % Symmetry parameter
alpha = 1; % Diffusion coefficient
% Define PDE, initial conditions, and boundary conditions
pdefun = @(x,t,u,DuDx) alpha*DuDx;
icfun = @(x) sin(pi*x); % Initial condition
bcfun = @(xl,ul,xr,ur,t) [ul; ur]; % Boundary condition
% Solve the PDE
x = linspace(0, 1, 20);
t = linspace(0, 2, 50);
sol = pdepe(m, pdefun, icfun, bcfun, x, t);
% Plot the solution
surf(x, t, sol);
title('Heat Equation Solution');
xlabel('Space x');
ylabel('Time t');
2. Wave Equation
The wave equation is a hyperbolic PDE given by:
\[ \frac{\partial^2 u}{\partial t^2} = c^2 \frac{\partial^2 u}{\partial x^2} \]
% Solving the wave equation
model = createpde();
% Define geometry
geometryFromEdges(model,@circleg);
% Define PDE coefficients
specifyCoefficients(model,'m',0,'d',1,'c',1,'a',0,'f',0);
% Apply boundary and initial conditions
applyBoundaryCondition(model,'dirichlet','Edge',1:model.Geometry.NumEdges,'u',0);
setInitialConditions(model,0,0);
% Generate the mesh
generateMesh(model);
% Solve the PDE
tlist = 0:0.1:2;
results = solvepde(model,tlist);
% Visualize the solution
pdeplot(model,'XYData',results.NodalSolution(:,end));
title('Wave Equation Solution');
3. Laplace’s Equation
Laplace’s equation is an elliptic PDE given by:
\[ \nabla^2 u = 0 \]
% Solving Laplace’s equation
model = createpde();
geometryFromEdges(model,@circleg); % Define geometry
specifyCoefficients(model,'m',0,'d',0,'c',1,'a',0,'f',0); % Define coefficients
% Apply boundary conditions
applyBoundaryCondition(model,'dirichlet','Edge',1:model.Geometry.NumEdges,'u',sin(theta));
% Solve the PDE
results = solvepde(model);
% Visualize the solution
pdeplot(model,'XYData',results.NodalSolution);
title('Laplace Equation Solution');
4. Initial Value Problem (IVP): Wave Equation
Consider the wave equation with initial conditions:
\[ \frac{\partial^2 u}{\partial t^2} = c^2 \frac{\partial^2 u}{\partial x^2} \]
Initial conditions:
- \( u(x,0) = \sin(\pi x) \) (initial displacement)
- \( \partial u/\partial t(x,0) = 0 \) (initial velocity)
% Parameters
c = 1; % Wave speed
L = 1; % Length of the domain
x = linspace(0, L, 100); % Space grid
t = linspace(0, 2, 200); % Time grid
% Initial conditions
u0 = sin(pi * x); % Initial displacement
ut0 = zeros(size(x)); % Initial velocity
% Solve the PDE using the method of lines (finite differences)
dx = x(2) - x(1);
dt = t(2) - t(1);
u = zeros(length(x), length(t)); % Initialize solution matrix
u(:,1) = u0; % Apply initial displacement
u(:,2) = u(:,1) + dt * ut0 + 0.5 * (c * dt / dx)^2 * diff([0; u(:,1); 0], 2);
% Time-stepping loop
for n = 2:length(t)-1
u(:,n+1) = 2 * u(:,n) - u(:,n-1) + (c * dt / dx)^2 * diff([0; u(:,n); 0], 2);
end
% Visualize the solution
[X,T] = meshgrid(x, t);
surf(X, T, u');
title('Wave Equation Solution for IVP');
xlabel('Space x');
ylabel('Time t');
zlabel('u(x,t)');
The output is a 3D plot showing how the wave evolves over space and time.
5. Solving a PDE Using Green’s Functions: Poisson’s Equation
Consider the one-dimensional Poisson equation:
\[ -\frac{d^2 u}{dx^2} = f(x) \]
with homogeneous Dirichlet boundary conditions. The solution is expressed by the Green’s function integral
\[ u(x) = \int_0^1 G(x,\xi) f(\xi)\,d\xi. \]
% Define the problem
L = 1; % Length of the domain
f = @(x) x .* (1 - x); % Source function
N = 100; % Number of points for discretization
% Discretize the domain
x = linspace(0, L, N);
dx = x(2) - x(1); % Grid spacing
% Compute the Green’s function
G = zeros(N, N);
for i = 1:N
for j = 1:N
if x(i) <= x(j)
G(i, j) = x(i) * (1 - x(j));
else
G(i, j) = x(j) * (1 - x(i));
end
end
end
% Solve using Green's function
u = G * f(x)' * dx; % Integral as a matrix-vector multiplication
% Plot the results
plot(x, u, 'b-', 'LineWidth', 2);
xlabel('x');
ylabel('u(x)');
title('Solution to Poisson Equation Using Green''s Function');
grid on;
6. Solving an Eigenvalue Problem for PDEs: Vibrating String
Consider the eigenvalue problem for the vibrating string:
\[ \frac{d^2 u}{dx^2} + \lambda u = 0, \qquad u(0)=u(L)=0. \]
% Define the problem parameters
L = 1; % Length of the string
N = 100; % Number of grid points
% Discretize the domain
x = linspace(0, L, N);
dx = x(2) - x(1); % Grid spacing
% Construct the finite difference matrix (Laplacian operator)
D = diag(-2 * ones(N, 1)) + diag(ones(N - 1, 1), 1) + diag(ones(N - 1, 1), -1);
D = D / dx^2;
% Apply boundary conditions (Dirichlet: u(0) = u(L) = 0)
D(1, :) = 0;
D(N, :) = 0;
D(1, 1) = 1;
D(N, N) = 1;
% Compute eigenvalues and eigenvectors
[Eigenvectors, Eigenvalues] = eig(D);
eigenvalues = diag(Eigenvalues); % Extract eigenvalues
% Select the first few eigenfunctions and eigenvalues
num_modes = 3;
for k = 1:num_modes
figure;
plot(x, Eigenvectors(:, k), 'LineWidth', 2);
title(['Eigenfunction for λ = ', num2str(eigenvalues(k))]);
xlabel('x');
ylabel('u(x)');
grid on;
end
7. Solving a Nonlinear PDE: Burger’s Equation
Burger’s equation is a fundamental nonlinear PDE:
\[ \frac{\partial u}{\partial t} + u\frac{\partial u}{\partial x} = \nu\frac{\partial^2 u}{\partial x^2} \]
with initial condition \( u(x,0)=\sin(\pi x) \) and homogeneous Dirichlet boundaries.
% Define the problem parameters
L = 1; % Length of the domain
N = 100; % Number of spatial grid points
T = 0.1; % Simulation time
dt = 0.001; % Time step
nu = 0.01; % Viscosity parameter
% Discretize the spatial domain
x = linspace(0, L, N);
dx = x(2) - x(1);
% Initialize the solution
u = sin(pi * x)'; % Initial condition
% Time-stepping loop
for n = 1:round(T/dt)
u_prev = u; % Save the previous time step
% Update the solution using an explicit scheme
for i = 2:N-1
u(i) = u_prev(i) - dt * u_prev(i) * (u_prev(i+1) - u_prev(i-1)) / (2*dx) ...
+ nu * dt * (u_prev(i+1) - 2*u_prev(i) + u_prev(i-1)) / dx^2;
end
% Apply boundary conditions
u(1) = 0;
u(N) = 0;
end
% Plot the solution
figure;
plot(x, u, 'LineWidth', 2);
title('Solution to Burger’s Equation');
xlabel('x');
ylabel('u(x, t)');
grid on;
8. Weak Solutions: Heat Equation with Discontinuous Initial Data
Consider the heat equation with a discontinuous initial condition:
\[ \frac{\partial u}{\partial t} = \frac{\partial^2 u}{\partial x^2} \]
where \( u(x,0)=1 \) for \( 0<x\le 0.5 \) and \( u(x,0)=0 \) otherwise, together with homogeneous Dirichlet boundaries.
% Define problem parameters
L = 1; % Length of the domain
N = 100; % Number of spatial points
T = 0.1; % Total simulation time
dt = 0.0005; % Time step
% Discretize the spatial domain
x = linspace(0, L, N);
dx = x(2) - x(1);
% Initialize the solution with discontinuous initial condition
u = zeros(N, 1);
u(x > 0 & x <= 0.5) = 1;
% Time-stepping loop (explicit finite difference scheme)
alpha = dt / dx^2; % Diffusion coefficient
for n = 1:round(T / dt)
u_prev = u; % Save the previous time step
% Update solution at interior points
for i = 2:N-1
u(i) = u_prev(i) + alpha * (u_prev(i+1) - 2*u_prev(i) + u_prev(i-1));
end
% Apply boundary conditions
u(1) = 0;
u(N) = 0;
end
% Plot the solution
figure;
plot(x, u, 'LineWidth', 2);
title('Weak Solution of the Heat Equation');
xlabel('x');
ylabel('u(x, t)');
grid on;
9. Quantum Mechanics: Solving the Time-Dependent Schrödinger Equation
We solve the one-dimensional time-dependent Schrödinger equation
\[ i\hbar\frac{\partial\psi}{\partial t} = -\frac{\hbar^2}{2m}\frac{\partial^2\psi}{\partial x^2} + V(x)\psi \]
inside an infinite well with a Gaussian wave-packet initial condition, using the Crank–Nicolson method.
L = 1; % Length of the domain
N = 500; % Number of spatial points
T = 0.01; % Total simulation time
dt = 1e-5; % Time step
h = 1; % Reduced Planck's constant
m = 1; % Particle mass
a = 50; % Width of the Gaussian
xo = 0.5; % Initial position of the wave packet
ko = 50; % Wave number of the packet
% Discretize the spatial domain
x = linspace(0, L, N);
dx = x(2) - x(1);
% Initialize the wavefunction
psi = exp(-a * (x - xo).^2) .* exp(1i * ko * x);
% Potential (infinite well)
V = zeros(size(x));
V(1) = 1e10; % Large potential at boundaries
V(end) = 1e10;
% Construct the Hamiltonian matrix (finite difference method)
H = -h^2 / (2 * m * dx^2) * (diag(ones(N-1,1),1) + diag(ones(N-1,1),-1) - 2*diag(ones(N,1))) + diag(V);
% Apply Dirichlet boundary conditions
H = H(2:end-1, 2:end-1);
psi = psi(2:end-1);
I = eye(N-2);
% Time evolution using Crank-Nicolson
A = I + 1i * dt / (2 * h) * H;
B = I - 1i * dt / (2 * h) * H;
for t = 0:dt:T
psi = A \ (B * psi(:));
end
% Normalize the wavefunction
psi = psi / sqrt(sum(abs(psi).^2) * dx);
% Plot the real part of the wavefunction
figure;
plot(x(2:end-1), real(psi), 'LineWidth', 2);
title('Real Part of the Wavefunction');
xlabel('x');
ylabel('Re(\psi)');
grid on;
Useful MATLAB Functions
Practice Questions
Test Yourself
1. Solve the heat equation for different initial conditions and plot the results.
2. Solve the wave equation on a rectangular domain using the PDE Toolbox.
3. Modify Laplace's equation to include a source term and solve it.
4. Modify the source function \(f(x)\) to \(\sin(\pi x)\) and recompute the solution using Green’s functions.
5. Modify the boundary conditions to \(u'(0)=0\) (Neumann) and \(u(L)=0\), and compute the eigenvalues and eigenfunctions for the updated PDE.
6. For the Burger’s equation example, change the initial condition to \(u(x,0)=\exp(-50(x-0.5)^2)\) and analyze the evolution.