Solving Mathematica Summation Code Problem

In summary: If there are other enhancements to error reporting that are closely related then it might be possible for me to get those included if anyone can make a strong case for the...In summary, there is a problem with the Mathematica code that causes it to get stuck running.
  • #1
Dustinsfl
2,281
5
Can anyone tell me what is the problem with this Mathematica code?
Code:
Nmax = 10;
Mmax = 10;

A = 4/Pi^2*Integrate[x*Sin[n*x]*Sin[m*y], {x, 0, Pi}, {y, 0, Pi}];
B = 4/Pi^2*Integrate[Sin[n*x]*Sin[m*y], {x, 0, Pi}, {y, 0, Pi}];

u[x_, y_, t_] = 
  Sum[Sin[n*x]*
    Sin[m*y] (A*Cos[(n^2 + m^2)*t] + 
      B/(n^2 + m^2)*Sin[(n^2 + m^2)*t]), {n, 1, Nmax}, {m, 1, Max}];
Even though Mathematica will get stuck running this code(above), the code below has no problems. What is the deal?
Code:
Nmax = 20;
Mmax = 20;
\[Lambda] = Table[n^2 + m^2, {n, 1, Nmax}, {m, 1, Mmax}];

u[x_, y_, t_] = 
  4/Pi^2*Sum[
    Sin[n*Pi/2]*Sin[m*Pi/2]/\[Lambda][[n, m]]*Sin[n*x]*Sin[m*y]*
     Sin[t*\[Lambda][[n, m]]], {n, 1, Nmax}, {m, 1, Mmax}];
 
Physics news on Phys.org
  • #2
Try changing A to A[n_,m_] and B to B[n_,m_] in their definitions, and then invoking them with A[n,m] and B[n,m].
 
  • #3
Ackbach said:
Try changing A to A[n_,m_] and B to B[n_,m_] in their definitions, and then invoking them with A[n,m] and B[n,m].

Ok so I have tried A = Table and then using A[[n,m]] as well as B.
I am trying A[n_,m_] = Table.
I have change the kernel priority to above normal. I will try if this doesn't work A[n_,m_] = expression no table
 
  • #4
dwsmith said:
Ok so I have tried A = Table and then using A[[n,m]] as well as B.
I am trying A[n_,m_] = Table.
I have change the kernel priority to above normal. I will try if this doesn't work A[n_,m_] = expression no table

Nothing seems to work in a reasonable amount of time.
 
  • #5
How about A[n_,m_]:= and B[n_,m_]:=? The colon equals gives you that delayed definition that might work out for you.

Another option would be to compute the integrals exactly (if possible), and put that expression into your u directly. Computing that many integrals might take awhile.
 
  • #6
dwsmith said:
Can anyone tell me what is the problem with this Mathematica code?
Code:
...{m, 1, Max}];

I believe should be
Code:
...{m, 1, Mmax}];

and then it works just fine even without changing A= to A[n_,m_]:=
 
  • #7
BillSimpson said:
I believe should be
Code:
...{m, 1, Mmax}];

and then it works just fine even without changing A= to A[n_,m_]:=

Good eye. Only if you knew how much time I wasted.
 
  • #8
BillSimpson said:
I believe should be
Code:
...{m, 1, Mmax}];

and then it works just fine even without changing A= to A[n_,m_]:=

Second dwsmith's comment. Excellent eye. This is why strictly typed programming languages are superior to weakly typed ones! The compiler would catch this one.
 
  • #9
dwsmith said:
Good eye. Only if you knew how much time I wasted.

Funny story.
I remember too many decades ago when I was a student and time was running out, both wall clock and my allotted number of CPU seconds, and I just could not find a bug. In absolute desperation I turned, grabbed the nearest person by the collar, aimed him at my line printer listing and said "DON'T YOU MOVE!" Then at high speed I began to explain to him exactly what my thousand lines of code was doing, line by line. About a minute and two hundred lines into the code I said "AHHH! Found It!, Get away from me!" and frantically ran to the card punch to fix the flaw so I could get the job in the run queue before the deadline.

Moral: There are things in our own code that we are simply literally blind to. Having someone else, who has to know enough that you cannot just blow them off, you must take them seriously, is an extremely powerful tool to help us spot our own "obvious" mistakes. Having someone else look at the code for a few minutes is not as good, but sometimes that is all you have.

Now on the subject of checking.
I've been told that that my repeated pleas for a "novice mode" has finally been recognized and has been submitted for actual consideration. I do not know what the outcome of that will be and there are no guarantees. But if that is approved and implemented some day then it would be possible to turn on a flag that will make the system recognize and complain about incorrect capitalization and use of system function names as user variables.

My suggestion was that the text in error be displayed in GIANT RED BLINKING letters but I doubt they will actually do that. But the itty bitty little orange "warnings" on a separate line that they currently display are simply not doing the job that needs to be done.

If there are other enhancements to error reporting that are closely related then it might be possible for me to get those included if anyone can make a strong case for the change.
 
Last edited:

FAQ: Solving Mathematica Summation Code Problem

How do I solve a summation code problem in Mathematica?

To solve a summation code problem in Mathematica, you can use the built-in function "Sum" or "Summation" to represent the summation notation. You can also use the "Solve" or "NSolve" functions to solve for the value of the summation.

What is the correct syntax for writing a summation code in Mathematica?

The correct syntax for writing a summation code in Mathematica is "Sum[expression, {variable, start, end}]" or "Summation[expression, {variable, start, end}]" where the expression is the mathematical expression inside the summation, the variable is the variable of summation, and the start and end represent the starting and ending values of the summation.

How can I specify the range of the summation in Mathematica?

To specify the range of the summation in Mathematica, you can use the "Range" function to generate a list of values that will be used as the start and end values in the summation code. You can also use the "Table" function to create a table of values and then use the "Sum" function to sum up the values in the table.

Can I use variables in my summation code in Mathematica?

Yes, you can use variables in your summation code in Mathematica. You can specify the variable of summation in the syntax and use it in the expression inside the summation. You can also use multiple variables in your summation code.

How can I check if my summation code in Mathematica is correct?

To check if your summation code in Mathematica is correct, you can use the "Sum" function with specific values for the variable, start, and end to see if the output matches your expected result. You can also use the "Table" function to create a table of values and compare it with the output of your summation code.

Similar threads

Replies
1
Views
744
Replies
1
Views
2K
Replies
1
Views
825
Replies
1
Views
2K
Replies
1
Views
1K
Replies
2
Views
473
Replies
1
Views
934
Replies
3
Views
2K
Replies
2
Views
1K
Back
Top