Mathematica: Please help me to improve the summation codes inside Do-Loop

In summary: Clear[mat1,mat2,usum1,vsum1,usum2,vsum2,totalmat];(*Define mat1, mat2, usum1, vsum1, usum2, vsum2 as functions*) mat1={{m+n},{3m+5n}}; mat2={{3m+4n},{5m+10n}}; usum1[m_,n_]:=mat1[[1,1]]; vsum1[m_,n_]:=mat1[[2,1]]; usum2[m_,n_]:=mat2[[1,1]]; vsum2
  • #1
kaizen.moto
98
0
Dear all,

I have a problem with regard to the summation which is done inside the Do-Loop iteration. I have attached the file which includes further information about the defintions, some examples and etc.

Please help me to get the right codes in order to express totalu1, totalv1, totalu2 and totalv2. Please note that all of these codes to be kept inside the Do-loop.

The code is expressed as follows:

Do[Clear[mat1,mat2,usum1,vsum1,usum2,vsum2,totalmat];mat1={{m+n},{3m+5n}};mat2={{3m+4n},{5m+10n}};usum1[m_,n_]:=mat1[[1,1]];vsum1[m_,n_]:=mat1[[2,1]];usum2[m_,n_]:=mat2[[1,1]];vsum2[m_,n_]:=mat2[[2,1]];totalu1=?;totalv1= ? ;totalu2 = ? ;totalv2 = ? ;totalmat={totalu1,totalv1,totalu2,totalv2};Print["m=",m,",","n=",n,"-->",totalmat],{m,1,5,2},{n,1,5,2}]

mat1 and mat2 are column vectors. m and n are intergers where m = 1,3,5,7,9... and n = 1,3,5,7,9,11,...

totalu1 is the summation of usum1 with respect to ' m' term only and must begins with m = 1 (i.e. n is fixed). For example, usum1[5,3]: totalu1 = usum1[1,3]+ usum1[3,3] + usum11[5,3]

totalv1 is the summation of vsum1 with respect to ' n' term only and must begins with n = 1 (i.e. m is fixed). For example, vsum1[3,3]: totalu1 = usum1[3,1]+ usum1[3,3]

same as above, totalu2 is the summation of usum2 with respect to ' m' term only and must begins with m = 1 (i.e. n is fixed). For example, usum2[1,3]: totalu2 = usum2[1,3] only

same as above, totalv2 is the summation of vsum2 with respect to 'n' term only and must begins with n = 1 (i.e. m is fixed). For example, vsum2[1,3]: totalv2 = vsum2[1,1]+ vsum2[1,3] .

For examples: when m = 1 and n = 5,
totalu1[m, n] or totalu1[1, 5] = usum1[1, 5] = 6

when m = 5 and n = 5,
totalu1[m, n] or totalu1[5, 5] = usum1[1, 5] + usum1[3, 5] + usum1[5, 5] = 6 + 8 + 10 = 24

when m = 3 and n = 5,
totalv1[m, n] or totalv1[3, 5] = vsum1[3, 1] + vsum1[3, 3] + vsum1[3, 5] = 14 + 24 + 34 = 72

when m = 5 and n = 1,
totalu2[m, n] or totalu2[5, 1] = usum2[1, 1] + usum2[3, 1] + usum2[5, 1] = 7 + 13 + 19 = 39

when m = 1 and n = 5,
totalv2[m, n] or totalv2[1, 5] = vsum2[1, 1] + vsum2[1, 3] + vsum2[1, 5] = 15 + 35 + 55 = 105

when m = 5 and n = 1, totalv2[5, 1] = vsum2[5, 1] = 35

Please note that all of these should be done inside the Do-Loop.

Any kind help is much appreciated.

Thank you.
 

Attachments

  • DoLoopSummation.nb
    16.4 KB · Views: 462
Last edited:
Physics news on Phys.org
  • #2
kaizen.moto said:
Please note that all of these codes to be kept inside the Do-loop.
The first thing to do is to take them out of the Do loop and test them for a fixed m and n, so that you can see the output at every step of the way. Once you have it working for a fixed m and n then you can pack it back into the Do loop, but until you have fixed the code inside the act of packing it in the Do loop makes troubleshooting difficult.

Second, the various total variables are undefined, so you need to define them. From your descriptions they should be functions which are merely evaluated inside the Do loop, but defined outside.

Third, the various sum functions do not depend on m and n, so those should probably be variables rather than functions.

Fourth, the mat variables do depend on m and n, so they should probably be functions.

In short, pull everything outside of the Do loop for troubleshooting. Make sure that you are using variables and functions appropriately. Write the missing pieces. Test for fixed m and n. Then re-pack into the Do loop.
 
  • #3
One other thing. Usually, if you are trying to Clear a bunch of variables at the beginning of each iteration of a loop that is a strong indication that the variables are effectively local variables which should be contained inside a With or a Module statement. Your Do loop should probably consist of a single call to a function defined with a Module.
 
  • #4
Thank you for the advise.
I got it. I have solved my problem based on your first advise. However, I wanted to get the code as per your second advise, i.e. to use Module.

Could you please help me how to rectify the code from Do-loop to Module form:

For example:
(*Do Loop*)
Do[Clear[mat1,mat2,usum1,vsum1,usum2,vsum2,totalmat];mat1={{m+n},{3m+5n}};mat2={{3m+4n},{5m+10n}};usum1[m_,n_]:=mat1[[1,1]];vsum1[m_,n_]:=mat1[[2,1]];usum2[m_,n_]:=mat2[[1,1]];vsum2[m_,n_]:=mat2[[2,1]];totalu1[m_,n_]:=Total[Table[usum1[m,n][[1]],{m,1,m,2}]];totalv1[m_,n_]:=Total[Table[vsum1[m,n][[1]],{n,1,n,2}]];totalu2[m_,n_]:=Total[Table[usum2[m,n][[1]],{m,1,m,2}]];totalv2[m_,n_]:=Total[Table[vsum2[m,n][[1]],{n,1,n,2}]];totalmat={totalu1,totalv1,totalu2,totalv2};Print["m=",m,",","n=",n,"-->",totalmat],{m,1,5,2},{n,1,5,2}]

(*Module*)
qsolve[m_,n_]:=Module[{mat1,mat2,usum1,vsum1,usum2,vsum2,totalmat},mat1={{m+n},{3m+5n}};mat2={{3m+4n},{5m+10n}};usum1[m_,n_]:=mat1[[1,1]];vsum1[m_,n_]:=mat1[[2,1]];usum2[m_,n_]:=mat2[[1,1]];vsum2[m_,n_]:=mat2[[2,1]];totalu1[m_,n_]:=Total[Table[usum1[m,n][[1]],{m,1,m,2}]];totalv1[m_,n_]:=Total[Table[vsum1[m,n][[1]],{n,1,n,2}]];totalu2[m_,n_]:=Total[Table[usum2[m,n][[1]],{m,1,m,2}]];totalv2[m_,n_]:=Total[Table[vsum2[m,n][[1]],{n,1,n,2}]];totalmat={totalu1,totalv1,totalu2,totalv2}]

Sum[qsolve[m,n],{m,1,5,2},{n,1,5,2}]
 
  • #5
I tried your Do loop code but it didn't work, so I can't really make specific suggestions. However I did notice one clear thing. You are defining several functions inside the module. That is not very usual and, because you are using the same variable names, it is causing some conflicts. You can fix that simply by using different letters for the variables in your local function definitions.
 
  • #6
Try this Do-Loop:

Do[Clear[u1, v1, u2, v2, totalu1, totalv1, totalu2, totalv2,
totalmat]; u1[m_, n_] := m + n;
v1[m_, n_] := 3 m + 5 n;
u2[m_, n_] := 3 m + 4 n;
v2[m_, n_] := 5 m + 10 n; uu1[m, n] = Table[u1[i, n], {i, 1, m, 2}];
totalu1 = Simplify[Total[Table[uu1[m, n][[1]], {m, 1, m, 2}]]];
vv1[m, n] = Table[v1[m, jjj], {jjj, 1, n, 2}];
totalv1 = Simplify[ Total[Table[vv1[m, n][[1]], {n, 1, n, 2}]]];
uu2[m, n] = Table[u2[i, n], {i, 1, m, 2}];
totalu2 = Simplify[Total[Table[uu2[m, n][[1]], {m, 1, m, 2}]]];
vv2[m, n] = Table[v2[m, jjj], {jjj, 1, n, 2}];
totalv2 = Simplify[Total[Table[vv2[m, n][[1]], {n, 1, n, 2}]]];
totalmat = {totalu1, totalv1, totalu2, totalv2};
Print["m=", m, ",", "n=", n, "-->", totalmat], {m, 1, 5, 2}, {n, 1,
5, 2}]

Please help me to transform the above using Module statement. Thank you.
 
  • #7
First you define your module:

Code:
f[m_, n_] :=
 Module[{u1, v1, u2, v2, totalu1, totalv1, totalu2, totalv2, totalmat},
  u1[a_, b_] := a + b;
  v1[a_, b_] := 3 a + 5 b;
  u2[a_, b_] := 3 a + 4 b;
  v2[a_, b_] := 5 a + 10 b;
  uu1[m, n] = Table[u1[i, n], {i, 1, m, 2}];
  totalu1 = Simplify[Total[Table[uu1[i, n][[1]], {i, 1, m, 2}]]];
  vv1[m, n] = Table[v1[m, jjj], {jjj, 1, n, 2}];
  totalv1 = Simplify[Total[Table[vv1[m, i][[1]], {i, 1, n, 2}]]];
  uu2[m, n] = Table[u2[i, n], {i, 1, m, 2}];
  totalu2 = Simplify[Total[Table[uu2[i, n][[1]], {i, 1, m, 2}]]];
  vv2[m, n] = Table[v2[m, jjj], {jjj, 1, n, 2}];
  totalv2 = Simplify[Total[Table[vv2[m, i][[1]], {i, 1, n, 2}]]];
  totalmat = {totalu1, totalv1, totalu2, totalv2}
  ]

Then you define your loop.

Code:
Do[
 Print["m=", m, ",", "n=", n, "-->", f[m, n]], {m, 1, 5, 2}, {n, 1, 5,
   2}]

I would even recommend breaking it up some more. The module is a pretty ugly module that is probably pretty hard for you to troubleshoot. It looks like it should be split up into several smaller modules that do each step of the process individually and would be individually easier to troubleshoot.
 
  • #8
Thank you, yes, its working now using Module for the above case. However, it did not work for another case as shown below:

f[m_, n_] :=
Module[{u1, v1, u2, v2, totalu1, totalv1, totalu2, totalv2,
totalmat}, u1[a_, b_] := a + b;
v1[a_, b_] := 3 a + 5 b;
u2[a_, b_] := 3 a + 4 b;
v2[a_, b_] := 5 a + 10 b;
totalu1[mMAx_, n] := Sum[u1[m, n], {m, 1, mMAx, 2}];
totalv1[m, nMax_] := Sum[v1[m, n], {n, 1, nMax, 2}];
totalu2[mMAx_, n] := Sum[u2[m, n], {m, 1, mMAx, 2}];
totalv2[m, nMax_] := Sum[v2[m, n], {n, 1, nMax, 2}];
totalmat = {totalu1[m, n], totalv1[m, n], totalu2[m, n],
totalv2[m, n]}]

Do[Print["m=", m, ",", "n=", n, "-->", f[m, n]], {m, 1, 3, 2}, {n, 1,
3, 2}]

Could you please let me know how to fix this case?

Thanks.
 
  • #9
Consider the call f[3,5]. Inside your module this expression:

Sum[u1[m, n], {m, 1, mMAx, 2}]

turns into this

Sum[u1[3,5],{3,1,mMAx,2}]

Can you spot the problem with this expression? I will give you a hint, the error message will be:
Sum::itraw: Raw object 3 cannot be used as an iterator.

And the link to the help documentation will give you:
http://reference.wolfram.com/mathematica/ref/message/General/itraw.html
 
  • #10
Sorry...i have deleted my previous message simply because finally I got the right code already using Module. it's finally working.
Thanks for your kind help.
 
  • #11
You're welcome!
 

1. What is Mathematica and how is it used in scientific research?

Mathematica is a powerful software program used by scientists to perform complex mathematical and statistical calculations, create interactive visualizations, and develop algorithms for data analysis. It is commonly used in various fields of science, including physics, chemistry, biology, and engineering.

2. What is a Do-Loop in Mathematica and how does it work?

A Do-Loop is a programming structure in Mathematica that allows for repeated execution of a set of commands. It consists of a loop variable, a starting value, an ending value, and a step size. The loop variable is incremented by the step size until it reaches the ending value, and the commands within the loop are executed each time the loop variable changes.

3. How can I improve the efficiency of my summation codes inside a Do-Loop in Mathematica?

There are several ways to improve the efficiency of summation codes inside a Do-Loop in Mathematica, including using built-in functions like Sum or Total instead of explicitly writing out the summation, using vectorized operations, and avoiding unnecessary calculations. It is also helpful to reduce the number of iterations in the loop if possible.

4. Can I perform summations over multiple variables in a single Do-Loop?

Yes, you can perform summations over multiple variables in a single Do-Loop in Mathematica by using nested loops. Each loop can have its own loop variable and range of values, allowing you to sum over multiple variables simultaneously.

5. Are there any resources available to help me improve my Mathematica coding skills?

Yes, there are various resources available to help you improve your Mathematica coding skills, including online tutorials, documentation, and forums where you can ask questions and learn from other users. The official Mathematica website also offers training courses and workshops for users at all levels of expertise.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
9K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Precalculus Mathematics Homework Help
Replies
6
Views
3K
Back
Top