请用mathematica写一段代码能够生成一个格子第一行是自变量的取值范围第二行是函数的导数的正负和0的判断第三行是函数的单调性和极值点如textErrorBoxlefttextGridBoxleftleftbeginarraycccc textx & textRowBoxtextErrorBoxtextRowBox01 & 1 & textRowBoxtextRowBox1textRowBox
这里给出一个可能的代码实现:
f[x_] := x^3 - 3 x^2 + x + 2; (* 定义一个示例函数 *)
range = {-2, 3}; (* 自变量的取值范围 *)
(* 计算函数的导数值,并判断正负和0 *) deriv = D[f[x], x]; signs = Sign[deriv /. x -> #] & /@ range; zeros = Select[range, deriv /. x -> # == 0 &];
(* 判断函数的单调性和极值点 *) monotonicity = If[deriv > 0, "increasing", If[deriv < 0, "decreasing", "constant"]] /. x -> # & /@ range; extremes = If[deriv' == 0 && deriv'' < 0, "maximum", If[deriv' == 0 && deriv'' > 0, "minimum", ""]] /. x -> # & /@ zeros;
(* 构造表格 *) Grid[{{"x", Range @@ range, SpanFromLeft}, {"f'(x)", If[# > 0, "+", If[# < 0, "-", "0"]] & /@ signs, SpanFromLeft}, {"f(x)", If[monotonicity[[1]] == "increasing", FormBox[TagBox["[NearArrow]", MathMLPresentationTag, AutoDelete -> True], TraditionalForm], FormBox[TagBox["[Searrow]", MathMLPresentationTag, AutoDelete -> True], TraditionalForm]], ToString@MaxValue[{f[x], x [Element] Interval[range]}, x], If[monotonicity[[2]] == "decreasing", FormBox[TagBox["[Nearrow]", MathMLPresentationTag, AutoDelete -> True], TraditionalForm], FormBox[TagBox["[Searrow]", MathMLPresentationTag, AutoDelete -> True], TraditionalForm]]}, {"zeros of f'(x)", If[# != {} && MemberQ[#, 0], Row[{"(", Row[#[[;; Position[#, 0][[1, 1]] - 1]], ","], Row[#[[Position[#, 0][[1, 1]] + 1 ;;]]], ")"}], Row[{"(", Row[#, ","], ")"}]] &@Split[zeros]}, {"extremes of f(x)", "", If[extremes[[1]] != "", ToString[#] <> " (" <> extremes[[1]] <> ")" & /@ zeros, ""]}}], Frame -> All, Alignment -> {Center, Center}, BaseStyle -> {FontFamily -> "Times"}
原文地址: http://www.cveoy.top/t/topic/hmk7 著作权归作者所有。请勿转载和采集!