A lazy numerical solution to the diffusion equation

Click For Summary

Discussion Overview

The discussion revolves around implementing a numerical solution to the diffusion equation using a gradient function for spatial differences. Participants explore issues related to stability, initial conditions, and boundary conditions in their numerical approach.

Discussion Character

  • Exploratory, Technical explanation, Homework-related

Main Points Raised

  • One participant describes their approach to solving the diffusion equation using the gradient function for second-order derivatives, expressing concerns about obtaining erroneous results, particularly negative values from a Gaussian initial condition.
  • Another participant suggests that more specificity about the problem and results, as well as sharing the code, would be beneficial for troubleshooting.
  • A participant mentions that they were not initially satisfying the CFL condition, which they found led to instability in their solution. After addressing this, they noted improvements but had yet to verify the effectiveness of their original formulation.
  • One participant shares a teaching anecdote about the importance of the CFL condition, illustrating how ignoring it can lead to unstable solutions, which they used as a cautionary example for students.
  • A participant humorously acknowledges their oversight in not having solved a PDE in a long time, referring to their earlier mistake as a "schoolboy error."

Areas of Agreement / Disagreement

Participants generally agree on the importance of the CFL condition for stability in numerical solutions, but there are varying levels of understanding and experience with the implementation of the diffusion equation, leading to unresolved issues regarding specific coding practices and results.

Contextual Notes

Participants have not fully resolved the implications of their numerical methods, particularly regarding the stability conditions and the specific formulation of the diffusion equation they are using.

hunt_mat
Homework Helper
Messages
1,816
Reaction score
33
So I want to write a short code to solve the diffusion equation and I want to be lazy and use the gradient function for the spatial differences, so for the second order derivative:
<br /> \frac{f(i+1)-2*f(i)+f(i-1)}{h^{2}}=\textrm{gradient}(\textrm{gradient}(f,h),h)<br />

So the code I wold use is:

<br /> T=T+dt*D*\textrm{gradient}(\textrm{gradient}(f,h),h)<br />

When I do this, I get erroneous results from my initial condition of a Gaussian for example. values start becoming negative which wrong. I don't know where I am going wrong.
 
Physics news on Phys.org
I think you will need to be more specific about what you are doing and what result you are trying to obtain relative to what you get. Also, posting your code would be helpful.
 
My goal is solving the diffusion equation with arbitrary initial conditions and arbitrary boundary conditions.

One problem I noted was I wasn't using the CFL condition on time. Once I did that it worked fine. I have yet to check it works with the formulation above yet.
 
Not satisfying the CFL condition of course would make your solution unstable. I once taught this to students and made a point out of not telling them about it as I was telling them how to discretise the heat equation. Then I purposely set ##\Delta t## just above the limit for stability to have the resulting animation start out as expected but break down after about ten seconds. That brought down some laughs as I said "... and we see that the solution behaves perfectly as expected ..." at the same time as it started to diverge and a cautionary tale to make sure students would take care to check their stability conditions and not blindly apply numerical recipes.
 
Schoolboy error. It's been a long long time since I had to numerically solve a PDE...
 

Similar threads

Replies
2
Views
2K
  • · Replies 41 ·
2
Replies
41
Views
10K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
6K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K