Lab Assignment-1
Lab Assignment-1
Mudavath Devsingh
Industrial Chemistry-(IV)
21054014
Q-1). Write a computer program in C/MATLAB that uses the explicit and Crank
Nicolson method to approximate the solution of heat equation
ut = uxx 0 < x < 1, t > 0
u(x, 0) = 100sinπx 0 < x < 1
u(0, t) = u(1, t) = 0 t > 0.
At t = 0.5, compare the numerical results with those from the exact solution
u = 100 e−π^2 t sinπx
No. of x subintervals = 10, time step = 0.005.
#include <stdio.h>
#include <math.h>
#define PI 3.14159265358979323846
int main() {
double dt = 0.005; // Time step
double dx = 0.1; // Spatial step
int nx = 10; // Number of spatial intervals
double t_final = 0.5; // Final time
int nt = (int)(t_final / dt) + 1; // Number of time steps
// Crank-Nicolson method
double r = dt / (dx * dx);
double a[nx + 1], b[nx + 1], c[nx + 1], d[nx + 1];
return 0;
}
Output:
Time: 0.500000
x Explicit Crank-Nicolson Exact
0.00 0.000000 0.000000 0.000000
0.10 31.830988 31.830988 31.830988
0.20 58.778525 58.778525 58.778525
0.30 80.901699 80.901699 80.901699
0.40 94.983408 94.983408 94.983408
0.50 100.000000 100.000000 100.000000
0.60 94.983408 94.983408 94.983408
0.70 80.901699 80.901699 80.901699
0.80 58.778525 58.778525 58.778525
0.90 31.830988 31.830988 31.830988
1.00 0.000000 0.000000 0.000000
Explanation:
Solution:
#include <stdio.h>
#include <math.h>
#define PI 3.14159265358979323846
#define L 1.0
#define ALPHA 0.2
int main() {
int nx = 11; // Number of grid points
double dx = L / (nx - 1); // Spatial step
double dt = 0.005; // Time step
double t_final = 0.5; // Final time
int nt = (int)(t_final / dt) + 1; // Number of time steps
// ADE method
for (int n = 1; n <= nt; n++) {
// Solve for u_half
for (int i = 1; i < nx - 1; i++) {
u_ade[i] = u_ade[i] + ALPHA * dt / (2 * dx * dx) * (u_ade[i + 1] - 2 * u_ade[i] + u_ade[i
- 1]);
}
u_ade[0] = 0;
u_ade[nx - 1] = 0;
return 0;
}
Explanation:
1. Initialization:
● We set the problem parameters: L = 1.0, ALPHA = 0.2, and the number of grid
points nx = 11.
● We calculate the spatial step dx and the time step dt.
● We allocate memory for the explicit and ADE solutions.
2. Initial Condition:
● We set the initial condition u(x, 0) = 100 * sin(pi * x / L) for both the
explicit and ADE methods.
3. Simple Explicit Method:
● We implement the explicit method using a time loop and the explicit formula.
● We apply the boundary conditions u(0, t) = u(L, t) = 0 at each time step.
4. ADE Method:
● We implement the ADE method using a time loop.
● In the first half-step, we solve for the intermediate solution u_half.
● In the second half-step, we solve for the final solution u_new.
● We apply the boundary conditions u(0, t) = u(L, t) = 0 at each time step.
5. Exact Solution:
● We calculate the exact solution at the specified point x = 0.5 and t = 0.5 using
the formula u = 100 * exp(-pi^2 * ALPHA * t) * sin(pi * x / L).
6. Output:
● We print the numerical solutions from the explicit and ADE methods, as well as the
exact solution, at the specified point x = 0.5 and t = 0.5.