Mathematica - Iterations with 2 arguments

  • Context: Mathematica 
  • Thread starter Thread starter nothingbetter
  • Start date Start date
  • Tags Tags
    Mathematica
Click For Summary
SUMMARY

The discussion focuses on creating a function in Mathematica that generates a list of iterations based on the equation x(n+1) = a*sin[x(n)]. The user initially attempts to define the function using NestList but encounters issues with Mathematica not recognizing a*Sin as a valid function. The solution provided involves using the shorthand "&" to define an anonymous function, allowing the user to successfully implement the iteration function as iteration[a_,n_]:=NestList[(a*Sin[#])&,1.0,n]. This method effectively generates the desired list of iterations.

PREREQUISITES
  • Familiarity with Mathematica programming language
  • Understanding of functional programming concepts
  • Knowledge of the NestList function in Mathematica
  • Basic understanding of trigonometric functions
NEXT STEPS
  • Explore advanced features of the NestList function in Mathematica
  • Learn about defining and using anonymous functions in Mathematica
  • Investigate the use of trigonometric functions in iterative algorithms
  • Study performance optimization techniques for Mathematica functions
USEFUL FOR

This discussion is beneficial for Mathematica users, particularly those interested in iterative functions, mathematical modeling, and functional programming techniques within the software.

nothingbetter
Messages
5
Reaction score
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
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.
 
I see. It works great, thank you very much!
 

Similar threads

  • · Replies 12 ·
Replies
12
Views
5K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 6 ·
Replies
6
Views
2K