I have a pair of equations f= {eq1,eq2} that are (x,y) dependent. I take derivative by (x) and (y) for each equation, for example,

 Grad[f, {x, y}] . {1, 1}] 

and multiply it with a Xlist. The functions are dependent on Xlist too say {{1,2,3},{2,2,4}} For some functions (say f1) my output is listed like this


The function f1 usually is

 f1={xy+x^2,xy+y^2 }  

form. For other functions like

 f2 ={x,y^2} 



the output is


for f1 and


for f2. It causing me problems for next calculations because the output format is required to be like {{},{},{}....} etc. Looks like derivative of functions such as f2 and f3 yield constant or scalar f2 = (scalar, vector) and f3 =(scalar,scalar). How do I make scalar stay as a vector? How do I turn list for example {1,{3,4,..} or {{3,2,2},3} into {{1},{3,4,..} or {{3,2,2},{3}}? I have tried a rule method but my approach did not seems to pan out correctly,

 code /. {a_,{b_}}->{{a},{b}}

because output of size derivative vary with list of functions and Xlist.


Test code is

 Clear[f1, f2, f3, fun1, fun2]

 f3 = {a y + 1, x + 2}

 {1 + a y, 2 + x}

 f2 = {y + x a, x y a}

 {a x + y, a x y}

 f1 = {a y (2 - x), a x (2 - y)}

 {a (2 - x) y, a x (2 - y)}

 fun1[a_][{x_, y_}] = f2;
 dfun1[a_][{x_, y_}] := f2;
 fun2[a_][{x_, y_}] = Grad[dfun1[a][{x, y}], {x, y}] . {1, 1};
 list = Drop[NestList[fun1[0.88], {1, 2}, 4], 2]

 {{4.2944, 4.46054}, {8.23962, 16.8567}, {24.1076, 122.226}}

 {{4.2944, 8.23962, 24.1076}, {4.46054, 16.8567, 122.226}}

 {1.88, {7.70435, 22.0848, 128.773}}

 Transpose::nmtx: The first two levels of {1.88, {7.70435,22.0848,128.773}} cannot be transposed.

 Transpose[{1.88, {7.70435, 22.0848, 128.773}}]

I believe the output should be {{1.88,1.88,1.88} , {7.70435, 22.0848, 128.773}} If you type

  fun1[a_][{x_, y_}] = f1; 



it proceed without failing to transpose. If your change f1 to f2 or f3, it will fail to compile without transpose error. It is due to Grad function when the result is scalar not some x or/and y values. How to impose this step to include {} brackets even when value is scalar? Or is there alternative approach? Note that size of the NestList is not fixed.

  • 1
    $\begingroup$ do you have a syntax error? do you mean simply Grad[f, {x, y}]? I get e.g. {{2 x, 0}, {0, 2 y}} with f1. taking {{2 x, 0}, {0, 2 y}} . {1, 1} gives {2x, 2y}. I'm not quite sure how you're getting the outputs you're getting; can you elaborate a bit? $\endgroup$
    – thorimur
    Jul 10 at 0:54
  • $\begingroup$ Okay, I will write up a test code to demonstrate it. $\endgroup$
    – Aschoolar
    Jul 10 at 1:32
  • $\begingroup$ Updated with test code in question post. Run it with f1 which works and then with f2 or f3 which fails. I already explained why I just do not know how to fix it. The curly brackets are missing for f2 and f3 solution because derivative yields scalar. $\endgroup$
    – Aschoolar
    Jul 10 at 1:56
  • $\begingroup$ Still, I don't quite understand your question. In the code sample, the line Abs[Grad[fun2[a][{x, y}], {x, y}] . {1, 1}] is already improper, the output involves Abs', you probably need RealAbs. $\endgroup$
    – xzczd
    Jul 10 at 2:14
  • $\begingroup$ Wait, are you looking for fun2[0.88] /@ list? If so, I really suggest you to put a bit more effort in learning the core language. A possible starting point is here: mathematica.stackexchange.com/a/25616/1871 Also, consider reading this book: mathprogramming-intro.org $\endgroup$
    – xzczd
    Jul 10 at 2:17

Your Answer

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

Browse other questions tagged or ask your own question.