Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have the following Mathematica code

Clear["Global`*"];
Off[Solve::"ratnz"]
V = 1/2*(ω1^2*x^2 + ω2^2*y^2) - \[Epsilon]*x^2*y^2;
rad2 = (2*h*(ω1^2 + ω2^2))/(ω1^2*ω2^2);
ω1 = 0.4; ω2 = 0.4;
\[Epsilon] = 1; 
hmin = 0.0064;
hmax = 0.03;
For[h = hmin, h <= hmax, h++,
   data = {};
   sol = Solve[{V == h, x^2 + y^2 == rad2}, {x, y}];
   xL = Abs[x /. sol[[1]]];
   yL = Abs[y /. sol[[2]]];
   P1 = {xL, yL};
   P2 = {yL, xL};
   dist = Sqrt[(xL^2 - yL^2)^2 + (yL^2 - xL^2)^2];
   AppendTo[data, {h, dist}];
]

However, even if it should be an iterative process (loop) it computes the value of dist only for the first value of h which is 0.0064. So, the list data contains only one pair of numbers. I cannot find where is the mistake in the code which prevents the loop. Moreover, how can I adjust the step of the loop, I mean the step between the successive values of h. Many thanks in advance.

share|improve this question

closed as too localized by Ajasja, Oleksandr R., Yves Klett, Sjoerd C. de Vries, rcollyer Jan 16 '13 at 15:31

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

    
hmin +1 = 1.0064 which is greater than hmax –  image_doctor Jan 16 '13 at 11:47
    
@image_doctor Right! So, how can adjust the step of the loop? Let's say to be 0.0001. –  Vaggelis_Z Jan 16 '13 at 11:50
    
try h+=0.0001 if you want to keep the For –  Pinguin Dirk Jan 16 '13 at 11:52
    
@Vaggelis_Z From the documentation, For[start,test,increment,body]. –  image_doctor Jan 16 '13 at 11:52
1  
You might want to think about uses of Map, short form /@, and Range as in myF[#]&/@Range[start,end,step]. –  image_doctor Jan 16 '13 at 12:35
show 2 more comments

1 Answer 1

up vote 1 down vote accepted
  Clear["Global`*"]; 
  Off[Solve::"ratnz"];
  V = 1/2*(\[Omega]1^2*x^2 + \[Omega]2^2*y^2) - \[Epsilon]*x^2*y^2;
  rad2 = (2*h*(\[Omega]1^2 + \[Omega]2^2))/(\[Omega]1^2*\[Omega]2^2);
  \[Omega]1 = 0.4; \[Omega]2 = 0.4;
  \[Epsilon] = 1;
  hmin = 0.0064;
  hmax = 0.03;
  increment = (hmax - hmin)/5; (* change 5 to your choice for the number of steps *)
  data = {}; (* INITIALIZE data OUTSIDE THE LOOP*)
  For[h = hmin, h <= hmax, 
  h += increment, (* instead of h++ *)
  sol = Solve[{V == h, x^2 + y^2 == rad2}, {x, y}];
  xL = Abs[x /. sol[[1]]]; yL = Abs[y /. sol[[2]]];
  (* P1 = {xL, yL}; P2 = {yL, xL};  not used anywhere in the code *)
  dist = Sqrt[(xL^2 - yL^2)^2 + (yL^2 - xL^2)^2]; 
  AppendTo[data, {h, dist}];]
  data

gives

 {{0.0064, 0.}, {0.01112, 0.256141}, {0.01584, 0.432333},
  {0.02056, 0.603251}, {0.02528, 0.772404}, {0.03, 0.940744}}
share|improve this answer
    
Thank you very much! It's working fine now. By the way the number 5 indicates the number of steps. So, if we want to include 500 points in the list then we have to use 499 as a step. Correct? –  Vaggelis_Z Jan 16 '13 at 12:03
1  
Do you need the P1 and P2 definitions? I can't see where they are subsequently used. –  Verbeia Jan 16 '13 at 12:04
    
@Vaggelis_Z, right. Thank you for the accept. –  kguler Jan 16 '13 at 12:05
    
@Verbeia In this code I do not use them. P1 and P2 are points at the circle x^2 + y^2 = rad2. Initially I wanted to measure the length of the arc defined by them as we increase the value of h, but it seems to be difficult. So, instead of this I calculate the distance between these two points. –  Vaggelis_Z Jan 16 '13 at 12:09
    
@Verbeia, you are right.. noted in update.) –  kguler Jan 16 '13 at 12:14
show 2 more comments

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