Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Mathematica: need help please from the experts

  1. Aug 24, 2011 #1
    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.
     
  2. jcsd
  3. Aug 24, 2011 #2

    Dale

    Staff: Mentor

    Are you sure these are correct?

    Try this:
    Code (Text):
    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}]
     
  4. Aug 24, 2011 #3
    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 gonna 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?
     
  5. Aug 24, 2011 #4

    Dale

    Staff: Mentor

    Are the expressions always linear, as in this example?

    Of course. There are always an infinite number of ways.
     
  6. Aug 25, 2011 #5
    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.
     
  7. Aug 25, 2011 #6

    Dale

    Staff: Mentor

    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

    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: Aug 25, 2011
  8. Aug 25, 2011 #7
    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...
     
  9. Aug 25, 2011 #8

    Dale

    Staff: Mentor

    Thanks, and I am still available for hire to program it. :smile:
     
  10. Aug 26, 2011 #9
    can I have your e-mail address please, if you dont mind...
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Mathematica: need help please from the experts
Loading...