输入的语句如下:
f[x_] := Which[-2Pi ≤ x < -Pi, 1, -Pi ≤ x < 0, -x, 0 ≤ x < Pi, 1,
Pi ≤ x < 2Pi, 2Pi - x];
a[n_] := (Integrate[-x*Cos[nx], {x, -Pi, 0}] + Integrate[Cos[nx], {x,
0, Pi}])/Pi;
b[n_] := (Integrate[-x*
Sin[nx], {x, -Pi, 0}] + Integrate[Sin[nx], {x, 0, Pi}])/Pi;
s[x_, n_] := a[0]/2 + Sum[a[k]*Cos[kx] + b[k]*Sin[kx], {k, 1, n}];
g1 = Plot[f[x], {x, -2Pi,
2Pi}, PlotStyle -> RGBColor[0, 0, 1], DisplayFunction -> Identity];
m = 18;
For[i = 1, i
≤ m, i += 2, g2 = Plot[Evaluate[s[x,
i]], {x, -2Pi, 2Pi}, DisplayFunction -> Identity]; Show[g1, g2,
DisplayFunction -> $DisplayFunction]]
运行后就出现了Plot::plnr :。。。(一个表达式)is not a machine-size real number at x = -6.28318.
应该怎么做才对呀?
你的程序有点错误,我把正确的代码贴出来,就不刻意给你指正错误了,只插入最后一张图,你复制命令后得到的是同样的结果:
f[x_] := Which[Inequality[-2*Pi, LessEqual, x, Less, -Pi], 1,
Inequality[-Pi, LessEqual, x, Less, 0], -x,
Inequality[0, LessEqual, x, Less, Pi], 1,
Inequality[Pi, LessEqual,
x, Less, 2*Pi], 2*Pi - x];
a[n_] := (Integrate[Cos[n*x], {x, 0, Pi}] + Integrate[(-x)*Cos[n*x],
{x, -Pi, 0}])/Pi;
b[n_] := (Integrate[Sin[n*x], {x, 0, Pi}] + Integrate[(-x)*Sin[n*x],
{x, -Pi, 0}])/Pi;
s[x_, n_Integer] := Sum[a[k]*Cos[k*x] + b[k]*Sin[k*x], {k, 1, n}] +
a[0]/2;
g1 = Plot[f[x], {x, -2*Pi, 2*Pi}, PlotStyle -> RGBColor[0, 0, 1],
DisplayFunction -> Identity];
m = 18;
For[i = 1, i <= m, i += 2,
g2 = Plot[Evaluate[s[x, i]], {x, -2*Pi, 2*Pi},
DisplayFunction -> Identity]; Print[Show[g1, g2]]]