# Spetrum method and DifferentialEquaitons

**URL:** https://discourse.julialang.org/t/spetrum-method-and-differentialequaitons/42919
**Category:** General Usage
**Tags:** differentiation
**Created:** [July 12, 2020, 3:52am UTC](https://discourse.julialang.org/t/spetrum-method-and-differentialequaitons/42919 "2020-07-12T03:52:28Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![hzgzh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hzgzh/32/6596_2.png) [@hzgzh](https://discourse.julialang.org/u/hzgzh)
#### Post date: [July 12, 2020, 3:52am UTC](https://discourse.julialang.org/t/spetrum-method-and-differentialequaitons/42919/1 "2020-07-12T03:52:28Z")

</div>

Hi,  
I use Differentialequantions to solve the pde,

\left\{\begin{array}{l} \frac{\partial u}{\partial t}=\varepsilon \frac{\partial^{2} u}{\partial x^{2}}+u-u^{3}, \quad-1\<x\<1 \\ \left.u\right|\_{x=\pm 1}=\pm 1 \\ \left.u\right|\_{t=0}=0.53 x-0.47 \sin (1.5 \pi x) \end{array}\right.

the spartial discrete using spectrum method,but the result as not I want

```julia
using LinearAlgebra
using Plots
using OrdinaryDiffEq

function cheb(n)
    @assert n>0 "N should great than 1"
    
    x=cos.(π*(0:n)./n)
    c=vcat(2,ones(n-1),2).*(-1).^(0:n)
    X=repeat(x,1,n+1)
    ΔX=X-X'
    D=(c*(1 ./c)')./(ΔX+I)
    D-=diagm(sum(D,dims=2)[:])
    D,x
end

meshgrid(x,y)=repeat(x',length(x),1),repeat(y,1,length(y))

L=2;N=20
D,x=cheb(N);D=D./(L/2)
x=(L/2).*x;D2=D^2
#initial condition
u₀=@. 0.53x-0.47*sin(1.5π*x)
function allen_cahn(du,u,p,t,D2)
    ϵ=p
    du=ϵ*D2*u+u-u.^3.0
    du[1]=0.0;du[end]=0.0
    du
end
tspan=(0.0,100.0);ϵ=0.01
prob=ODEProblem((du,u,p,t)->allen_cahn(du,u,p,t,D2),u₀,tspan,ϵ)
sol=solve(prob,Tsit5();saveat=2,reltol=1e-8,abstol=1e-8);
surface(sol.t,x,sol[:,:],xlabel="t",ylabel="x",zlabel="u")

```

the u not integrate,how do I choose ode alg or something

---

<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: [July 12, 2020, 6:11am UTC](https://discourse.julialang.org/t/spetrum-method-and-differentialequaitons/42919/2 "2020-07-12T06:11:11Z")

</div>

> [@hzgzh](#):
>
> ```julia
> function allen_cahn(du,u,p,t,D2)
> ϵ=p
> du=ϵ*D2*u+u-u.^3.0
> du[1]=0.0;du[end]=0.0
> du
> end
> 
> ```

You didn’t mutate the vector.

```julia
function allen_cahn(du,u,p,t,D2)
    ϵ=p
    du.=ϵ*D2*u+u-u.^3.0
    du[1]=0.0;du[end]=0.0
    nothing
end

```

is the easiest fix, but it’s still allocating of course.

---

<div class="post-metadata">

### Author: ![hzgzh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hzgzh/32/6596_2.png) [@hzgzh](https://discourse.julialang.org/u/hzgzh)
#### Post date: [July 12, 2020, 6:29am UTC](https://discourse.julialang.org/t/spetrum-method-and-differentialequaitons/42919/3 "2020-07-12T06:29:51Z")

</div>

Hi,ChrisRackauckas  
result correct,thank
