I have what I think is a basic do loop, but it is not executing as planned. Starting with some setup:

Vx = -10;   (* meters/second *)
Vz = 0;
x = 10 ;(* meters from the wire *)
z = 0;  
By[x_] := (Mu0 Current)/(2 \[Pi] x)    
Fz[x_] := q Vx By[x] ;   
Fx[x_] := -q Vz By[x] ;
deltaT = 10^-6;

Then the Do loop:

Do [   {xAcceleration := Fx[x]/Mass;
Vx := Vx + (xAcceleration deltaT);
x := x + Vx deltaT;
zAcceleration := Fz[x]/Mass;
Vz := Vz + (zAcceleration deltaT);
z := z + Vz deltaT; 
Print[Vx] (*doesn't do this *)
}, {i, 10^6}]

I think this should execute 10^6 times (or simulating 1 second). Just want the loop executed sequentially, 10^6 times. However, I get the error:

$RecursionLimit::reclim2: Recursion depth of 1024 exceeded during evaluation of Vx+xAcceleration deltaT.

Please, why is it recurring when I want it just to loop? Tried While with much the same issue. Thanks!

  • 1
    $\begingroup$ Changing all of the :='s inside the Do loop to ='s will probably do it. The construction a := a +1 always leads to an infinite recursion once you evaluate a, which is why you need to do instead a = a +1. $\endgroup$ – march Oct 31 '16 at 22:19
  • $\begingroup$ in the loop, to do what you want, you should exclusively do "Set" and never do "SetDelayed". So, change your ":=" to simply "=" $\endgroup$ – Manuel --Moe-- G Oct 31 '16 at 22:21
  • $\begingroup$ secondly, the "statements" in the Do loop do not have to be enclosed in "{ }" $\endgroup$ – Manuel --Moe-- G Oct 31 '16 at 22:22
  • $\begingroup$ are you trying to get numerical results, because actually running this code in proper form gives more and more complex expressions in terms of "Mass" and "Mu0" and "Current" and "q" and [Pi] $\endgroup$ – Manuel --Moe-- G Oct 31 '16 at 22:27
  • $\begingroup$ Thanks so much that worked: just put everything in the loop and used "set" not "setdelayed". Yes this would be a numerical calculation - so Mass would have a value, and Mu0 is a constant (free space magnetic permeability). It works :) $\endgroup$ – Jack Adams Nov 2 '16 at 2:18

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.