3D Diffusion Equation in MATLAB

Click For Summary
SUMMARY

The discussion centers on solving the 3D Diffusion equation using MATLAB, specifically employing a 3D Fourier transform and the Crank-Nicolsen method. The original code exhibited a decay to zero regardless of initial and boundary conditions, which was later resolved by correctly restricting the loops for Fourier coefficients to positive values. The solution now successfully displays heat flux instead of static decay. Key insights include the importance of defining initial conditions on a grid and using the fftn() and ifftn() functions for transitioning between Fourier and physical space.

PREREQUISITES
  • Proficiency in MATLAB programming
  • Understanding of the 3D Diffusion equation
  • Familiarity with Fourier transforms and their applications
  • Knowledge of numerical methods, specifically the Crank-Nicolsen scheme
NEXT STEPS
  • Explore MATLAB's fftn() and ifftn() functions for Fourier transforms
  • Study the implementation of the Crank-Nicolsen method in numerical solutions
  • Learn about the implications of boundary conditions in partial differential equations
  • Investigate common pitfalls in numerical simulations of diffusion processes
USEFUL FOR

Researchers, engineers, and students working on numerical simulations of partial differential equations, particularly those focusing on diffusion processes in MATLAB.

johnnyTransform
Messages
16
Reaction score
2
Hi guys,

I have functioning MATLAB code for my solution of the 3D Diffusion equation (using a 3D Fourier transform and Crank-Nicolsen) that runs just from the command window and automatically plots the results. However, it seems like my solution just decays to zero regardless of what initial condition I use, or boundary conditions I implement. Could I toss this code to somebody so they can run it on their rig and let me know if they can diagnose it? I've been stuck on it for days and I'd much appreciate it.

If you'd like me to post it all in the thread using the code formatting tool, I can do that, but I'll take the suggestion first before completely diluting the post. Thanks guys
 
Physics news on Phys.org
Just as a conjecture for those who are curious, I think it may have to do with the fact that I'm forwarding my Fourier coefficients in time using an explicit scheme. However, it's still an open question

EDIT: Sorry, I couldn't find the option to edit my original post. Not trying to bump this up
EDIT 2: Thank you mods, this subforum seems more appropriate for the question. Much appreciated!
 
Update: I think I may have figured out my universal decay problem. My solutions now display heat flux, and not just a static decay to zero. I'll explain what I'm thinking might be the trick for people who have similar problems in the future:

Starting with the 3D Diffsuion Eq.: ##u_t=k(u_{xx}+u_{yy}+u_{zz})##, assume the solution has a Fourier expansion (##e^{-ipz},e^{-imy},e^{-inx}##).

Physical space: ##u(x,y,z,t)##. Fourier space: ##c(m,n,p,t)##.

So we have a continuous Fourier transform pair. Once you define your initial condition on a grid, that continuous Fourier transform pair becomes a discrete Fourier transform pair (since your solution becomes discrete once you define it on a grid). In the DFT pair, {n,m,p} is restricted from ##(0,N-1)##, where N is the number of spatial points in your grid.

So, when you're forwarding your Fourier coefficients in time, and you need to loop through your spatial frequencies {n,m,p}, make sure to restrict your loops to positive values, so for example, in MATLAB:
Code:
for n=1:1:N
    for m=1:1:N
        for p=1:1:N

Not:
Code:
for n=-N/2:1:N/2
    for m=-N/2:1:N/2
        for p=-N/2:1:N/2

which would be appropriate if we were still dealing with our continuous Fourier transform pair, which allows for spatial frequencies {n,m,p} over ##(-\infty,\infty)##. The continuous Fourier transform pair is what most people are familiar with, which explains why this would be a natural mistake to make. With this in mind, you can use fftn() and ifftn() to loop back and forth between Fourier space and physical space, while forwarding your Fourier coefficients in time using some type of numerical scheme (for example, Crank-Nicolsen). Hope this is helpful (and correct)!
 

Similar threads

  • · Replies 41 ·
2
Replies
41
Views
10K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
0
Views
1K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K