Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.
Juju3[expr_] :=
(Solve[expr[[1]] == 0, expr[[2]] == 0, expr[[3]] == 0, {r, x, y}];
a = %[[1]];
b = %%[[2]];
c = %%%[[3]];
Which[a[[2, 2]] == 0 || a[[3, 2]] == 0, Speak["MOO"],a[[2, 2]] != 0 && a[[3, 2]] != 0, Speak["PEACE AND LOVE"]]
j = D[expr, {{r, x, y}}];
j
FullSimplify[j /.Solve[expr[[1]] == 0, expr[[2]] == 0,expr[[3]] == 0, {r, x, y}]] ;
FullSimplify[Eigenvalues[%[[1]]]]
FullSimplify[Eigenvalues[%%[[2]]]]
FullSimplify[Eigenvalues[%%%[[3]]]])

Then I tried:

Juju3[{i - l*r - ux*r*x - uy*r*y, -mx*x + ex*ux*r*x, -my*y + ey*uy*r*y}]

,and it did not work whereas when I just use my code without creating a function it works very well. I am reading about building functions with Mathematica right now but I still want to post this because I do not know what I am doing wrong. Everything? Maybe! Because I am a noob ^^ But I am reading tutorials so please do not get angry :)

Specifically I have been told by a user that one should put semicolon for things that one do not want the output of, and also that one should put parentheses when having a function with multiple lines, so I did do that here.

PS: I shortened the function because I use b and c also in the same way as a but no need for it here I guess.

share|improve this question
"mathematica.stackexchange.com/questions/18/…; and your solve generates a book...if amended for Reals. – Blackbird Aug 6 at 15:59
2  
I'm worried by those "%" signs. Can you explain what they're referring to? – cormullion Aug 6 at 16:07
3  
@Blackbird subtly incorrect, and likely to lead the OP to the wrong conclusion. Specifically, % is an alias for Out, %n an alias for Out[n], %% an alias for Out[-2], etc. Note, this is different from the prior computation result, which in the OPs case is the result of Solve. – rcollyer Aug 6 at 18:51
@rcollyer: well I am afraid you are right, and I have removed my comment, I should be more careful. Thanks for correcting me. – Blackbird Aug 6 at 18:55
1  
When you evaluate three 'cells' of code interactively, you can use the % shortcut to refer to the three Outputs (the cells labelled Out[1], etc.. But when you started writing a function definition, which doesn't produce lots of separate Out cells during evaluation, you found out it didn't work. Instead, use variables. I personally never use % signs, because I'm always modifying the notebook and I'd lose track of them quickly... – cormullion Aug 7 at 13:40
show 1 more comment

1 Answer

up vote 7 down vote accepted

Here's how I would write it.

  • Use Module instead of wrapping everything in brackets. The variables a,b,c,j are localized to the module
  • Just solve for expr = 0 (Solve understands that all components should equal 0)
  • Assign the output of Solve directly to {a,b,c}
  • Use If instead of Which (there is no need for both tests)
  • Use Print to display output from within the function
  • We already solved the equation, so don't do it again, just use j / .{a, b, c}
  • Map Eigenvalues over the result (that's the /@ notation)

.

Juju3[expr_] := Module[{a, b, c, j},
 {a, b, c} = Solve[expr == 0, {r, x, y}];
 If[a[[2, 2]] == 0 || a[[3, 2]] == 0, Speak["MOO"], Speak["PEACE AND LOVE"]] ;
 j = D[expr, {{r, x, y}}];
 Print["j = ", j] ;
 FullSimplify[Eigenvalues /@ (j /. {a, b, c})]]

Juju3[{i - l*r - ux*r*x - uy*r*y, -mx*x + ex*ux*r*x, -my*y + ey*uy*r*y}]

(*
j = {{-l-ux x-uy y,-r ux,-r uy},{ex ux x,-mx+ex r ux,0},{ey uy y,0,-my+ey r uy}} 

{{-l, -mx + (ex i ux)/l, -my + (ey i uy)/l}, {-((
   ex i ux + Sqrt[4 l mx^3 + ex i ux (-4 mx^2 + ex i ux)])/(
   2 mx)), (-ex i ux + Sqrt[4 l mx^3 + ex i ux (-4 mx^2 + ex i ux)])/(
  2 mx), -my + (ey mx uy)/(ex ux)}, {-mx + (ex my ux)/(ey uy), -((
   ey i uy + Sqrt[4 l my^3 + ey i uy (-4 my^2 + ey i uy)])/(
   2 my)), (-ey i uy + Sqrt[4 l my^3 + ey i uy (-4 my^2 + ey i uy)])/(
  2 my)}}
*)
share|improve this answer
Thanks ! I did not know that Solve could recognize that all components should equal one value. This is way shorter and more elegant now and it will be very helpful in the future for me :D And all the rest is very useful as well. – Ouistiti Aug 7 at 12:19
What if I don't know in advance the number of solutions of the first Solve, writing {a,b,c} makes the assumption that there will be three solutions. I guess I should write a vector with as many elements (letters) as the number of results from the Solve I used. – Ouistiti Aug 7 at 14:58
@Ouistiti, you could just store the list of solutions in a single variable, sols = Solve[...]. Then instead of a[[2,2]] you would write sols[[1,2,2]] – Simon Woods Aug 7 at 15:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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