Mathematica - Iterations with 2 arguments

  • Mathematica
  • Thread starter nothingbetter
  • Start date
  • Tags
    Mathematica
In summary, the conversation was about building a simple function that takes 2 arguments and generates a list of iterations based on a given formula. The solution involved using the NestList function and the shorthand "&" notation for Function[].
  • #1
nothingbetter
5
0
I'm trying to build a simple function that takes 2 arguments. Basically, I want it to do this:

x(n+1)=a*sin[x(n)]

and generate a list of x0,x1,x2 etc. up to x(n) like NestList does, starting from x0=1.0

So I want to make a list of these iterations, where a is a constant of my choice, and n is the number of iterations. My first guess was to try

iteration[a_,n_]:=NestList[a*Sin,1.0,n]

but of course it doesn't work as Mathematica doesn't recognize a*Sin as a function. So I tried defining a function acos[a_,x_]:=a*Sin[x] first but not only does it look inefficient to define 2 functions for something this simple, it also introduces another argument, and I still can't find a way to get it to work.

Thanks!
 
Physics news on Phys.org
  • #2
In[1]:= iteration[a_,n_]:=NestList[(a*Sin[#])&,1.0,n]

In[3]:= iteration[1,4]
Out[3]= {1.,0.841471,0.745624,0.67843,0.627572}

"&" is a shorthand for Function[] and you could equally write this as

In[4]:= iteration[a_,n_]:=NestList[Function[x,a*Sin[x]],1.0,n]

In[5]:= iteration[1,4]
Out[5]= {1.,0.841471,0.745624,0.67843,0.627572}

& is commonly used in Mathematica programming, but often confusing when first encountered.
 
  • #3
I see. It works great, thank you very much!
 

Related to Mathematica - Iterations with 2 arguments

What is the purpose of using 2 arguments in iterations with Mathematica?

The use of 2 arguments in iterations with Mathematica allows for more flexibility and control in the iterative process. It allows the user to specify both the starting point and the end point of the iteration, as well as the step size.

How do I use 2 arguments in iterations with Mathematica?

To use 2 arguments in iterations with Mathematica, you can use the Table function and specify the starting point, end point, and step size in the form of {i, start, end, step}. For example, Table[i, {i, 1, 10, 2}] will iterate through the values of i from 1 to 10 with a step size of 2.

What are some common mistakes when using 2 arguments in iterations with Mathematica?

One common mistake is forgetting to specify the step size, which will result in the default step size of 1 being used. Another mistake is specifying the end point as a decimal number, which can lead to unexpected results due to rounding errors.

Can I use non-numeric values as arguments in iterations with Mathematica?

Yes, you can use non-numeric values as arguments in iterations with Mathematica. However, the result will depend on the specific function being used. For example, using a non-numeric value as the step size in the Table function will result in an error.

How can I visualize the results of iterations with 2 arguments in Mathematica?

You can use the ListPlot function to visualize the results of iterations with 2 arguments in Mathematica. Simply use the Table function to generate a list of values and then plot them using ListPlot. You can also use other plotting functions such as Plot or Graphics to create more complex visualizations.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
292
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
1K
  • Precalculus Mathematics Homework Help
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
Back
Top