Mathematica Can someone me on this problem using Mathematica

  • Thread starter Thread starter kaizen.moto
  • Start date Start date
  • Tags Tags
    Mathematica
AI Thread Summary
The discussion focuses on improving the syntax for a Do-loop function in Mathematica that involves solving simultaneous equations and summation. Participants suggest that the user could rewrite the system using matrices, utilizing Array[], SparseArray[], or Band[] for better efficiency. Instead of using Do loops, which are common in languages like FORTRAN and C, it is recommended to use Table to generate results, allowing for a more concise and understandable code structure. The use of Reap and Sow is also mentioned as an alternative for those who prefer Do loops, enabling the collection of values during iteration. The user expresses gratitude for the guidance received and acknowledges the effectiveness of the recommendations, emphasizing a desire to learn and improve programming skills in Mathematica. The conversation highlights the importance of writing clear and efficient code that reflects the underlying mathematical logic.
kaizen.moto
Messages
94
Reaction score
0
Hi all,

Please help me to improve the syntax which involving Do-loop function and solving simultaneous equations and summation together.

the answer should match with the one given in the following cell.

Many thanks for any feeback.
 

Attachments

Physics news on Phys.org
That's really ugly! Where did this system of equations come from?

All your equations look linear -- can you not rewrite the system using matrices?

You can construct your matrices of unknowns using Array[] or SparseArray[] and maybe Band[] etc...
 
Perhaps instead of

Do[...,{m,1,3,2},{n,1,3,2},{r,0,15,1}];Sum[jjj1[0],{m,1,3,2},{n,1,3,2}]/.{...}

consider

Table[Sum[...;jjj1[0]/.{...},{m,1,3,2},{n,1,3,2}],{r,0,15,1}]

so you can put the work for Solve inside the Sum and have the expression you want to Sum as the last item before your Sum indicies and then you create a Table of all the needed Sums.

FORTRAN and C and BASIC have drilled into people that they must use Do or For. But in Mathematica it is sometimes better to accomplish this using Table.

So the alternative I wrote above, instead of Doing something 16 times, you create a Table to hold the 16 results. Each of those results will be your Sum over m and n. Once you have that Table you can then use Total to add up all the jjj1 values if needed.

If you wish to continue using Do and For then there is a method to accomplish this using Reap and Sow.

Reap[ Do[x=y+2;etc;etc;Sow[x],{n,1,3,2}]][[2,1]]

Any time anywhere inside your Do or For you can Sow an expression, even multiple times. All those values will be concatenated into a list and when the Do is finished the Reap will give you the list of values. The [[2,1]] discards some extra information that Reap and Sow also put in there that you may or may not need.

So see if you can move your Sum inside a Table or use Reap and Sow.

If you need more help then try to explain how you need this changed and I'll see what I can do
 
I don't ask for any judgement how beautiful or ugly my syntax or equations are in this forum. As a new user of Mathematica, I want to learn how to use it in programming.

I really appreciate for the positive comment anyway. I would try to apply as per recommendations, thanks.
 
Hi Bill,

I got it now, you are so genius. Thanks a lot for your kind help. You save my day. It works at your first recommendation.

really appreciate it.
 
kaizen.moto said:
I don't ask for any judgement how beautiful or ugly my syntax or equations are in this forum. As a new user of Mathematica, I want to learn how to use it in programming.

I meant no offence by the ugly comment. It's just that code that "looks ugly" normally means that it's not optimal in any way. Neither efficient, understandable nor concise.

My comment about where the system came from was related and relevant -- in systems like Mathematica, it's normally best if your code can reflect the underlying logic of the problem as much as possible. A whole heap of Do[] loops rarely reflects how you should think about a math problem.
 

Similar threads

Replies
1
Views
3K
Replies
0
Views
2K
Replies
5
Views
3K
Replies
23
Views
3K
Replies
6
Views
4K
Back
Top