Group Abstract Group Abstract

Message Boards Message Boards

0
|
1.9K Views
|
2 Replies
|
1 Total Like
View groups...
Share
Share this post:

[?] Add a result of expression's differentiation result to string variable?

Posted 7 years ago

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). 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 result I have is (the first 2 rows of the Grid):enter image description here

Can you advise me please how can I convert the result of D[z[x, y], {x, i - j}, {y, j}] to the human-readable format? (I tried TraditionalForm[] but it is not what I exactly need)

POSTED BY: Dmitriy K
2 Replies
Posted 7 years ago

This question was also asked on StackOverflow. See answer.

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]

enter image description here

POSTED BY: Rohit Namjoshi
Posted 7 years ago

Thanks a lot! The great solution!

POSTED BY: Dmitriy K
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard