# Solving 1D heat equation using FFT

**URL:** https://discourse.julialang.org/t/solving-1d-heat-equation-using-fft/111457
**Category:** Specific Domains
**Tags:** differentialequation
**Created:** [March 11, 2024, 12:15pm UTC](https://discourse.julialang.org/t/solving-1d-heat-equation-using-fft/111457 "2024-03-11T12:15:12Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Abdulrhmn\_Ghanem](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abdulrhmn_ghanem/32/205482_2.png) [@Abdulrhmn\_Ghanem](https://discourse.julialang.org/u/Abdulrhmn_Ghanem)
#### Post date: [March 11, 2024, 12:15pm UTC](https://discourse.julialang.org/t/solving-1d-heat-equation-using-fft/111457/1 "2024-03-11T12:15:12Z")

</div>

I am trying to reproduce the solution of heat 1D equation solution from [https://databookuw.com/](https://databookuw.com/).

u\_t = \alpha^2u\_{xx}

where u(t,\ x) is the temperature distribution in time and space.

\mathcal{F}(u(t,\ x)) = \hat u(t, \ kappa)

u\_x \xrightarrow{\mathcal{F}} i k \hat u

\hat u\_t = - \alpha^2 k^2 \hat u

Here are the solutions in [python](https://github.com/dynamicslab/databook_python/blob/master/CH02/CH02_SEC03_1_FFTHeat.ipynb) and [matlab](https://github.com/dynamicslab/databook_matlab/blob/master/CH02/CH02_SEC03_1_FFTHeat.m).

and here is my take on it

```julia
using FFTW, DifferentialEquations

function heat()
	α = 1.0
	L = 100.0
	N = 1000
	dx = L / N
	domain = range(-L/2, stop=L/2-dx, length=N)

	u₀ = collect(0 * domain)
	u₀[400:600] .= 1
	u₀ = fft(u₀, 1)

	kappa = (2π / L) * (-N/2:N/2-1)
	kappa = fftshift(kappa, 1)
	
	tspan = (0, 10)
	t = tspan[1]:0.1:tspan[2]
	params = (α, kappa)

	function rhs(dû, û, p, t)
		α, k = p 
		dû = -α^2 * (k.^2)' .* û
	end
	prob = ODEProblem(rhs, u₀, tspan, params)
	û = solve(prob, saveat=t)
	u = zeros(Complex, size(û)...)
	
	for k in 1:length(t)
		u[:, k] = ifft(û[:, k])
	end
	domain, real(u)
end

```

My results don’t match the other solutions. I have verified that up to calculating the kappa, I am consistent with the other solutions. So I am skeptical of the ODE solution or the ifft step.

---

<div class="post-metadata">

### Author: ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)
#### Post date: [March 11, 2024, 12:29pm UTC](https://discourse.julialang.org/t/solving-1d-heat-equation-using-fft/111457/2 "2024-03-11T12:29:32Z")

</div>

Compare your solution to a known analytic solution for the same \alpha and initial and boundary conditions. E.g. a simple sinusoid initial condition should decay exponentially.

---

<div class="post-metadata">

### Author: ![Abdulrhmn\_Ghanem](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abdulrhmn_ghanem/32/205482_2.png) [@Abdulrhmn\_Ghanem](https://discourse.julialang.org/u/Abdulrhmn_Ghanem)
#### Post date: [March 11, 2024, 12:43pm UTC](https://discourse.julialang.org/t/solving-1d-heat-equation-using-fft/111457/3 "2024-03-11T12:43:51Z")

</div>

My solution is wrong, I couldn’t make any sense of the output 😅

But the python and matlab code solutions match the analytical solution

 ![image](https://global.discourse-cdn.com/julialang/original/3X/c/c/cc0a752af8c0fc9b928709c80bf3a790fb98ec53.jpeg)

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [March 11, 2024, 2:30pm UTC](https://discourse.julialang.org/t/solving-1d-heat-equation-using-fft/111457/4 "2024-03-11T14:30:48Z")

</div>

> [@Abdulrhmn\_Ghanem](#):
>
> `u[k, :] = ifft(û[k, :])`

Why are you doing an ifft in time instead of space? It seems you messed up your indexing here.

---

<div class="post-metadata">

### Author: ![Abdulrhmn\_Ghanem](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abdulrhmn_ghanem/32/205482_2.png) [@Abdulrhmn\_Ghanem](https://discourse.julialang.org/u/Abdulrhmn_Ghanem)
#### Post date: [March 11, 2024, 2:56pm UTC](https://discourse.julialang.org/t/solving-1d-heat-equation-using-fft/111457/5 "2024-03-11T14:56:10Z")

</div>

> [@ChrisRackauckas](#):
>
> Why are you doing an ifft in time instead of space? It seems you messed up your indexing here.

Probably, I’ll flip it and try

---

<div class="post-metadata">

### Author: ![Abdulrhmn\_Ghanem](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abdulrhmn_ghanem/32/205482_2.png) [@Abdulrhmn\_Ghanem](https://discourse.julialang.org/u/Abdulrhmn_Ghanem)
#### Post date: [March 11, 2024, 3:18pm UTC](https://discourse.julialang.org/t/solving-1d-heat-equation-using-fft/111457/6 "2024-03-11T15:18:32Z")

</div>

Switching the Indies fixed the shape of the solution indeed

```julia
# fixed ifft indices
#...
û = solve(prob)
# ...
	
for k in 1:length(eachcol(u))
	u[:, k] = ifft(û[:, k])
end

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/b/2/b2534aecb17f88644b765269080765cf242582ee.png)

But the solution doesn’t get time-stepped (i.e., the final the distribution is the same as the initial condition)

 ![image](https://global.discourse-cdn.com/julialang/original/3X/b/c/bc9a035d09a1d1ff4864516de99890181deade5e.png)

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [March 11, 2024, 4:26pm UTC](https://discourse.julialang.org/t/solving-1d-heat-equation-using-fft/111457/7 "2024-03-11T16:26:46Z")

</div>

> [@Abdulrhmn\_Ghanem](#):
>
> `dû = -α^2 * (k.^2)' .* û`

You’re not updating.

`dû .= -α^2 * (k.^2)' .* û`

---

<div class="post-metadata">

### Author: ![Abdulrhmn\_Ghanem](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abdulrhmn_ghanem/32/205482_2.png) [@Abdulrhmn\_Ghanem](https://discourse.julialang.org/u/Abdulrhmn_Ghanem)
#### Post date: [March 11, 2024, 9:55pm UTC](https://discourse.julialang.org/t/solving-1d-heat-equation-using-fft/111457/8 "2024-03-11T21:55:39Z")

</div>

The solution is unstable 😕 do I need a specific solver to handle `Complex` numbers?

```julia
function rhs(dû, û, p, t)
	α, k = p
	dû .= α^2 * (k.^2) .* û
end
prob = ODEProblem(rhs, u₀, tspan, params)
û = solve(prob, Rodas4P(autodiff=false))

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/e/4/e4712210b6b45febbee6532f5afb910f3458e184.png)

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [March 12, 2024, 12:02pm UTC](https://discourse.julialang.org/t/solving-1d-heat-equation-using-fft/111457/9 "2024-03-12T12:02:59Z")

</div>

Aren’t you missing a `-`?

---

<div class="post-metadata">

### Author: ![photor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/photor/32/14343_2.png) [@photor](https://discourse.julialang.org/u/photor)
#### Post date: [March 12, 2024, 12:56pm UTC](https://discourse.julialang.org/t/solving-1d-heat-equation-using-fft/111457/10 "2024-03-12T12:56:37Z")

</div>

exactly

---

<div class="post-metadata">

### Author: ![Abdulrhmn\_Ghanem](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abdulrhmn_ghanem/32/205482_2.png) [@Abdulrhmn\_Ghanem](https://discourse.julialang.org/u/Abdulrhmn_Ghanem)
#### Post date: [March 12, 2024, 3:32pm UTC](https://discourse.julialang.org/t/solving-1d-heat-equation-using-fft/111457/11 "2024-03-12T15:32:34Z")

</div>

Thank you for y

> [@ChrisRackauckas](#):
>
> Aren’t you missing a `-`?

🤦‍♂️

Thank you for your precious time!
