Welcome to our community

Be a part of something great, join today!

[SOLVED] mathematica summation

dwsmith

Well-known member
Feb 1, 2012
1,673
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}];
 

Ackbach

Indicium Physicus
Staff member
Jan 26, 2012
4,191
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].
 

dwsmith

Well-known member
Feb 1, 2012
1,673
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

Well-known member
Feb 1, 2012
1,673
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.
 

Ackbach

Indicium Physicus
Staff member
Jan 26, 2012
4,191
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.
 

BillSimpson

New member
Jun 4, 2012
4
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_]:=
 

dwsmith

Well-known member
Feb 1, 2012
1,673
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.
 

Ackbach

Indicium Physicus
Staff member
Jan 26, 2012
4,191
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.
 

BillSimpson

New member
Jun 4, 2012
4
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: