Mathematica Mathematica: please from the experts

  • Thread starter Thread starter kaizen.moto
  • Start date Start date
  • Tags Tags
    Mathematica
AI Thread Summary
The discussion revolves around troubleshooting a Mathematica function designed to compute sums based on expressions involving variables m and n. The original code provided by the user was not yielding the expected results for the sums of u1, u2, v1, and v2. A suggestion was made to simplify the expressions by using matrices, which successfully worked for a simpler case. However, the user expressed difficulty in applying this method to their more complex, nonlinear expressions, which involve high-degree polynomials. The conversation highlighted the importance of avoiding variable conflicts within the function definitions and suggested using different variable names to prevent issues. Ultimately, the user managed to resolve their coding issues with guidance, leading to a functional solution. The discussion emphasized the challenges of working with complex mathematical expressions in programming and the collaborative effort to find a solution.
kaizen.moto
Messages
94
Reaction score
0
Dear all,

Please help me to fix this problem:

f[m_, n_] :=
Module[{u1, u2, v1, v2, sumu1, sumu2, sumv1, sumv2}, u1 = 3 m + 2 n;
u2 = 2 m - 5 n; v1 = 4 m - 3 n; v2 = 6 m + 3 n;
sumu1 = Sum[u1[i, n], {i, 1, m, 2}];
sumu2 = Sum[u2[i, n], {i, 1, m, 2}];
sumv1 = Sum[v1[m, j], {j, 1, n, 2}];
sumv2 = Sum[v2[m, j], {j, 1, n, 2}] {sumu1, sumu2, sumv1, sumv2}];

Do[f[m, n]; Print[f[m, n]], {m, 1, 3, 2}, {n, 1, 3, 2}]

Definitions: the idea is that sumu1(1,1) = u1(1,1); sumu1(1,3) = u1(1,3); sumu1(3,1) = u1(1,1) + u1(3,1); sumu1(3,3) = u1(1,3) + u1(3,3)

and for sumu2(1,1) = u2(1,1); sumu2(1,3) = u2(1,3); sumu2(3,1) = u2(1,1) + u2(3,1); sumu2(3,3) = u2(1,3) + u2(3,3)

However for sumv1(1,1) = v1(1,1); sumv1(1,3) = v1(1,1) + v1(1,3); sumv1(3,1) = v1(3,1) ; sumv1(3,3) = v1(3,1) + v1(3,3)

and similarly for sumv2(1,1) = v2(1,1); sumv2(1,3) = v2(1,1) + v2(1,3); sumv2(3,1) = v2(3,1) ; sumv2(3,3) = v2(3,1) + v2(3,3)


The correct answers I should be getting from the Do-loop are:

f(1,1) = { 5, -3 , 1 , 9}
f(1,3) = { 9 , -13 , -4 , 24 }
f(3,1) = {15 , -2 , 11 , 19 }
f(3,3) = { 24 , -22 , -2 , 42 }

Thank you for any kind help.
 
Physics news on Phys.org
kaizen.moto said:
The correct answers I should be getting from the Do-loop are:

f(1,1) = { 5, -3 , 1 , 9}
f(1,3) = { 9 , -13 , -4 , 24 }
f(3,1) = {15 , -2 , 11 , 19 }
f(3,3) = { 24 , -22 , -2 , 42 }
Are you sure these are correct?

Try this:
Code:
u = {{3, 2}, {2, -5}};
v = {{4, -3}, {6, 3}};
f[m_, n_] :=
 Module[{sumu, sumv},
  sumu = Sum[u.{i, n}, {i, 1, m, 2}];
  sumv = Sum[v.{m, j}, {j, 1, n, 2}];
  Flatten[{sumu, sumv}]
];
Do[Print[f[m, n]], {m, 1, 3, 2}, {n, 1, 3, 2}]
 
Apologise for my mistakes. You are absolutely right. Your revised code is working perfectly for the above simplified exercise.

However, due to the real application, working your code with my case, its not practical. One of the reasons, my actual expression for u1, u1, v1 and v2 consisting of variables m and n (and also some other constants) are quite lengthy. Its going to be tedious to rerrange them collectively in a single expression (such as u={{3,2},{2,-5}).

I still prefer to express u and v in a separate terms, i.e. u1, u2, v1 and v2 as per my original version.

I have tried to use your suggested code but it gives funny looking outputs.

f[m_, n_] :=
Module[{u1, u2, v1, v2, sumu1, sumv1, sumu2, sumv2}, u1 = 3 m + 2 n;
u2 = 2 m - 5 n; v1 = 4 m - 3 n; v2 = 6 m + 3 n;
sumu1 = Sum[u1.{i, n}, {i, 1, m, 2}];
sumv1 = Sum[v1.{m, j}, {j, 1, n, 2}];
sumu2 = Sum[u2.{i, n}, {i, 1, m, 2}];
sumv2 = Sum[v2.{m, j}, {j, 1, n, 2}];
Flatten[{sumu1, sumu2, sumv1, sumv2}]];
Do[Print[f[m, n]], {m, 1, 3, 2}, {n, 1, 3, 2}].

Is there any other way, to get the right code?
 
kaizen.moto said:
However, due to the real application, working your code with my case, its not practical. One of the reasons, my actual expression for u1, u1, v1 and v2 consisting of variables m and n (and also some other constants) are quite lengthy.
Are the expressions always linear, as in this example?

kaizen.moto said:
Is there any other way, to get the right code?
Of course. There are always an infinite number of ways.
 
all the expressions of u1, u2..., v1, v2,... consists of non linear equation. In fact, some of them have a high very degree of polynomial expressions. Actually, I was trying to get a single expression of u or v in terms of other variables including m, n and other constants from Mathematica, but it took me ages to get this expression. The program is still running for many hours to get u's and v's. From this fact, that u's and v's are pretty much high degree polynomial of m and n variables.

In the meantime, Iam tyring to express u's and v's interms of a function such as u1[m_,n_]:=...v1[m_,n_]:=...inside the Module.

I hope it could be done.
 
kaizen.moto said:
all the expressions of u1, u2..., v1, v2,... consists of non linear equation. In fact, some of them have a high very degree of polynomial expressions.
I am a little confusesd by this response. Polynomials of any order are linear. The sum of two polynomials of order k is a polynomial of order k, and the product of a scalar and a polynomial of order k is a polynomial of order k.

The reason that I ask is that if you can cast your problem in terms of a system of linear equations then you open the door to the use of some very powerful pre-packaged computer techniques:

http://en.wikipedia.org/wiki/System_of_linear_equations

kaizen.moto said:
In the meantime, Iam tyring to express u's and v's interms of a function such as u1[m_,n_]:=...v1[m_,n_]:=...inside the Module.

I hope it could be done.
Certainly, it can be done. You just cannot use m and n as your variables since those are already being used by f. Use, e.g. u1[a_,b_]:=... instead. Alternative you can change f so that it doesn't use m and n, the point is that you need to avoid the conflict between the two.
 
Last edited:
I've got it finally, the code is working now with Module.

Again, thank you so much for your relentless support and guidance. You are a very nice person...you deserve more medals...
 
Thanks, and I am still available for hire to program it. :smile:
 
can I have your e-mail address please, if you don't mind...
 
Back
Top