# How to use NDsolve for PDE? [closed]

I am trying to use NDsolve to solve PDE which has an exact solution and evaluate the absolute error:

  ClearAll["Global*"]

f = E^(-3 t) Sin[\[Pi] x] (E^(2 t) (-2 + \[Pi]^2) + Sin[\[Pi] x]^2)

NDSolve[{D[u[t, x], t] == (-u[t, x]^3 + u[t, x]) + D[u[t, x], x, x] +
f, u[0, x] == Sin[Pi*x], u[t, 0] == Exp[-t], u[t, 1] == 0}, u, {t,
0, 1}, {x, 0, 1}]

Plot3D[Evaluate[u[t, x] /. %], {t, 0, 1}, {x, 0, 1}, PlotRange -> All]

Plot3D[Evaluate[E^-t*Sin[Pi*x], {x, 0, 1}, {t, 0, 1},
PlotRange -> All, AxesLabel -> {"x", "t", "Exact solution"},
BaseStyle -> 12]]

Plot3D[Abs[Evaluate[u[t, x] /. %] - (E^-t*Sin[Pi*x])], {x, 0, 1}, {t,
0, 1}, PlotRange -> All, AxesLabel -> {"t", "x"}, PlotLabel -> err]

x = Table[i, {i, 0, 1, 0.1}];

t = Table[i, {i, 0, 1, 0.1}];

Abserror = Evaluate[Abs[Evaluate[u[t, x] /. %] - (E^-t*Sin[Pi*x])]]


but some steps not working!!!

Look up Out in Help. There you will find that:" % gives the last result generated." You are using % way after the last result.

It is best to give a name to you solution. Here I use sol

ClearAll["Global*"]

f = E^(-3 t) Sin[\[Pi] x] (E^(2 t) (-2 + \[Pi]^2) + Sin[\[Pi] x]^2)

sol = First@
NDSolve[{D[u[t, x], t] == (-u[t, x]^3 + u[t, x]) +
D[u[t, x], x, x] + f, u[0, x] == Sin[Pi*x], u[t, 0] == Exp[-t],
u[t, 1] == 0}, u, {t, 0, 1}, {x, 0, 1}]

Plot3D[Evaluate[u[t, x] /. sol], {t, 0, 1}, {x, 0, 1},
PlotRange -> All]

Plot3D[Evaluate[E^-t*Sin[Pi*x], {x, 0, 1}, {t, 0, 1},
PlotRange -> All, AxesLabel -> {"x", "t", "Exact solution"},
BaseStyle -> 12]]

Plot3D[Abs[Evaluate[u[t, x] /. sol] - (E^-t*Sin[Pi*x])], {x, 0,
1}, {t, 0, 1}, PlotRange -> All, AxesLabel -> {"t", "x"},
PlotLabel -> err]

x = Table[i, {i, 0, 1, 0.1}];

t = Table[i, {i, 0, 1, 0.1}];

Abserror = Evaluate[Abs[Evaluate[u[t, x] /. sol] - (E^-t*Sin[Pi*x])]]


Then it all works. You need to resolve you boundary and initial condition warning.

Hope that helps.

• Dear Hugh, many thanks..................Best regards Nov 24 '21 at 11:05