Message Boards Message Boards

0
|
357 Views
|
8 Replies
|
9 Total Likes
View groups...
Share
Share this post:

Jacobian matrix of two functions

Posted 6 days ago

Greetings --

Apologies if this is posted to the wrong forum. Have been using Maple and Maxima for 20+ years, and have only recently started using Mathematica (v. 14). Having a heck of a time getting Mathematica to do what is trivial in either Maple or Maxima (not a slam on Mathematica - just an honest statement of where I am on the learning curve). Simple example: 2 equations in 2 unknowns (say, x1 and x2). Want to derive the Jacobian of these equations wrt x1 and x2, and print said Jacobian in matrix form. This is basically 3-4 lines in Maple or Maxima. But in Mathematica? Here is what I've tried.

  (*Define the functions*)
  f1[x1_, x2_] := x1^2 + x2^2
  f2[x1_, x2_] := x1 x2

  (*Compute the Jacobian matrix with respect to x1 and x2*)
  jacobian = JacobianMatrix[{f1[x1, x2], f2[x1, x2]}, {x1, x2}]

  (*Display the Jacobian matrix*) 
  MatrixForm[jacobian]

But, the final command to display the Jacobian returns nada of any use. It simply returns the following:

  JacobianMatrix[{x1^2+x2^2,x1x2},{x1,x2}]

So, how the heck do I get Mathematica to output what Maple (for example - same code more or less in Maxima) outputs in one command, looking like what I expect (i.e., the formatted Jacobian matrix, below):

enter image description here

Many thanks in advance. I was warned the learning curve for Mathematica was appreciably steeper than (say) Maple, but I wasn't expected even something this simple to stump me quite so soon.

POSTED BY: Evan Cooch
8 Replies
(* Define the functions *)
f1[x1_, x2_] := x1^2 + x2^2 f2[x1_, x2_] := x1 x2
(* Compute the Jacobian matrix with respect to x1 and x2 *)
jacobian = D[{f1[x1, x2], f2[x1, x2]}, {{x1, x2}}]
(* Display the Jacobian matrix *)
MatrixForm[jacobian]
POSTED BY: kaylene ginny

You can simulate the Maple syntax quite easily:

f1 = x1^2 + x2^2;
f2 = x1*x2;
jac = D[{f1, f2}, {{x1, x2}}];
MatrixForm[jac]

The only notable difference is the display of the matrix.

POSTED BY: Gianluca Gorni
Posted 6 days ago

Following works - but having to figure out/remember "ResourceFunction" is clunky...and I'm still nt entirely sure I know what a 'resource function' is.

f1 = (x1)^2+(x2)^2
f2 = (x1)*(x2)
jac = ResourceFunction["JacobianMatrix"][{f1,f2},{x1,x2}]
MatrixForm[jac]   

Compare this to (say) Maxima (lest someone think I'm only trolling a comparison of Maple and Mathematica) -- note that Maxima by default outputs each step, properly formatted so that it actually looks like math (still have no idea how to get Mathematica to do that).

enter image description here

POSTED BY: Evan Cooch
Posted 6 days ago

Resource functions are just useful functions that have been submitted to the resource function repository by users. There is a whole system for submitting them and getting them accepted. They aren't part of the core language, so to access them, you need to go through the resource function repository. But again, there's D, which is core to the language, so I'm not sure what the problem is. The Jacobian is even mentioned in the documentation for D.

Side note: comparing Mathematica to Maxima or Maple is not really appropriate for this forum. This forum is just here to help folks with Mathematica. If you're suggesting feature requests or criticisms, that's something you should send to Wolfram directly, not this forum.

POSTED BY: Eric Rimbey
Posted 6 days ago

Oh, if it's a resource function, then just "fetch" it from the resource repository.

f1[x1_, x2_] := x1^2 + x2^2;
f2[x1_, x2_] := x1 x2;
ResourceFunction["JacobianMatrix"][{f1[x1, x2], f2[x1, x2]}, {x1, x2}]
(* {{2 x1, 2 x2}, {x2, x1}} *)

If you don't like typing all of that each time, you can define a symbol for it:

myJacobian = ResourceFunction["JacobianMatrix"];
myJacobian[{f1[x1, x2], f2[x1, x2]}, {x1, x2}]

But I don't really see what's so cumbersome about just using the built in D symbol.

D[{f1[x1, x2], f2[x1, x2]}, {{x1, x2}}]
POSTED BY: Eric Rimbey
Posted 6 days ago

I don't know where you came across the symbol JacobianMatrix, but I don't think that's a built in symbol. You can just use the normal derivative.

D[{x1^2 + x2^2, x1 x2}, {{x1, x2}}]
(* {{2 x1, 2 x2}, {x2, x1}} *)

And just let me say proactively, that MatrixForm is only for display (which is fine for what you have so far), so don't try to compute further with that specific form.

POSTED BY: Eric Rimbey
Posted 6 days ago

Simple - JacobianMatrix (or equivalent) is baked in to Maple, Maxima, MATLAB, and every other CAS or CAS-like environment I've used. And, it is in Mathematica, but apparently it is something called a 'Resource Function' (no idea what that is). See below:

enter image description here

What you suggested works, but it is cumbersome for large sets of equations.

POSTED BY: Evan Cooch

Why do you keep ignoring D[{f1, f2}, {{x1, x2}}], which a couple of others have mentioned? (Assumes f1 = x1^2 + x2^2; f2 = x1 x2.)

Is it just because it's not named "Jacobian"? It's one of the problems when we adopt a person's name as the basis for calling a common object, such as the derivative, something else. (Of course, I believe the "Jacobian matrix" came into use before a vector-matrix-tensor view of multivariate functions and their derivatives became dominant. So there's a historical reason why "Jacobian" is used.)

Anyway, D[{f1, f2}, {{x1, x2}}] is shorter than Jacobian[{f1, f2}, {x1, x2}]. And so is Grad[{f1, f2}, {x1, x2}], which also works.

D[{f1, f2}, {{x1, x2}}] is the currently documented way to calculate the Jacobian (in the docs for D[]). The method from the version 2 Mathematica book is Outer[D, {f1, f2}, {x1, x2}], which also appears in the docs for D[]. When I search the help center for "jacobian" (lower case), D[] is the fourth hit in the desktop documentation; it's a disappointing eighth in the online docs, after the despisèd "JacobianMatrix" resource function.

You mentioned large sets of equations. It's not exactly clear what you mean, but here's what I do when I have equations and variables that I don't want retype. You need a list of the variables, and their order matters usually. If you can name them such that Sort[] puts them in the desired order, then great, you can just extract the variables from the formulas and Union[] them. Otherwise, you'll probably have to type them by hand.

vars = {x1, x2, x3, x4};
funcs ={f1, f2, f3, f4}; (* pretend they've been defined in terms of vars *)
jac = D[funcs, {vars}]
POSTED BY: Michael Rogers
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract