Assume that we have a 2D unsteady heat conduction problem as follows:
main.m45 chars2 lines
with initial conditions:
main.m22 chars2 lines
and boundary conditions:
main.m58 chars2 lines
We consider the backward difference method for the spatial derivatives and implicit Euler for time integration with time step size Δt
.
Let, Δx
and Δy
represent the spatial step sizes along x
and y
directions, respectively. Then, the discretized solution at time nΔt
and grid points iΔx
and jΔy
can be defined as u(i,j,n)
.
The discrete form of the problem is as follows:
main.m161 chars2 lines
Rearranging the terms, we have:
main.m24 chars2 lines
where
main.m381 chars8 lines
The coefficient matrix A
and right-hand-side vector B
can be generated by performing the following steps:
main.m2415 chars84 lines
Finally, we can solve the system of equations for each time step using a linear solver, such as Matlab's backslash operator:
main.m923 chars22 lines
Note that the above code assumes homogeneous Dirichlet boundary conditions. If you have different boundary conditions, you need to modify the code accordingly. For example, for Neumann boundary conditions, you need to add extra terms to the coefficient matrix and the right-hand-side vector to represent the boundary flux.
gistlibby LogSnag