# How to make a simplified general code for repetitive formulations in Mathematica

• Mathematica
• kaizen.moto
In summary, your code can generate all the 17 expressions, but the subs need to be defined explicitly.
kaizen.moto
Hi there,

I have altogether 17 formulations or expressions which are repetitive and have link from each other.

My question is that how to make these formulation or expressions in a simpler form. In another word, how to make the codes or syntax become shorter due to these repetitive expressions.

Is there any better way to express all the 17 formulations in just one or two general codes but yet it works for all 17 expressions.

For instance:

the general expression is sub[r_]:=..... (1)

so if I want to substitute the value of any r, from 1 to 17, I got all the different 17 expressions as stated in the notebook attached.

Thank you in advance for any feedback.

#### Attachments

• formulation.nb
41.9 KB · Views: 507
Of course you can define
sub[1] = ...
sub1 = ...
and so on. If you're happy to use the current right-hand-sides of the definitions.
You don't need to have blanks (_) in the definition, i.e. you don't need sub[r_] := ...

But if you want to generate the right-hand-sides of the definition and use a single
sub[r_] := something goes here that depends on r
Then that might be a bit trickier. Do you have a clear mathematical formulation that generates all of the subs? I couldn't see anything too obvious from your code.

Sorry, I think I have confused you.

Actually, Iam trying to create a generic code of which I think looks like this sub[r_]:= ...

This desired generic code would be applicable to compute all the matrices as per r value.

Thank you for any help.

#### Attachments

• RevisedFormulationaaa.nb
53.3 KB · Views: 493
OK, you can implement a recursive definition.
Your text showed that you knew what you wanted, just not how to obtain it!

See the attached notebook.

#### Attachments

• RevisedFormulationaaa.nb
63 KB · Views: 486
• RevisedFormulationaaa.pdf
188 KB · Views: 510
wow..brilliant!. Eventhough, the notebook you sent was empty, I could not manage to open it. But, I have re typed the syntax from the pdf file and run the codes, its really works...you are truly very good at this...fantastic job..and I become more and more like Mathematica itself.

For your info, why sub[1] does not return the required expression. I mean when I run sub[1], it gives me sub[1]. However, the rests are working perfectly.

anway, can you please send the notebook again to my email add at kaizen.moto@yahoo.com, if you don't mind.

I really really apreciate your kind help..may God bless you..

## 1. How do I create a loop in Mathematica for repetitive formulations?

In Mathematica, loops can be created using the For or While functions. These functions allow you to set a starting value, condition, and step size for the loop. For example, the code For[i = 1, i < 10, i++, Print[i]] will print the numbers 1 to 9.

## 2. Can I use variables in a repetitive formulation code?

Yes, you can use variables in a repetitive formulation code in Mathematica. Variables can be defined using the = operator, and their values can be updated within the loop. For example, x = 1; For[i = 1, i < 10, i++, x = x * i]; Print[x] will calculate the factorial of 9 and print the result.

## 3. Is there a way to control the number of iterations in a repetitive formulation code?

Yes, the number of iterations in a repetitive formulation code can be controlled using the Break or Continue statements. These statements allow you to exit the loop or skip certain iterations based on a condition. For example, For[i = 1, i <= 10, i++, If[i == 5, Break[]]; Print[i]] will print the numbers 1 to 4.

## 4. How can I make my repetitive formulation code more efficient?

To make your repetitive formulation code more efficient, you can use built-in functions or vectorization techniques. Built-in functions such as Table or Array can perform repetitive tasks without the need for a loop. Vectorization techniques involve performing operations on entire arrays or matrices at once, rather than on individual elements. This can greatly improve the speed and efficiency of your code.

## 5. Can I create nested loops in Mathematica?

Yes, you can create nested loops in Mathematica. This allows you to perform repetitive tasks within another loop. For example, For[i = 1, i < 5, i++, For[j = 1, j < i, j++, Print[j]]] will print the numbers 1 to 3 in the first iteration, 1 to 2 in the second iteration, and 1 in the third iteration.

• MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
3K
• MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
3K
• MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
• MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
• MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
5K
• MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
3K