Re: Help needed

*To*: mathgroup at smc.vnet.net*Subject*: [mg18856] Re: [mg18801] Help needed*From*: "Tomas Garza" <tgarza at mail.internet.com.mx>*Date*: Thu, 22 Jul 1999 22:57:51 -0400*Sender*: owner-wri-mathgroup at wolfram.com

Bin Liu [binliu at vt.edu] wrote: > I'm a new Mathematica (3.0) user. The program is listed below. The > problem is that the last command (For loop) gives me no output. But it > works if I input a specific number, 1 to 4, to substitute i in > "Apply[mag,{Watoms[[i]],Gatoms[[i]]}]". > > Thanks. > > Bin > > > watoms= Table[{{0,0,0},{0.5,-0.5,0.5},{0.5,0.5,0.5},{0,0,1}}] > Bao = 2.8665; > Watoms =watoms*Bao; > > gatoms=Table[ {{0,0,0},{0.5,-0.5,0.5},{0.5,0.5,0.5},{0,0,1}}] > Fao = 3.5975 > Gatoms =gatoms*Fao > > mag[{a1_,a2_,a3_},{b1_,b2_,b3_}]:=Sqrt[(a1-b1)^2+(a2-b2)^2+(a3-b3)^2] > > For[i=1,i<5,i++,Apply[mag,{Watoms[[i]],Gatoms[[i]]}]] Bin, Your procedure is OK, except that you have not instructed it what to do with the results of the calculation. Simply wrap Print around [Apply...] and you'll get your results nicely printed. Furthermore, you needn't use Apply in this case. You can instead write mag[Watoms[[i]],Gatoms[[i]]]. But I think you are not taking full advantage of the power of Mathematica. You are using procedural programming, instead of functional programming, which I strongly recommend you to learn. You could then solve your problem in an elegant, Mathematica-like way as follows: In[1]:= mgg[a_List, b_List] := Sqrt[Plus @@ (#^2 & /@ (a - b))] In[2]:= MapThread[mgg, {Watoms, Gatoms}] Out[2]= {0, 0.633065, 0.633065, 0.731} Tomas Garza Mexico City