m=21# The number of 'space' nodesn=401# The number of 'time' nodesτ=10/(n-1)# Time step sizeh=1/(m-1)# Space step sizeλ=0.3α=0.3+0.1*(1-cos(1))# Integral of g(x) = 0.3 +0.1sin(x) = 0.3+0.1(1-cos1)x=vec(linspace(0,1,m))t=vec(linspace(0,10,n))α=0.3+0.1*(1-cos(1))# Integral of g(x) = 0.3 +0.1sin(x) = 0.3+0.1(1-cos1)μ=1./(1-x).^2;μ[1]=0;μ[m]=0;β=zeros(m,m)γ=(1-λ*τ+h*τ/2)optv=0.85+0.05cos(2π*t)# The optimal distribution we are aiming at## Calcuate AA=zeros(m,m)fori=2:m-1A[i,i-1]=1-τ*μ[i]+τ/hA[i,i+1]=1-τ*μ[i]-τ/hend## Calculate gg=(0.3+0.1*sin(x))## Calculate Bβ=zeros(n,n)#println(size(x), " ", size(t), " ", size(g), " ", size(A), " ", size(μ'))β[1,1]=αforj=2:nfork=1:j-1a=(A^(j-1-k))'*μβ[j,k]=dot(μ,g)[1]endβ[j,j]=αendB=zeros(n,n)forj=2:nfork=1:jB[j,:]+=γ^(j-k)*β[k,:]endendB=τ*Bc=zeros(n,1)fori=1:nc[i]=γ^(i-1)endestb=(inv(B'B)*B')*vec(c-optv)estv=-B*estb+cerrv=norm(estv-optv,2);Pkg.add("Gadfly")usingGadfly#plot(x=t[2:n-1], y=estv[2:n-1], Guide.XLabel("Time"), Guide.YLabel("Optimal p0"))plot(x=t[2:n-1],y=abs(estv-optv))