Mathematica repeating commands

  • Mathematica
  • Thread starter Anna Kaladze
  • Start date
  • Tags
    Mathematica
In summary, the user is trying to use Mathematica to calculate the value of y for different values of "a" ranging from 1 to 2 in increments of 0.5, and store the results in an array. The code provided uses a While loop and the AppendTo function to achieve this, but it may not be the most efficient method. The user suggests using the Compile function for better performance.
  • #1
Anna Kaladze
35
0
Hi,

I have the following simple code in Mathematica:

a=1;
step=0.5;
b=2;
y=a^2-5
-4

What I need to do, is make Mathematica calculate the value of y each time for a different value of "a" ranging from the initial value of 1 to "b"(=2) in "step" increment (=0.5), and remember previous values of y in constructing a final array of y. That is, the answer would be y={-4, -2.75,-1}. It would be great to avoid using Table commands or tampering with the individual lines much. Just plain simple multiple iterations which would “nicely” lead to the y array of 3 numbers.

Any help is appreciated.

Thanks a lot.

Anna.
 
Physics news on Phys.org
  • #2
I don't see how you can do this without using arrays. So you have...

atable=Table[1+(i-1)*0.5,{i,1,3}];
ytable=Table[Extract[atable,i]^2-5,{i,1,Length[atable]}];
 
  • #3
Something like the http://reference.wolfram.com/mathematica/guide/ProceduralProgramming.html" construct

Code:
In[1]:= a=1;
        step=0.5;
        b=2;
        y={};
        While[a<=b, AppendTo[y,a^2-5]; a+=step]
        y
Out[6]= {-4,-2.75,-1.}

Of course, this is much slower than using Do or Table (etc) for two reasons;
1) At each step it has to make the comparison a <= b
2) Modifying Lists using AppendTo is a slow way to construct a list. http://reference.wolfram.com/legacy/v5_2/Built-inFunctions/AdvancedDocumentation/LinearAlgebra/LinearAlgebraInMathematica/Performance/ProgrammingEfficiency/AdvancedDocumentationLinearAlgebra5.1.1.html" .

That said, I assume that this is a simple example you constructed and that in your actual code the real bottle neck will lie somewhere else, so the above to considerations can be safely ignored.

The advantage of the above code is that it could easily be put into a
http://reference.wolfram.com/mathematica/ref/Compile.html"
command and be made quite fast.
 
Last edited by a moderator:

1. How do I repeat a command in Mathematica?

To repeat a command in Mathematica, you can use the Do or While loop functions. These allow you to specify the number of times you want the command to be repeated.

2. Can I repeat a command more than once in a loop?

Yes, you can repeat a command multiple times in a loop by using the Do or While loop functions. You can specify the number of repetitions by changing the parameters in the function.

3. How do I repeat a command until a certain condition is met?

You can use the While loop function in Mathematica to repeat a command until a certain condition is met. The condition should be specified inside the loop function, and the command will continue to be repeated until the condition is no longer true.

4. Is there a way to repeat a command with a specific time delay?

Yes, you can use the Pause function in Mathematica to add a time delay between repetitions of a command. This function takes in the number of seconds you want the delay to last.

5. Can I repeat a command on a specific list of values?

Yes, you can use the Table function in Mathematica to repeat a command on a specific list of values. This function takes in the command you want to repeat and the list of values you want to iterate through.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
262
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
352
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
223
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
4K
Back
Top