Mathematica Looking for a generic code using Mathematica

  • Thread starter Thread starter kaizen.moto
  • Start date Start date
  • Tags Tags
    Code Mathematica
AI Thread Summary
A user is seeking help to create a generic Mathematica code for a series of mathematical expressions involving dot products and indexed variables. They encounter warnings related to variable naming and structure, specifically regarding the use of hl and h1. Despite receiving guidance, the user struggles with understanding the dot product implementation and the source of the warnings. Suggestions are made to improve variable naming for clarity and to test the code with known examples to verify correctness. The discussion emphasizes the importance of learning programming fundamentals to avoid recurring issues.
kaizen.moto
Messages
94
Reaction score
0
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.
 
Physics news on Phys.org
term[n_Integer,m_Integer]:=Product[matg[],{i,m+1,n}] hl[[m]] /;(n>=m)
Subscript[mata,n_]:=Sum[term[n,i],{i,n}]
 
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
 
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.
 
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.
 
term[n_Integer,m_Integer]:=Apply[Dot, Table[matg[],{i,m+1,n}]].hl[[m]] /;(n>=m)
 
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).
 
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.
 
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.
 
  • #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!
 
  • #11
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.
 

Similar threads

Replies
2
Views
3K
Replies
2
Views
2K
Replies
5
Views
3K
Replies
1
Views
2K
Replies
1
Views
3K
Replies
13
Views
2K
Replies
1
Views
2K
Replies
2
Views
3K
Back
Top