# Passing a function and its derivative to a module

This is a classical way to calculate Lyapunov exponent

\[Lambda][r_] := Module[{f, l},
f[x_] := μ x (1 - x);
l[x_] := Log[Abs[μ (1 - 2 x)]];
Mean[l[NestList[f, 0.1, 1*^2]]]];
Plot[λ[μ], {μ, 0, 4}, PlotStyle -> Thickness[.0001],
AxesLabel -> {"μ", "λ(μ)"}]


No problem it works nicely. There are two problems

1) I have earlier in my code defined $f[x\_, \mu\_]$ and I would like not to redefine $f$. 2) In the code one pass the derivative by hand --- μ (1 - 2 x) ---. I have tried to add a function $g$ in the module which calculate the derivative but this doesn't work.

How could I do ? I think 1) is perhaps impossible with a module but 2) ?

• What is r? it's never used... – BlacKow Oct 14 '16 at 20:02

Is this what you want? I assumed that it is $\lambda(\mu)$ and not $\lambda(r)$

λ[μ_, f_] := Module[{l},
l[x_] := Evaluate@Log[Abs[D[f[x, μ], x]]];
Mean[l[NestList[f[#, μ] &, 0.1, 1*^2]]]
];

f[x_, μ_] := μ x (1 - x);

Plot[λ[μ, f], {μ, 0, 4},
AxesLabel -> {"μ", "λ(μ)"}, PlotRange -> Full]


Assuming r is supposed to be mu, another way to accomplish the above is

\[CapitalLambda][f_] := Module[{
df, l
},
df = Derivative[1][f];
l = Log@*Abs@*df;
Mean[l[NestList[f, 0.1, 1*^2]]]
];

f[\[Mu]_] := Function[x, \[Mu] x (1 - x)]

Plot[\[CapitalLambda][f[\[Mu]]], {\[Mu], 0, 4}, AxesLabel -> {"\[Mu]", "\[Lambda](\[Mu])"}]


which produces the same plot as BlacKow's answer.

• +1 Alternatively to be concise you can define your f[μ]:=μ # (1 - #) & – BlacKow Oct 14 '16 at 21:39