2
$\begingroup$

I have two functions:

f1[x1_,x2_] := 2x1*x2
f2[x1_,x2_] := x1+x2*x1

and I would like to compute this matrix (linearization of the system), but the differentials should first be taken and then the x1, x2 input should be applied. How do I do it?

I tried:

m[x1_, x2_] := {{D[f1[x1, x2], x1], 
   D[f1[x1, x2], x2]}, {D[f2[x1, x2], x1], D[f2[x1, x2], x2]}}

but it does not work...

EDIT: The documentation was not very clear to me, so for people who also need a bit of help to understand the difference of "Set" and "Set Delay" :

The function definition without ":" before the "=" (Set) evaluates the function a first time and the output is assigned to m[x1_,x2_], so if you input values they are feed into the already-differentiated equations.

If now, you use ":" before the "=", (Set Delay), it evaluates the function when it is needed, hence it will evaluate the function only when values for x1 and x2 are set, hence it will not work, since one cannot differentiate a function with respect to 5 (a numerical value for example).

$\endgroup$
  • $\begingroup$ You are using double [[ ]] on your definition of m. Should be single [ ] for a function definition. $\endgroup$ – LouisB Oct 16 '16 at 8:37
  • $\begingroup$ Thanks. Did not see that, but still does not work, since I first want to calculate the differential and then input the x1 and x2 values. $\endgroup$ – henry Oct 16 '16 at 8:41
3
$\begingroup$
f1[x1_, x2_] = 2 x1*x2;
f2[x1_, x2_] = x1 + x2*x1;
m[x1_, x2_] = {Grad[f1[x1, x2], {x1, x2}], Grad[f2[x1, x2], {x1, x2}]}

{{2 x2, 2 x1}, {1 + x2, x1}}

m[1, 2]
{{4, 2}, {3, 1}}

Edit

To answer your question in the comment: Look up in the documention `Set' and 'SetDelayed' . Note the two examples:

1.)

f1[x1_, x2_] := 2 x1*x2;
f2[x1_, x2_] := x1 + x2*x1;
m[x1_, x2_] = {Grad[f1[x1, x2], {x1, x2}], Grad[f2[x1, x2], {x1, x2}]}

{{2 x2, 2 x1}, {1 + x2, x1}}
m[1, 2]
{{4, 2}, {3, 1}}

2.)

f1[x1_, x2_] := 2 x1*x2;
f2[x1_, x2_] := x1 + x2*x1;
m[x1_, x2_] := {Grad[f1[x1, x2], {x1, x2}], Grad[f2[x1, x2], {x1, x2}]}

m[1,2]

enter image description here

$\endgroup$
  • $\begingroup$ Why does it work when you do not use the ":" before the "=" ? $\endgroup$ – henry Oct 16 '16 at 8:47
  • $\begingroup$ Thank you for the edit, I think I understand: Set, so without ":" evaluates the function a first time and the output is assigned to m[x1_,x2_], so if you input values they are feed into the already-differentiated equations. If know, you use ":", so Set Delay, it evaluates the function when it is needed, hence it will evaluate the function only when values for x1 and x2 are set, hence it will not work, since one cannot differentiate a function with respect to 5 (a numerical value, example). $\endgroup$ – henry Oct 16 '16 at 9:28
  • $\begingroup$ if my previous comment is correct, would you mind to add it to your response, so that people can quickly see the complete answer to my question... Thanks! $\endgroup$ – henry Oct 16 '16 at 9:29
  • $\begingroup$ @DoHe The documentation is understandable. I would not repeat here. $\endgroup$ – user36273 Oct 16 '16 at 10:23
  • $\begingroup$ okay, I mentioned it in my question for people (like me) who need a different explenation. $\endgroup$ – henry Oct 16 '16 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.