Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

MATLAB A lazy numerical solution to the diffusion equation

  1. May 18, 2018 at 8:57 AM #1

    hunt_mat

    User Avatar
    Homework Helper

    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 spacial differences, so for the second order derivative:
    [tex]
    \frac{f(i+1)-2*f(i)+f(i-1)}{h^{2}}=\textrm{gradient}(\textrm{gradient}(f,h),h)
    [/tex]

    So the code I wold use is:

    [tex]
    T=T+dt*D*\textrm{gradient}(\textrm{gradient}(f,h),h)
    [/tex]

    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.
     
  2. jcsd
  3. May 18, 2018 at 11:24 AM #2

    Orodruin

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper
    Gold Member
    2017 Award

    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.
     
  4. May 18, 2018 at 1:06 PM #3

    hunt_mat

    User Avatar
    Homework Helper

    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.
     
  5. May 18, 2018 at 3:22 PM #4

    Orodruin

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper
    Gold Member
    2017 Award

    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.
     
  6. May 18, 2018 at 3:38 PM #5

    hunt_mat

    User Avatar
    Homework Helper

    Schoolboy error. It's been a long long time since I had to numerically solve a PDE...
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Loading...