Good day to everyone!
I have two (just for simple example) functions and they compose a map:
m[t_]:=RotationMatrix[t].#&
s[k_]:={#[[1]],#[[2]]-k2 #[[1]]^2}&
map=Composition[m[0.2],s[2]]
If I use this map
in NestList
all works nice:
NestList[map,{0.01,0.001},1000]
But now I want to change the first argument t
of m[t]
at each (or with some step) iteration. For this I was hoping to use FoldList
, but this does not work:
FoldList[Composition[m[0.2+#2]&,s[2]],{0.01,0.001},Range[0.01,0.02,0.001]]
How can I use Composition
in FoldList
?
EDIT
I'm not sure can I add some more wishes or should I post another question.
To speed-up iterations, I can Compile
functions:
mc=Compile[{t,{x,_Real,1}},{{Cos[t],-Sin[t]},{Sin[t],Cos[t]}}.x]
sc=Compile[{k2,{x,_Real,1}},{x[[1]],x[[2]]-k2 x[[1]]^2}]
Using With
technique that @kglr advised in comment I can write:
FoldList[With[{xx = #2},RightComposition[mc[.2+xx,#]&,sc[2,#]&]],
{0.01,0.001},Range[0.01,0.02,0.001]]
This works. But what confuses me is that #2
is in purple and Composition
does not work when replaced RightComposition
. What is the difference in this case between Composition
and RightComposition
? Am I using Compile
correctly?
s[k2_]:=...
? $\endgroup$FoldList[With[{xx = #2}, Composition[m[.2 + xx], s[2]]@#] &,...]
? $\endgroup$#2
fails. $\endgroup$FoldList[Function[{foldarg1, foldarg2}, With[{xx = foldarg2}, Composition[m[.2 + xx], s[2]]@foldarg1]], ...]
, is it easier to understand? $\endgroup$