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

Need help to get the right codes using Mathematica

  1. Sep 1, 2011 #1
    Dear all,

    Please help me to get the right codes of sumu17[m,n] and sumv17[m,n] that should be kept inside the Do-Loop.

    You can refer to the attached file to view the code.

    I appreciate for any comments.

    Thank you.
     

    Attached Files:

  2. jcsd
  3. Sep 2, 2011 #2
    Sometimes, Kaizen, I'm convinced that you must be a machine or an alien. No human being would write code like this.
    (Hey look! http://www.sitepoint.com/forums/css-53/page-background-big-image-how-eliminate-horizontal-scrollbar-766663.html#post4906935" [Broken])

    In other words, I had a look at your notebook. It's not clear what you're asking, what you're trying to do or why you're trying to do it.

    ----

    Anyway, I think that the notebook you uploaded is broken.
    I found the sumu17 object that you're talking about at the end of your single, massive code block. There you define

    u17[m_, n_] := sub[17][[1, 1]];
    sumu17[mMAx_, n_] := Chop[Sum[u17[m, n], {m, 1, mMAx, 2}]];

    where sub[17] is some horrendous matrix product that it's not necessary to care about.
    How on earth, then, do you expect sumu17[mMAx, n] not to be anything but Floor[mMax/2] times sub[17][[1, 1]]?
     
    Last edited by a moderator: May 5, 2017
  4. Sep 2, 2011 #3
    Frankly speaking, Iam just a human being who is just about to embark into the world of programming. Being a novice user of Mathematica let alone to the world of programming language, this is the best code I could write so far. Iam pretty sure that the code can be further improved and simplifed but the problem is that I have lack of knowledge on how to do that.

    I have now attached the full version of the code that I have created, yet again, its very lengthy and lots of repetition of expressions, so that it would become more clearer on what Iam trying to do and achieve. Iam also attach other files for further explanation and descriptions.

    As Iam considered myself at the stage of crawling to learn Mathematica as a programming tool, Iam continuously trying my best to improve and simplified the code as possoble.

    My question as I asked before is that how to get the right formulations for sumu1[m,n], sumu2[m,n], .....sumu17[m,n] and sumv1[m,n],.....sumv17[m,n].

    Thank you.
     

    Attached Files:

  5. Sep 3, 2011 #4

    Dale

    Staff: Mentor

    I think you used () instead of [] for a function call. Of course, your code is a mess, so it is hard to know.

    You really need to break this up. Your flow chart is a nice start. You should modularize your code into functions that do each step of the flow chart.
     
  6. Sep 3, 2011 #5
    Thank you for your comments.
    Iam trying to break my codes now using Module eventhough I have no idea on how to do this. How can I separate the codes into modular functions in which they are inter-related to each other. In another words, I still cannot visualise the separations between them. Please let me know if you have any suggestions.
     
  7. Sep 4, 2011 #6

    Dale

    Staff: Mentor

    Your flow chart showed the process split into very clearly delineated sections. Your code should reflect this. And inside each of those you should think of a similar sub-flow-chart.
     
  8. Sep 12, 2011 #7
    I have successfully created the code separately using Module by breaking the codes into smaller parts and the results are as expected (* through hardwork and difficulties*). However, I have another new problem that it can only run up to about 25 cycles or iterations. When I try to run further, I got this warning and error messages:

    ' Results Inverse of badly conditioned matrix and Row Reduce badly conditioned matrix '.

    The other problem is that its runs very slow especially when the iterators or variables get larger and larger. Iam pretty sure that the code could be further improved but this is the best syntax that I could get so far.

    For this 25 cycles, it would take to about somewhere 1+ hours to run using my standard PC.
    My target number of cycles would be up to 2500 cycles maximum and 750 the minimum.

    Please let me know how to overcome this new problem.

    Thank you.
     
  9. Sep 12, 2011 #8

    Dale

    Staff: Mentor

    Can you post the function where the error occurs and the arguments to the function which generate the error?

    Regarding the execution time. When I am trying to speed up code the first thing that I do is actually measure the time that each part takes and determine where the bulk of the time is being spent.
     
  10. Sep 12, 2011 #9
    The errors starts from this function (simplified version):

    dispmat[variables_]:=Module[{local variables}, psimat = psimatrix[variables];..............; disp = (Inverse[mat A]).(mat B - mat C); disp]


    where mat A is 3 x 3 matrix, mat B is 3 x 1 (column vector) and mat C is also a column vector (3 x1 ). So, disp would give a 3 x 1 vector.

    All these mat A, mat B and mat C contains the main variables m and n. As I mentioned it before, after running the code beyond 25 cycles and m & n gets larger values. I have got the warnings and errors of badly inversed matrix and badly row reduce matrix and eventually the codes stopped running without giving any outputs.

    Do you have any ideas how to solve this problem as I need to run the codes further up to 2500 cycles max and 750 cycles minimum. And also Iam trying to improve the codes for computation time cost.

    Do you think that by increasing the precision would tackle this issue?

    Thank you.

    Hope this helps.
     
  11. Sep 12, 2011 #10

    Dale

    Staff: Mentor

    Can you print out the determinant of A prior to each call to Inverse[A]? I suspect that the determinant will either be 0 or very close to 0.
     
  12. Sep 12, 2011 #11
    For the matter of discussion, I run the code for iterators of {m,1,11,2},{n,1,11,2}, (meaning there are 36 cycles in total) since code had failed to give results at the stage m = 11 and n = 5, i.e (11,5).

    I encountered two warnings after running the code, first being inverse of badly conditioned matrix (IBC) and the other of Reduce of badly conditioned matrix (RBC). Both IBC and RBC happened at different stages of Module. IBC happens on Inverse matrix and RBC started at the stage of solving the simultaneous equations.

    The determinat of mat A are as follows:

    det A(1,1) = 1218.0571830493543;
    det A(1,3) = 3.25849×E7+2.30668×E-9 i;
    det A(1,5) = 5.23556×E11-0.000170191i ;
    det A(1,7) = 5.33487×E15+13.1718i ;
    det A(1,9) = 4.13566×E19-109791.i ;
    det A(1,11) = 2.65883×E23+2.08477×E8i ;
    det A(3,1) = 5.22604×E8;
    det A(3,3) = 6.19662×E10+0.0000672132i ;
    det A(3,5) = 7.29786×E13+0.301006i ;
    det A(3,7) = 1.94001×E17+46.1054i ;
    det A(3,9) = 6.80971×E20+280218i. ;
    det A(3,11) = 2.61023×E24-5.66728×E9i ;
    det A(5,1) = 7.22257×E13;
    det A(5,3) = 1.28851×E15-0.144967i ;
    det A(5,5) = 1.90658×E17-401.127i ;
    det A(5,7) = 1.02526×E20+257353.i ;
    det A(5,9) = 1.14107×E23+1.22945×E8i ;
    det A(5,11) = 1.90323×E26+1.70316×E11i ;
    det A(7,1) = 5.7285×E18;
    det A(7,3) = 4.32027×E19;
    det A(7,5) = 1.7982×E21-710529.i ;
    det A(7,7) = 2.69851×E23-8.65577×E8i ;
    det A(7,9) = 9.92617×E25-1.0085×E11i ;
    det A(7,11) = 6.68698×E28+1.88156×E13i ;

    IBC warning begins from these two iterators:
    det A(9,1) = 3.264×E23;
    det A(9,3) = 1.52529×E24;

    No IBC warnings from these iterators:
    det A(9,5) = 2.8594×E25;
    det A(9,7) = 1.70011×E27+3.9149×E12i ;
    det A(9,9) = 2.50373×E29-1.01033×E14 i;
    det A(9,11) = 7.33413×E31-4.60551×E16i ;

    IBC warnings resumed:
    det A(11,1) = 1.47702×E28;
    det A(11,3) = 5.11187×E28;
    det A(11,5) = 5.62718×E29;
    det A(11,7) = 1.71332×E31-1.76121×E16i ;
    det A(11,9) = 1.22858×E33+1.1912×E18i ;
    det A(11,11) = 1.77265×E35+7.89441×E19i ;

    RBC warnings only started when solving the simultaneous equations from the iterators of (9,11), (11,1), (11,3) and still give outputs . Only from (11,5) until (11,11) , Mathematica gives RBC warnings with no outputs were shown. Mathematica stopped running at (11,5).

    Iam assuming that eventhough there are IBC warnings displayed, code is still running and generate outputs. However, RBC warnings resulted serious problems especially when iterators get larger and eventually becomes unsolvable.

    Therefore, from these observations, I must find way to solve the RBC issues, so that I could run the code up to (99,99) (2500 cycles) if possible. Is there any other way how to solve simultaneous equations using the ‘Solve’ command? How can I treat the simultaneous equations so that it can be solvable from iterators (11,5) onwards.
     
    Last edited: Sep 12, 2011
  13. Sep 13, 2011 #12

    Dale

    Staff: Mentor

    Are those imaginary parts correct or are they a result of numerical precision loss?
     
  14. Sep 14, 2011 #13
    I have checked that those imaginary parts are not correct. When I solve the eigenvalues of a 6 x 6 matrix, I got 6 eigenvalues which contains 2 reals, 4 complex roots. The imaginary parts are not always true, they are just the result of numerical precision loss as you said.
     
  15. Sep 15, 2011 #14

    Dale

    Staff: Mentor

    Can you do one more check? I think that your procedure is numerically unstable. Can you calculate the matrix condition number of A at each point by evaluating the following:

    With[{singularValues = SingularValueList[A, Tolerance -> 0]},
    First[singularValues]/Last[singularValues]]

    Preferably at each step as you did for Det above. Also, if you can find which one has the largest condition number and print out what that matrix is, that would help.
     
  16. Sep 17, 2011 #15
    Finally, I have solved my problems, and I got the results. For now, I just need to refine my codes so that it can works faster and efficiently.

    Thank you so much for your kind help.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Need help to get the right codes using Mathematica
  1. Mathematica code help (Replies: 24)

Loading...