1
$\begingroup$

I have the following issue. I am trying to solve a 3-vector equation for the vector slx, sly, and slz as below. The vector of sl enters in a very complex manner so I was hoping to use NSolve. There is a term involving a single cross-product and another involving a double cross-product. The double cross-product term is giving me problems - e.g. I have not been able to wait long enough for a solution to pop out. The single cross-product seems to pose no problem whatsoever. I suppose the double cross-product term possesses higher orders of sl that are the issue.

Is there anyway the code below can be vastly sped up?

Off[NSolve::ratnz] (*Turn off annoying warning when using \
NSolve...can probably be addressed by using Rationalize[]*)
sl = {slx, sly, slz};
(* electron charge *) e = 1.602176565`*^-19;
(* electron mass *) me = 9.10938291`*^-31;
\[Gamma] = -0.44 e/(2 me) 10^-9/10^4 ;
bn = -5000. ;
be = -2. ;
BL = 20. ;
\[Tau]0 = 2.; \[Tau]n = 6.; k0 = 5.; kn = 10.;
G = 0.08;
sol[B_List] := NSolve[{
{0, 0, 0} == -(k0/(k0 + kn) *1/\[Tau]n + k0/(k0 + kn) *1/\[Tau]0) sl + \[Gamma] Cross[B + bn  (((B + be sl).sl) (B + be sl))/((B + be sl).(B + be sl) + 
BL^2), sl] - \[Gamma]^2 1/(k0 + kn) Cross[bn (((B + be sl).sl) (B + be sl))/((B + be sl).(B + be sl) + BL^2), Cross[sl, 
     bn (((B + be sl).sl) (B + be sl))/((B + be sl).(B + be sl) + 
       BL^2)]] + G {1, 0, 0}}, sl, Reals] // Flatten

sol[{100. Sin[0 Degree], 0, 100. Cos[0 Degree]}]

has not outputted in the time I have entered this cell.

However it returns quickly when the double cross product term is negated:

{slx -> 0.0854324, sly -> -0.153157, slz -> 0}
$\endgroup$

1 Answer 1

4
$\begingroup$

If you are not too concerned about the possibility of denominators vanishing, could remove them first. The Numerator[Together[...]] below is for that purpose. It might also be better to use exact input since Together and approximate numbers do not always play nice together. But in this case it seems to work out. Your example then runs to completion in a minute and a half or so.

sl = {slx, sly, slz};
(*electron charge*)e = 1.602176565`*^-19;
(*electron mass*)me = 9.10938291`*^-31;
\[Gamma] = -0.44 e/(2 me) 10^-9/10^4;
bn = -5000;
be = -2;
BL = 20;
\[Tau]0 = 2; \[Tau]n = 6; k0 = 5; kn = 10;
G = 8/100;
sol[B_List] := 
 NSolve[Numerator[
   Together[-(k0/(k0 + kn)*1/\[Tau]n + 
         k0/(k0 + kn)*1/\[Tau]0) sl + \[Gamma] Cross[
       B + bn (((B + be sl).sl) (B + 
              be sl))/((B + be sl).(B + be sl) + BL^2), 
       sl] - \[Gamma]^2 1/(k0 + kn) *
      Cross[bn (((B + be sl).sl) (B + 
             be sl))/((B + be sl).(B + be sl) + BL^2), 
       Cross[sl, 
        bn (((B + be sl).sl) (B + be sl))/((B + be sl).(B + be sl) + 
            BL^2)]] + G {1, 0, 0}]], sl, Reals]

Timing[sol[{100 Sin[0 Degree], 0, 100 Cos[0 Degree]}]]
{87.308000, {{slx -> 0.0854340416823, sly -> -0.153153750824,
        slz -> -2.41627183645*10^-8}, {slx -> 0.0854340416823, 
       sly -> -0.153153750824, 
       slz -> -2.41627183645*10^-8}, {slx -> 0.0854340416823, 
       sly -> -0.153153750824, slz -> -2.41627183645*10^-8}}}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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