原代码我就不细测了。总之用Piecewise是可以的。(再强调一遍,Piecewise是Mathematica里几种定义分段函数的方法里最稳定的。)当然,似乎是由于这个初始条件不够光滑,在使用NDSolve求解这个定解问题的时候需要在选项上下点功夫:
(* 这里使用了我惯用的拟谱方法。直觉上应该还有别的可用选项。拟谱并非必要也说不定,不过今天已经快过去了,这里不再多尝试 *)
nipu[n_] := {"MethodOfLines",
"SpatialDiscretization" -> {"TensorProductGrid", "MaxPoints" -> n,
"MinPoints" -> n, "DifferenceOrder" -> "Pseudospectral"}}
sol = NDSolve[{D[u[t, x], t, t] == D[u[t, x], x, x], u[t, 0] == 0,
u[t, 1] == 0, u[0, x] == F[x], (D[u[t, x], t] /. t -> 0) == 0},
u, {t, 0, 1}, {x, 0, 1}, Method -> nipu[35]][[1, 1, 2]]
Plot3D[sol[x, t], {x, 0, 1}, {t, 0, 1}]