0

I have a function that calculates temperature that is dependent on both temperature and time. I want to be able to iterate the function to calculate a new temperature and then use that as the next temperature input of the function, and increase the time input by 1.

For example, if my function is F[T,t], I want to be able to make it go through this sequence:

F[T0,t0]
F[F[T0,t0],t1]
F[F[F[T0,t0]],t2]
etc. 

I think that I can do this with a combination of Fold and a For loop, but I'm not sure how exactly.

Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453
  • Rest@FoldList[f, T0, {t0, t1, t2, t3}]? – kglr Jan 10 '15 at 16:08
  • @kguler You can include T0 in the list, FWIW. – Mr.Wizard Jan 10 '15 at 16:09
  • Welcome to Mathematica.SE! I suggest the following:
    1. As you receive help, try to give it too, by answering questions in your area of expertise.
    2. Read the [faq]!
    3. When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge.

    Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign!

    –  Jan 10 '15 at 16:18
  • @Mr.Wizard, thought that was a v10 feature; but it does work in v9 too. – kglr Jan 10 '15 at 16:25
  • Voting to close as this seems the very definition of FoldList[] – Dr. belisarius Jan 10 '15 at 16:58
  • @kguler As noted here I was told that it was implemented in 2011. – Mr.Wizard Jan 10 '15 at 17:16
  • @belisarius If Helen confirms my interpretation I shall join you. If F[F[F[T0,t0]],t2] is not a mistake the question requires additional clarification. – Mr.Wizard Jan 10 '15 at 17:17
  • @Mr.Wizard After reading the OP's "spoken" description of the problem I got convinced that your interpretation is right and the posted sequence is wrong. – Dr. belisarius Jan 10 '15 at 17:19
  • @Mr.Wizard After reading somewhere from you the syntax shortcutting for FoldList[] it is so much lean now. Thanks again. – Dr. belisarius Jan 10 '15 at 17:22
  • Helen, I am going to close this on the assumption that my interpretation, shared by belisarius, is correct. If I am wrong please explain what you really need and I shall reopen. – Mr.Wizard Jan 10 '15 at 18:37

1 Answers1

3

I think FoldList alone will do what you want. The output is slightly different from the sequence you show but I am assuming that is an error in the question.

FoldList[F, F[T0, t0], {t1, t2}]
{F[T0, t0], F[F[T0, t0], t1], F[F[F[T0, t0], t1], t2]}

This could also be written:

FoldList[F, {T0, t0, t1, t2}] // Rest
{F[T0, t0], F[F[T0, t0], t1], F[F[F[T0, t0], t1], t2]}

See: Shorter syntax for Fold and FoldList?

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371