Mathematica 9 中语句不显示错误,不运行结果?For[n = 1,n {"x/a","\!\(\*FractionBox[\(a\),\(2\)]\)|\[Psi]\!\(\*SuperscriptBox[\(|\\),\(2\)]\)"},PlotLabel -> "n=4",AspectRatio -> 1/3]]

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/08 14:35:27
Mathematica 9 中语句不显示错误,不运行结果?For[n = 1,n { "n=4",AspectRatio -> 1/3]]" />

Mathematica 9 中语句不显示错误,不运行结果?For[n = 1,n {"x/a","\!\(\*FractionBox[\(a\),\(2\)]\)|\[Psi]\!\(\*SuperscriptBox[\(|\\),\(2\)]\)"},PlotLabel -> "n=4",AspectRatio -> 1/3]]
Mathematica 9 中语句不显示错误,不运行结果?
For[n = 1,n {"x/a",
"\!\(\*FractionBox[\(a\),\(2\)]\)|\[Psi]\!\(\*SuperscriptBox[\(|\
\),\(2\)]\)"},PlotLabel -> "n=4",AspectRatio -> 1/3]]

Mathematica 9 中语句不显示错误,不运行结果?For[n = 1,n {"x/a","\!\(\*FractionBox[\(a\),\(2\)]\)|\[Psi]\!\(\*SuperscriptBox[\(|\\),\(2\)]\)"},PlotLabel -> "n=4",AspectRatio -> 1/3]]

原因是,除非使用明确的 Return,否则由 For 返回的最后的值是Null.所以,在循环完之后,没有返回值作为Out输出.一个简单的方法是,在Plot函数外套一个Print函数.

另外程序中PlotLabel -> "n=4"要改成PlotLabel -> StringForm["n=``", n],否则n不能传递到字符串中.

但是,这不是Mathematica的做事风格.Mathematica的风格是函数式编程,而不是过程式编程,因此,将上述代码变成

Table[Plot[Sin[n*\[Pi]*x]^2, {x, 0, 1}, 
  AxesLabel -> {"x/a", 
    "\!\(\*FractionBox[\(a\), \
\(2\)]\)|\[Psi]\!\(\*SuperscriptBox[\(|\), \(2\)]\)"}, 
  PlotLabel -> StringForm["n=``", n], AspectRatio -> 1/3], {n, 1, 5}]

更自然一些