Mathematica How can I fix the warning for solving summation in Mathematica?

AI Thread Summary
The discussion focuses on resolving a warning in Mathematica related to summation, specifically the error message indicating that a raw object cannot be used as an iterator. The user aims to define a function that sums values based on variable inputs for m and n, but encounters issues when using the Module command. Suggestions include changing index names and ensuring that the summation variables are correctly defined to avoid using constants as iterators. Additionally, participants recommend studying Mathematica through various books to improve understanding and reduce future errors. The conversation highlights the importance of proper indexing and function definition in Mathematica programming.
kaizen.moto
Messages
94
Reaction score
0
Hi everyone,

I have these warning solving summation:
*************************************************
In[1]: f[m_, n_] := 2 n + 3 m;

In[2]: g[m_, n_] := Sum[f[m, n], {m, 1, m, 2}, {n, n, 1}];

In[3]: g[1, 1]

During evaluation of In[3]:= Sum::itraw: Raw object 1 cannot be used as an iterator. >>

During evaluation of In[3]:= Sum::itraw: Raw object 1 cannot be used as an iterator. >>

Out[3]= Sum[f[1, 1], {1, 1, 1, 2}, {1, 1, 1}]
*************************************************

The idea is that I wanted to make summation of g[m,n] with variable m, from m=1 until m = m with step 2 and keeping variable n at all cycles. For instance:

g[5,3]= f[1,3]+f[3,3]+f[5,3]

or

g[19,5]=f[1,5]+f[3,5]+f[5,5]+...+f[19,5]

Please help me to solve this problem.

Thank you.
 
Physics news on Phys.org
kaizen.moto said:
Hi everyone,

In[2]: g[m_, n_] := Sum[f[m, n], {m, 1, m, 2}, {n, n, 1}];

Sum::itraw: Raw object 1 cannot be used as an iterator. >>

The idea is that I wanted to make summation of g[m,n] with variable m, from m=1 until m = m

You want m to vary between 1 and m itself?
Is that possible?

How about changing the summation indexes names, like this

Code:
f[j_,k_]:=2 j+3k;
g[m_,n_]:=Sum[f[j,k],{j,1,m,2},{k,n,1}];

g[1,1]
   5
 
To be honest, I have already changed the indexes and I got that output already. The thing is that it did not apply the same idea when it comes to Module command. This is because I put all my codes using Module.

So I am looking for another ways to represent the summation that would work with Module command.
 
Can you perhaps show what the sums should be for m and n = 1,2,3...6?
Maybe people can just reverse engineer from what you want the sum to be to figure out what your code should look like.
That might be easier than trying to understand your code.
 
Module won't prevent you from using an indexing variable.
 
The code iam working with and given by Mathematica is as follow:
**************************************************************************
In[1]: qSolve[m_, n_]:=Module[{ggg, hhh, s, t}, ggg = 2 m + 3 n; hhh = Sum[ggg, {m, 1, s, 2}, {n, t}]];
In[2]: qSolve[5,3]

Sum::itraw: Raw object 1 cannot be used as an iterator. >>
Sum::itraw: Raw object 1 cannot be used as an iterator. >>

Out[2]: Sum[ggg$708, {1, 1, s$708, 2}, {1, t$708}]**************************************************************************


As an example, Iam expecting the output should read:
In[3]: qSolve[5,3] (meaning hhh = Sum[ggg,{m,1,5,2},{n,3}])
Out[3a]: ggg[1,3] + ggg[3,3] + ggg[5,3]
Out[3b]: 11 + 15 + 19
Out[3c]: 45

Please let me know how to solve the matter.
 
Last edited:
Perhaps something like this?

In[1]:= ggg[m_,n_]:=2*m+3*n
In[2]:= qSolve[m_,n_]:=Sum[ggg[i,n],{i,1,m,2}]
In[3]:= Trace[qSolve[5,3],Plus|ggg]
Out[23]= {{ggg[1,3],2 1+3 3,2+9,11},{ggg[3,3],2 3+3 3,6+9,15},{ggg[5,3],2 5+3 3,10+9,19},11+15+19,45}

or

In[4]:= qqSolve[m_,n_]:=Module[{},Sum[ggg[i,n],{i,1,m,2}]]
In[5]:= Trace[qqSolve[5,3],Plus|ggg]
Out[5]= {{{ggg[1,3],2 1+3 3,2+9,11},{ggg[3,3],2 3+3 3,6+9,15},{ggg[5,3],2 5+3 3,10+9,19},11+15+19,45}}

By making this MUCH more complicated and including more things that you will not understand it would be possible to provide output much closer to the step by step display that you showed. But perhaps the output of Trace[] when limited to the patterns that I included as the last argument, or not even using Trace[] and just using qSolve[5,3] alone may be sufficient for what you actually need.

Your example of the output you needed was very helpful. Thank you. It would have required dozens of incomprehensible posts trying to figure out broken code before we might have guessed what you actually wanted.

I would strongly urge you to purchase a good book teaching how to use Mathematica if that is possible for you. "Applied Mathematica: Getting Started, Getting It Done" by Shaw and Tigg is very old and can be purchased used very cheaply. If you study every sentence in that book until you understand them you can learn much of what you need for the simplest use of Mathematica. "Mathematica Navigator" is an excellent book with many simple practical examples. You need the edition of the book that matches the version of Mathematica you have, the 2nd edition is for MMA5.x, the 3rd edition is for MMA 6 and perhaps a bit of 7, no edition covers the new material in version 8. "Mathematica Cookbook" is also good. Spending 100-200 intense hours learning the basics of Mathematica programming will make you far more productive and it will be much less frustrating for you and you will need to ask far fewer questions. Mathematica is VERY different from other programming languages. After perhaps 1000 hours of intense study when you think you are beginning to really understand how Mathematica works you may then be ready to understand that it is not actually working at all like you think it is and you start working on the next deeper level of understanding.
 
The code and error message
Code:
qSolve[m_, n_]:=Module[
 {ggg, hhh, s, t},
 ggg = 2 m + 3 n; 
 hhh = Sum[ggg, {m, 1, s, 2}, {n, t}]];

[I]Sum::itraw: Raw object 1 cannot be used as an iterator. >>[/I]
[I]Sum::itraw: Raw object 1 cannot be used as an iterator. >>[/I]

Tell you that you are trying to use a number (m is a number, when you call qSolve[1,1] passing numbers to it ) as an iterator.

Basically you are trying to compute this

Sum[ 2*1+3*1,{1, 1, s, 2},{1,t}]

i.e. the sum of 5 when 1 changes from 1 to an unknown local s, step 2, and 1 going from 1 to an unknown local t, step 1.
No wonder Mathematica is complaining.
This is no module limitation.
You should get your indexes right, methinks.
 
SredniVashtar said:
You should get your indexes right, methinks.
I agree. He is trying to use a constant as an iterator, so of course it fails.
 
  • #10
Just wanted to say thank you for all the comments. I really appreciate for the type of books recommended as so far I have only managed to refer to these books, Mathematica by Example 4 by Martha Abell & James Braselton and A Beginners Guide to Mathematica by David Mc Mahon. You guys are really helpful and very expert on this program. Thanks alot.
 

Similar threads

Replies
19
Views
2K
Replies
5
Views
3K
Replies
6
Views
4K
Replies
4
Views
3K
Replies
8
Views
3K
Replies
2
Views
2K
Back
Top