• Mathematica
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

• Exercise2.nb
22.5 KB · Views: 323

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...

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 dont 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 alot for your kind help. You save my day. It works at your first recommendation.

really appreciate it.

I dont 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.