wolfram-mathematica - 将表达式微分的结果添加到字符串变量
问题描述
我需要创建一个表达式差异列表(一阶、二阶等)并将结果打印到网格。
我正在尝试使用下一个代码(以及许多其他变体,但都是错误的)。我认为问题仅在于:ToString[D[z[x, y], {x, i - j}, {y, j}]]
MyFunction2[z_] := Block[ {x, y},
arr = {{1, 1}, {1, 2, 1}, {1, 3, 3, 1}, {1, 4, 6, 4, 1}};
result = {};
For[i = 1, i <= 4, i++,
res = "";
For[j = 0, j <= i , j++,
res = StringJoin[
res,
If[res == "", "", " + "],
If[arr[[i]][[j + 1]] > 1,
StringJoin[ToString[arr[[i]][[j + 1]]], "*"], ""],
ToString[D[z[x, y], {x, i - j}, {y, j}]],
If[i - j > 0, "dx", ""],
If[i - j > 1, StringJoin["^", ToString[ i - j]], ""],
If[j > 0, "dy", ""],
If[j > 1, StringJoin["^", ToString[j]], ""]
];
];
AppendTo[result, { StringJoin["d", If[i > 1, StringJoin["^", ToString[i]], ""], "z" ], res }];
];
Grid[result, Frame -> All]
];
MyFunction2[Sin[x*y]]
我期待有这样的结果:
| dz | *yCos(xy)dx + xCos(xy)dy* |
但我得到的结果是:
你能告诉我如何以人类可读的格式打印结果吗?
解决方案
可能不完全是您正在寻找的,但应该很容易修改。
derivativeGrid[f_Function, xmax_Integer, ymax_Integer] :=
Module[{derivatives, rowHeader, columnHeader, grid},
derivatives =
Table[D[f[x, y], {x, i}, {y, j}], {i, 0, xmax}, {j, 0, ymax}];
columnHeader = Table["dx"^x, {x, 0, xmax}];
rowHeader = Join[{""}, Table["dy"^y, {y, 0, ymax}]];
grid = MapThread[Prepend, {Prepend[derivatives, columnHeader], rowHeader}];
Grid[grid, ItemStyle -> {{1 -> Bold}, {1 -> Bold}},
Background -> {{LightYellow, None}, {LightYellow, None}},
Frame -> All]]
因为它计算一个有两个参数的函数的导数f[x, y]
,所以它需要传递一个有两个参数的函数。
derivativeGrid[Sin[#1*#2] &, 3, 3]
推荐阅读
- react-native - 反应原生 - `Geolocation.getCurrentPosition` 返回“没有可用的位置提供程序”
- c++ - 如何指定我希望我的 LIB 链接到的 DLL 文件?- 视觉工作室 2019
- php - 我如何将 mysqli_expects 参数 1 修复为给定的 mysqli_result 字符串
- python-3.x - 来自 numpy 的 Pandas 数据框,最后一个维度为对象
- asp.net-core - 如何在浏览器关闭时删除 cookie
- spring - 如果已经登录到主页,则重定向用户 spring boot security
- snowflake-task - 允许的管理帐户数量超出限制。请联系雪花支持
- javascript - 页面加载时如何设置默认导航栏选项卡
- angular - ngx slick carousel 在集成到项目后无法正常工作
- java - 了解反射的奇怪行为