I need to create a list of expression differentials (1st, 2nd order, and so on) and print results to the Grid.
I'm trying to use next code (and a lot of other variants, but all were wrong). I think the problem is only in the line: 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]]
I am expecting to have something like this as the result:
| dz | *yCos(xy)dx + xCos(xy)dy* |
But the result I have is:
Can you advise me please how to print results in a human-readable format?
May not be exactly what you are looking for, but should be easy to modify.
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]]
Since it computes derivatives of a function of two arguments f[x, y]
, it needs to be passed a function of two arguments.
derivativeGrid[Sin[#1*#2] &, 3, 3]