# How to compose two functions

Suppose I have a function f(x)=x^2+1

What is the command to compose it with itself twice or thrice?

I was using:

f= (x^2+1)

g= lambda t: (t^2+1)

f(*g)

or

sage: g = lambda t: (t^2+1) sage: f = lambda x: (x^+1) sage: f(*g(t))

or

sage: x = var('x')

sage: f=x^2+1

sage: compose(f, 3, x)

Nothing works! Also, I don't know how to use the Dynamical system code in this case here.

Also, it would be of great help if you can give me one example command for the composition of two different functions e.g f(x)=x^2+1 and g(x)=x^3+2 if it is not obvious from the answer of the composition of the same function twice.

edit retag close merge delete

Sort by » oldest newest most voted

Hello, @Arnab! I suppose the following is the answer you're looking for. First define $f$:

f(x) = x^2 + 1


Now compose simply by writing:

f2(x) = f(f(x))
f3(x) = f(f(f(x)))


As you might guess, $f2$ is the composition $f\circ f$ ($f$ with itself), while $f3$ is the composition $f\circ f\circ f$ (three times $f$).

The same mechanism works for different functions. Let's define

f(x) = x^2 + 1
g(t) = t^3


Then you can write

fg(t) = f(g(t))
gf(x) = g(f(x))


The first one will give you $t^6+1$, while the second will give you $(x^2+1)^3$.

I hope this helps!

more