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

Looking for a generic code using Mathematica

  1. Sep 17, 2011 #1
    Dear all,

    Iam looking for a generic code that could express the following functions:

    Subscript[mata, 1] = hl[[1]];
    Subscript[mata, 2] = matg[[2]].hl[[1]] + hl[[2]];
    Subscript[mata, 3] = matg[[3]].matg[[2]].hl[[1]]
    + matg[[3]].hl[[2]] + hl[[3]];
    Subscript[mata, 4] =
    matg[[4]].matg[[3]].matg[[2]].hl[[1]]
    + matg[[4]].matg[[3]].hl[[2]]
    + matg[[4]].hl[[3]]
    + hl[[4]];
    Subscript[mata, 5] = matg[[5]].matg[[4]].matg[[3]].matg[[2]].hl[[1]]
    + matg[[5]].matg[[4]].matg[[3]].hl[[2]]
    + matg[[5]].matg[[4]].hl[[3]]
    + matg[[5]].hl[[4]]
    + hl[[5]];

    Please help me to get the right code for the above expressions. It may looks like this:

    Subsript[mata,i] = ...... where i = 1,2,3,....17.

    Thank you for any kind help.
     
  2. jcsd
  3. Sep 18, 2011 #2

    Dale

    Staff: Mentor

    term[n_Integer,m_Integer]:=Product[matg[],{i,m+1,n}] hl[[m]] /;(n>=m)
    Subscript[mata,n_]:=Sum[term[n,i],{i,n}]
     
  4. Sep 19, 2011 #3
    Hm, the concept is correct and works, however, there is minor warnings that I need to fix, warnings says:

    ' Part::partd:Part specification h1[[1]] is longer than depth of object.>>'

    Thank you for your kind help
     
  5. Sep 19, 2011 #4
    If you look at your original post and Dale's attempt to help you you see that there are multiple uses of hl (HL if I were to capitalize that to make it obvious), but no uses of h1 (H followed by the digit one to make it obvious) until you tried typing back in what he attempted to show you how to do. So I would start looking there.
     
  6. Sep 19, 2011 #5
    Thank you for the comment, I found that the result of the given code shows the product rather than dot product. I mean after running the code, it gives me for instance
    In[1]:= Subscript[mata, 4]
    Out[1]:= matg[[4]] matg[[3]] matg[[2]] hl[[1]] + matg[[4]] matg[[3]] hl[[2]] + matg[[4]] hl[[3]] + hl[[4]];

    Iam trying to figure out how to make it dot product as per original code shown before.
     
  7. Sep 20, 2011 #6

    Dale

    Staff: Mentor

    term[n_Integer,m_Integer]:=Apply[Dot, Table[matg[],{i,m+1,n}]].hl[[m]] /;(n>=m)
     
  8. Sep 20, 2011 #7
    Thank you again for your help. It works now but I still can't figure out why I still got the warnings. I have tried many many ways to overcome the warnings but still failed including putting back the hl( hL) to h1 (h one).

    Iam still wondering though why did you change from h1(h one) to h1 (hL).
     
  9. Sep 20, 2011 #8
    One thing first semester students are taught in introductory programming classes, are not to name things like hl or h1 or matg or mata, but to instead use meaningful names that are not easily confused.

    Carefully reading all your posts in this particular thread seems to show that no one but you has written h1 (HOne) in any message except the ones where I was pointing out what I thought the source of your error was.

    As I've recommended to you at least once before, in the last year of your asking people to fix your problems you could easily have taken programming language classes and learned to avoid at least some of the problems that you struggle with month after month.

    If you knew no mathematics you would not stumble along asking people to do your math for you, but you seem to consider learning programming skills to be exempt from this advice.
     
  10. Sep 20, 2011 #9

    Dale

    Staff: Mentor

    Have you tested the code on a known example to determine if it is giving you the correct result? If so then don't worry about the warning, if not, then you need to figure out a test case. I cannot overemphasize the importance of thouroughly testing each module. That is, in fact, one of the big reasons for splitting your code up into smaller modules, so that you can test each piece separately and isolate problems.
     
  11. Sep 20, 2011 #10
    Dale, first of all, I wanted to say, you are a very nice person and very helpful. Iam very pleased with all your comments and advice from the beginning you helped me until now. You have given me the courage to learn to use mathematica in a such a way that makes me feel enthuastic towards learning mathematica. The way you gave me all the comments are very friendly and always make me happy to learn more and more each day. I just wish that you are awarded more medals than the two.
    Secondly, I have no idea that this forum is only suitable for advanced level knowledge of mathematica and no room for posting from novice user of mathematica.

    Thank you again for all your wonderful, fantastic guidance and commets.

    I wish we could meet one day for a cuppa of coffee!
     
  12. Sep 20, 2011 #11

    Dale

    Staff: Mentor

    Thank you, but even if he irritates you I would seriously consider Bill Simpson's advice. He is a good programmer and offers consistently good suggestions. In particular, it would be very valuable for you to take a programming class, a numerical methods class, or both. Even if it isn't specifically in Mathematica there would be a lot of language-independent programming concepts that would translate.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Looking for a generic code using Mathematica
Loading...