1
$\begingroup$
J[x_Function, y_Function] :=
Module[xu, xv, yu, yv]
xu = D[x, u];
xv = D[x, v];
yu = D[y, u];
yv = D[y, v];
matrix = {{xu, xv}, {yu, yv}};
jacobian = Det[matrix];
Print["The Jacobian is " <> ToString[jacobian]]

This is supposed to give the Jacobian for the change of variables x = x(u,v) and y = y(u,v), however if I input functions of u and v for x and y, it just returns

J[u + v, u - v]

Please help.

$\endgroup$
2
  • 1
    $\begingroup$ The module should be Module[{xu, xv, yu, yv},...]. $\endgroup$ Commented Sep 5, 2018 at 2:48
  • 2
    $\begingroup$ u + v has head Plus, not head Function that you specified, so it is not valid argument for your function. Same for u - v. $\endgroup$ Commented Sep 5, 2018 at 4:14

2 Answers 2

2
$\begingroup$

The function D can directly compute Jacobian matrix

D[{x[u, v], y[u, v]}, {{u, v}}]

For your specific case

D[{u + v, u - v}, {{u, v}}]
Det[%]

{{1, 1}, {1, -1}}

-2

$\endgroup$
0
$\begingroup$

There are some syntactical problems with the provided code-as noted in the comments- but apart from that, it would be possible to perform the same operations as the ones in the Q, with evaluating something that would eventually get to a form such as J[#1+#2&,#1-#2&] in order to accommodate the definition of J.

J is defined in such a way as to receive two arguments which have head Function. In the code example of the Q, J[u+v,u-v] does not evaluate as expected because the provided arguments have Head Plus not Function (as already pointed to in the comments). Here, the arguments passed to J are 'functions'.

Now, I am not sure you will be happy with this iteration of your code either, because-in all likelihood-those x and ys are already defined in terms of u and v and you'll have to rewrite them in terms of Slots #1 and #2 (rewrite them as pure functions in order for them to 'play nice' with the initial def. of J.)

Also, in this case, the remaing of the code needs refactoring too eg something like D[x, u] would have to be re- written as D[x[u,v],u] to take into considetation the fact that x is in fact a pure function of two arguments etc.

Additionally, relevant to the possible code refactoring is the fact that you can construct the Jacobian matrix in a more compact way as displayed in another nice answer to your Q.

In closing, refactor the code either to allow J to accept expressions other than Function as arguments or change the x and y functions to pure functions along with appropriate changes to the derivatives; also perhaps consider a more compact way for writing the Jacobian.

$\endgroup$
2
  • $\begingroup$ But what my question really is is how to modify my code to make it produce the right Jacobian, because I'm not that good with pure functions $\endgroup$ Commented Sep 5, 2018 at 18:51
  • $\begingroup$ Hello @SamTHEDuck999! You could try defining J in a way that doesn't restrict its arguments to be pure functions (lose the _Function pattern), as I have already alluded to in my answer and perhaps edit your question with any additional issues you encounter during the refactoring process. $\endgroup$ Commented Sep 6, 2018 at 10:54

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.