Mathematica Help Needed: Error Message With "For" Loop

In summary: If you just can't get your complete problem small enough then you can attach a notebook to a posting.But you still want simplicity without leaving out essential information and you want something that someone who has no idea what you are doing or thinking is going to be able to understand, diagnose and quickly provide you a solution.(*n+1 is the number of eigenvalues to be generated, k is the exponent in the potential term*,)**n=3;**k=4;**m=1;**g=.2;*\[Omega]=1;Sp = Range[n];e = Range[n];e
  • #1
the_kid
116
0
Hey all,

I'm very new to Mathematica, so I'm looking for some help here. I'm writing an elementary program using a "For" loop, and getting this error message: "Sum::write: Tag Plus in i-1 is Protected. >>" Any ideas what might be wrong?

Thanks!
 
Physics news on Phys.org
  • #2
Usually it means you have an expression like this:
i-1=5

Perhaps you meant something like:
i-1==5
 
  • #3
Hmm, I tried fixing that, but I had no luck. Any chance you could take a look at my code?
 
  • #4
the_kid said:
Any chance you could take a look at my code?

Simplify your notebook as much as you can to demonstrate just what you are having a problem with, not include irrelevant detail, but include everything so that someone else can reproduce exactly the problem you have.

If what is left is small enough you can select a cell and then click Edit, Copy As, Plain Text. Then paste into a message here. What you are trying to accomplish is to post something that someone can glance at, scrape off the screen, paste into their Mathematica and reproduce your problem exactly. You are trying to avoid introduce problems by you typing it back in or forcing someone who is trying to help you to have to type it back in. And you want to avoid an assortment of problems with "special characters" when copying between Mathematica and the forum. If, after you post, you can scrape off the forum, paste into a fresh Mathematica session and see the exact same problem then you are probably getting close.

If you just can't get your complete problem small enough then you can attach a notebook to a posting.

But you still want simplicity without leaving out essential information and you want something that someone who has no idea what you are doing or thinking is going to be able to understand, diagnose and quickly provide you a solution.
 
  • #5
(*n+1 is the number of eigenvalues to be generated, k is the exponent in the potential term*)
n=3;
k=4;
m=1;
g=.2;
\[Omega]=1;

Sp = Range[n];
e = Range[n];
e[[1]] = \[Omega];

f[x_] = Sqrt[m^2 \[Omega]^2 x^2 + 2 m g x^(2 k)];
Sp[[1]] = f[x];
Sp[[3]];
For[i = 2, i <= n, i++,
Sp[] =
1/Sp[[1]] (-i m e + i/2 D[Sp[[i - 1]], 1] -
1/2 Sum[i!/(k! (i - k)!) Sp[[k]] Sp[[i - k]], k, 2, i - 1]);
e[] =
Limit[(1/(
i m)) ((i/2) D[Sp[[i - 1]], 1] -
1/2 Sum[i!/(k! (i - k)!) Sp[[k]] Sp[[i - k]], k, 2, i - 1]), x,
0]]
 
  • #6
Ok, I copied that, and it looks pretty good. The last few lines are formated differently when I copy it back in, although they are still entirely correct. If anyone could help me out with this, I'd be very appreciative.
 
  • #7
1. Your
Sp[[3]];
on a line all by itself isn't going to do anything. It isn't clear why that line is in there.

2. Your
D[Sp[[i-1]],1]
is going to differentiate Sp[[i-1]] with respect to the variable 1, but 1 isn't a variable and Mathematica complains about that.
Check
http://reference.wolfram.com/mathematica/ref/D.html

3. Your Sum[...,k,2,i-1]
isn't correct Mathematica syntax. Perhaps that should be
Sum[...,{k,2,i-1}]
Check
http://reference.wolfram.com/mathematica/ref/Sum.html

4. Your
Limit[...,x,0]
isn't correct Mathematica syntax. Perhaps that should be
Limit[...,x->0]
Check
http://reference.wolfram.com/mathematica/ref/Limit.html

Fix each of those problems, see if you are done, and if not repeat the process.

You do get points for making a well formed posting that could be scraped right back into Mathematica to be able to diagnose your problem and give you help.
 
Last edited:
  • #8
I really appreciate the help. The program is running. *fingers crossed*
 
  • #9
I'm trying to compute a very low value for n: n=3, but the program just keeps saying it's running. I suspect it shouldn't be taking this long. Could I have hit an infinite loop or something of that sort? Any ideas?
 
  • #10
As I wrote:

Bill Simpson said:
Simplify your notebook as much as you can to demonstrate just what you are having a problem with, not include irrelevant detail, but include everything so that someone else can reproduce exactly the problem you have.
 
  • #11
(*n+1 is the number of eigenvalues to be generated, k is the exponent \
in the potential term*)
n = 2;
k = 4;
m = 1;
g = .2;
\[Omega] = 1;

Sp = Range[n];
e = Range[n];
e[[1]] = \[Omega]

f[x] = Sqrt[m^2 \[Omega]^2 x^2 + 2 m g x^(2 k)];
Sp[[1]] = f[x]

For[i = 2, i <= n, i++,
Sp[] =
1/Sp[[1]] (-i m e + i/2 D[Sp[[i - 1]], x] -
1/2 Sum[i!/(k! (i - k)!) Sp[[k]] Sp[[i - k]], {k, 2, i - 1}]);
e[] =
Limit[(1/(
i m)) ((i/2) D[Sp[[i - 1]], x] -
1/2 Sum[i!/(k! (i - k)!) Sp[[k]] Sp[[i - k]], {k, 2, i - 1}]),
x -> 0]]
 
  • #12
1. You probably want a semicolon after
e[[1]] = \[Omega]
You lost that somehow in making changes

2. You probably want
1/Sp[[1]] (-i m e...
to be
1/Sp[[1]] (-i m e[]...

If I make those changes it appears that your code runs almost instantly.

But it doesn't display any results and I have no idea what it is supposed to do.

So repeat the steps you have done above
 
  • #13
(*n+1 is the number of eigenvalues to be generated, k is the exponent \
in the potential term*)
n = 2;
k = 4;
m = 1;
g = .2;
\[Omega] = 1;

Sp = Range[n];
e = Range[n];
e[[1]] = \[Omega];

f[x] = Sqrt[m^2 \[Omega]^2 x^2 + 2 m g x^(2 k)];
Sp[[1]] = f[x]

For[i = 2, i <= n, i++,
Sp[] =
1/Sp[[1]] (-i m e[] + i/2 D[Sp[[i - 1]], x] -
1/2 Sum[i!/(k! (i - k)!) Sp[[k]] Sp[[i - k]], {k, 2, i - 1}]);
e[] =
Limit[(1/(
i m)) ((i/2) D[Sp[[i - 1]], x] -
1/2 Sum[i!/(k! (i - k)!) Sp[[k]] Sp[[i - k]], {k, 2, i - 1}]),
x -> 0]]

----------------------------
I made those changes, as seen above, and the code runs for me. What I was attempting to have it do was produce these eigenvalues from the equations in the for-loop. At the beginning, I have a place to enter the number of eigenvalues to be generated (these are quantum energy coefficients), and I started with a simple one, n=2. Any idea how I can make it spit these out for me?
 
  • #14
the_kid said:
What I was attempting to have it do was produce these eigenvalues from the equations in the for-loop. At the beginning, I have a place to enter the number of eigenvalues to be generated (these are quantum energy coefficients), and I started with a simple one, n=2. Any idea how I can make it spit these out for me?

Do you want it to Print something after the For[] has completed?
Do you want it to Print something after each calculation in the For[]?

Can you put a Print[]; at an appropriate spot in the code and put the expression you want to see the value of inside the Print[]?
 
  • #15
Bill Simpson said:
Do you want it to Print something after the For[] has completed?
Do you want it to Print something after each calculation in the For[]?

Can you put a Print[]; at an appropriate spot in the code and put the expression you want to see the value of inside the Print[]?

I want it to print something after each iteration of the For[]. How would I do that?
 
  • #16
For[i = 2, i <= n, i++,
Sp[] = 1/Sp[[1]] (-i m e[] + i/2 D[Sp[[i - 1]], x] - 1/2 Sum[i!/(k! (i - k)!) Sp[[k]] Sp[[i - k]], {k, 2, i - 1}]);
e[] = Limit[(1/(i m)) ((i/2) D[Sp[[i - 1]], x] - 1/2 Sum[i!/(k! (i - k)!) Sp[[k]] Sp[[i - k]], {k, 2, i - 1}]), x -> 0];
Print["something"];
]
 
  • #17
Hmm, okay. I want it to print Sp[1], Sp[2],..., Sp[n] and e[1], e[2],..., e[n]. Any idea what I would call that?
 
  • #18
For[i = 2, i <= n, i++,
Sp[] = 1/Sp[[1]] (-i m e[] + i/2 D[Sp[[i - 1]], x] - 1/2 Sum[i!/(k! (i - k)!) Sp[[k]] Sp[[i - k]], {k, 2, i - 1}]);
e[] = Limit[(1/(i m)) ((i/2) D[Sp[[i - 1]], x] - 1/2 Sum[i!/(k! (i - k)!) Sp[[k]] Sp[[i - k]], {k, 2, i - 1}]), x -> 0];
Print[Sp];
Print[e];
]
 
  • #19
Bill Simpson said:
For[i = 2, i <= n, i++,
Sp[] = 1/Sp[[1]] (-i m e[] + i/2 D[Sp[[i - 1]], x] - 1/2 Sum[i!/(k! (i - k)!) Sp[[k]] Sp[[i - k]], {k, 2, i - 1}]);
e[] = Limit[(1/(i m)) ((i/2) D[Sp[[i - 1]], x] - 1/2 Sum[i!/(k! (i - k)!) Sp[[k]] Sp[[i - k]], {k, 2, i - 1}]), x -> 0];
Print[Sp];
Print[e];
]


Hmm, I tried this and it isn't printing anything for me.

By the way, thank you SO much for all this help; it is incredibly instructive. I appreciate it.
 
  • #20
In[1]:=(*n+1 is the number of eigenvalues to be generated, k is the exponent in the potential term*)
n=2;k=4;m=1;g=.2;\[Omega]=1;
Sp=Range[n];
e=Range[n];
e[[1]]=\[Omega];
f[x]=Sqrt[m^2 \[Omega]^2 x^2+2 m g x^(2 k)];
Sp[[1]]=f[x];
For[i=2,i<=n,i++,
Sp[[i]]=1/Sp[[1]] (-i m e[]+i/2 D[Sp[[i-1]],x]-1/2 Sum[i!/(k! (i-k)!) Sp[[k]] Sp[[i-k]],{k,2,i-1}]);
e[]=Limit[(1/(i m)) ((i/2) D[Sp[[i-1]],x]-1/2 Sum[i!/(k! (i-k)!) Sp[[k]] Sp[[i-k]],{k,2,i-1}]),x->0];
Print["Sp=",InputForm[Sp]];
Print["e=",InputForm[e]];
]

From In[1]:=Sp={Sqrt[x^2+0.4*x^8], (-4+(2*x+3.2*x^7)/(2*Sqrt[x^2+0.4*x^8]))/Sqrt[x^2+0.4*x^8]}

From In[1]:=e={1, 0.5}

Note: I just added the labels and InputForm to the Print so you could tell which was which and to get rid of all the 2D formatting so I could paste the results for you here.
 
Last edited:
  • #21
That looks great, though I can't get it to show up for me. I literally copy and pasted what you have and it's not printing those values for me. Any ideas?
 
  • #22
Ah, I got it! One final question, if you have time: is there any way I could change the program so the input values (g, w, etc.) could be keep as "variables"? I would like to eliminate the numerical aspect, if possible.

Again, thank you so much!
 
  • #23
In[1]:=(*n+1 is the number of eigenvalues to be generated, k is the exponent in the potential term*)
n=2;k=4;Sp=Range[n];e=Range[n];e[[1]]=\[Omega];
f[x]=Sqrt[m^2 \[Omega]^2 x^2+2 m g x^(2 k)];
Sp[[1]]=f[x];
For[i=2,i<=n,i++,
Sp[[i]]=1/Sp[[1]] (-i m e[]+i/2 D[Sp[[i-1]],x]-1/2 Sum[i!/(k! (i-k)!) Sp[[k]] Sp[[i-k]],{k,2,i-1}]);
e[]=Limit[(1/(i m)) ((i/2) D[Sp[[i-1]],x]-1/2 Sum[i!/(k! (i-k)!) Sp[[k]] Sp[[i-k]],{k,2,i-1}]),x->0];
Print["Sp=",InputForm[Sp]];
Print["e=",InputForm[e]];
]

From In[1]:=Sp={Sqrt[2*g*m*x^8+m^2*x^2*\[Omega]^2], (-4*m+(16*g*m*x^7 + 2*m^2*x*\[Omega]^2)/(2*Sqrt[2*g*m*x^8 + m^2*x^2*\[Omega]^2]))/ Sqrt[2*g*m*x^8 + m^2*x^2*\[Omega]^2]}

From In[1]:=e={\[Omega], Sqrt[m^2*\[Omega]^2]/(2*m)}

n and k must have constant integer values assigned for For and Sum.
 
  • #24
Thank you!
 

1. What does the error message "For" loop mean?

The error message "For" loop typically indicates that there is an issue with the syntax or structure of a "for" loop in your Mathematica code. This could be due to a missing or incorrect variable assignment, an improper use of the "for" loop syntax, or a mistake in the loop's condition or iteration.

2. How can I fix the error message "For" loop?

To fix the error message "For" loop, you will need to carefully review the syntax and structure of your "for" loop in Mathematica. Make sure that all variables are properly assigned and that the loop is written in the correct format. You may also need to check the loop's condition and iteration to ensure they are functioning as intended.

3. Can I use a "For" loop in Mathematica to iterate through a list?

Yes, you can use a "for" loop in Mathematica to iterate through a list. You will need to use the Length function to determine the length of the list and then use the loop index as the index for accessing each element in the list.

4. How do I know which line of code is causing the "For" loop error message?

To determine which line of code is causing the "For" loop error message, you can add Print statements within the loop to output the values of variables and see where the error occurs. You can also use the Trace function to track the evaluation of your code and identify the specific line where the error occurs.

5. Is there an alternative to using a "For" loop in Mathematica?

Yes, there are alternative methods for iteration in Mathematica, such as using the Do function or using built-in functions like Map or Table. These methods may be more efficient and easier to use in certain situations, so it is worth exploring them as alternatives to "for" loops.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
407
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
23
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
Back
Top