Solving Mathematica Summation Code Problem

  • Context: Mathematica 
  • Thread starter Thread starter Dustinsfl
  • Start date Start date
  • Tags Tags
    Mathematica Summation
Click For Summary
SUMMARY

The forum discussion addresses a Mathematica code issue related to summation and integration. The original code fails to execute efficiently due to incorrect variable definitions for A and B, which should be defined as functions A[n_, m_] and B[n_, m_]. The solution involves correcting the loop limits from Max to Mmax and considering exact integral computations. Users emphasize the importance of strict typing in programming to avoid such errors.

PREREQUISITES
  • Familiarity with Mathematica syntax and functions
  • Understanding of summation and integration in mathematical programming
  • Knowledge of variable scoping and definitions in Mathematica
  • Experience with performance optimization in computational code
NEXT STEPS
  • Learn how to define functions in Mathematica using delayed definitions (:=)
  • Explore Mathematica's integration capabilities for exact computations
  • Research best practices for optimizing Mathematica code performance
  • Investigate the implications of variable scoping and typing in programming languages
USEFUL FOR

Mathematica users, mathematicians, and programmers seeking to optimize their code and troubleshoot integration and summation issues effectively.

Dustinsfl
Messages
2,217
Reaction score
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
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].
 
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
 
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.
 
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.
 
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_]:=
 
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.
 
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.
 
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:

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
5K