# How to solve a PDE problem by Fourier series method? [closed]

eo = Integrate[(Cos[Pi*x])^2, {x, 0, 1}]
en := 2*Integrate[(Cos[Pi*x])^2*Cos[n*Pi*x], {x, 0, 1}]
ca = Table[en, {n, 1, 20}]
ListPlot[ca]
v[x_, t_, N_] := eo + Sum[en*Cos[Pi*n*x]*E^[-(n^2)*(Pi^2)*t], {n, 1, N}]
v[x, t, 20]
ContourPlot[v[x, t, 20], {x, 0, Pi}, {t, 0, 0.5},
ColorFunction \[RightArrow] "DarkRainbow"]
u[x_, t_, b_, N_] := v[x, t, N]*E^[b*x]
u[x, t, b, 20]
ContourPlot[u[x, t, 5, 20], {x, 0, Pi}, {t, 0, 0.5},
ColorFunction \[RightArrow] "DarkRainbow"]


but it seems not to work, and I can't understand the reason. I was trying to solve $$v_t=v_{xx}$$ with $$v_x(0,t)=v_x(1,t)=0, v(x,0)=(\cos {(\pi x)})^2, 00.$$ I calculated analytically and show the results with the partial sums, that is my goal.
Any help?

• Not sure what you are doing here. Are you trying to calculate the Fourier series for Cos^2 over half a period? There are built-in routines for Fourier. – Hugh Mar 26 at 11:38
• I was trying to solve: $v_t=v_{xx}$ with $v_x(0,t)=v_x(1,t)=0, v[x,0]=(\cos {(\pi x)})^2, 0<x<1,t >0.$ I calculated analytically and show the results, that is my goal – George Mar 26 at 11:42
• There're multiple simple mistakes. 1. \[RightArrow] should be ->, notice they're different. 2. The E^[ should be e.g. Exp[. 3. {x,0,Pi} should be {x,0,1}. 4. If the ListPlot doesn't even work, consider Clear[x,n] at the beginning. – xzczd Mar 26 at 13:34

Mathematica solves your PDE with the boundary and initial conditions by

sol = DSolve[{D[v[x, t], t] == D[v[x, t], {x, 2}], Derivative[1, 0][v][0, t] == 0,
Derivative[1, 0][v][1, t] == 0, v[x, 0] == Cos[Pi*x^2]^2}, v[x,t], {x, t}]

(* {{v[x, t] ->  1/4 (2 + FresnelC[2]) +
2 Inactive[Sum][ 1/8 E^(-\[Pi]^2 t K[1]^2)
Cos[\[Pi] x K[
1]] (Cos[1/8 \[Pi] K[1]^2] FresnelC[2 - K[1]/2] +
Cos[1/8 \[Pi] K[1]^2] FresnelC[
1/2 (4 + K[1])] + (FresnelS[2 - K[1]/2] +
FresnelS[1/2 (4 + K[1])]) Sin[1/8 \[Pi] K[1]^2]), {K[1],
1, \[Infinity]}]*)


In order to visualize the solution,

asol = v[x, t] /. sol[[1]] /. {\[Infinity] -> 3} // Activate;
Plot3D[asol // Evaluate, {x, 0, 1}, {t, 0, 2}, Exclusions -> None,
PlotRange -> All, PlotPoints -> 50]


PS. It seems you changed the initial condition from $$\cos(x^2))^2$$ to $$\cos(x)^2$$. In this case

DSolve[{D[v[x, t], t] == D[v[x, t], {x, 2}], Derivative[1, 0][v][0, t] == 0, Derivative[1, 0][v][1, t] == 0, v[x, 0] == Cos[Pi*x]^2}, v[x, t], {x, t}]
(*{{v[x, t] -> 1/2 (1 + E^(-4 \[Pi]^2 t) Cos[2 \[Pi] x])}}*)

• I need to show the partial sums, as I tried and plot them – George Mar 26 at 12:22