I have written a module for my assignment like below.

 V[n + 1] = rk[x[n], y[n] ,z[n], V[n]];
 vs = V[n].V[n+1];
 V[n + 1] = 

I want to modify it with efficient and faster built-in functions like FoldList,FixedPoint,ComposeList etc. I tried FoldList, however, I can't get the right syntax for my program. Can I use these functions for this purpose? How would I write them? Or is it better to use program like this?

Any help is appreciated.

  • 2
    $\begingroup$ I edited your program to remove the Subscript constructs, which just make it harder to read on a web page. The short answer to your question is "yes, you can use these other functions like FoldList, and yes, you should". However, without knowing what data and func1 are, it is a bit hard to work out code to actually implement what you want to do. Similarly, how does one get a[0] etc? And what are m and n? Please post a minimal working example so that people can help you. $\endgroup$
    – Verbeia
    Jan 9 '13 at 22:37
  • $\begingroup$ @Verbeia Thank you, it looks better :) I have written n instead of t by mistake. data is interpolated tensor data and I find the tensor at {a[t], b[t], c[t]} by k[t]. func1 is a special function written by me. $\endgroup$
    – cesm
    Jan 9 '13 at 22:50
  • 2
    $\begingroup$ This still isn't a minimal working example: I can't see a definition for rk. If you have a separate function for it, you probably shouldn't have it as a local variable inside the Module. May I also point out that completely changing the variable names half an hour after posting the original question makes it quite difficult for people who are trying to answer the question to keep track. I would also recommend against using single capital letters as variable names (V etc) as they are often reserved for use as system symbols. $\endgroup$
    – Verbeia
    Jan 9 '13 at 23:43
  • $\begingroup$ @Verbeia Yes, rk is a separate function. Yes, I am aware of this and I am sorry, however, I should post the original program. All right, thank you. $\endgroup$
    – cesm
    Jan 10 '13 at 0:01
  • 1
    $\begingroup$ I'm still waiting for a complete executable code sample. What is rk, what is Fa, and how to you call curve? $\endgroup$
    – Mr.Wizard
    Jan 10 '13 at 9:41

I think you actually want NestWhileList. The reason is that FoldList "folds" successive elements of a list into the calculation in a fixed order, as this minimal example shows:

FoldList[0.9*#1 + #2 & , 0., RandomVariate[NormalDistribution[0,1],{100}]]

But you seem to need to access the data set at indices that might not be successive and seem only to be determined as part of the longer calculation.

You can also build in the condition to break the loop in directly to this construct, without having 1970s-BASIC-style Goto and Label expressions.

I am not going to rewrite your whole Module for you, as I think it will be a useful learning exercise to do it yourself. Remember you will need to use a pure function in the first argument. Here are some hints to get you started:

First, think of things in terms of a state vector and work out how that state vector gets updated. In your case, you have four elements: x[n], y[n], z[n], and V[n], so you should be setting up the NestWhileList to take a four-element vector and output a four-element vector.

Second, this piece

V[n + 1] = rk[x[n], y[n], z[n], V[n]]; 
vs = V[n].V[n + 1]; 
V[n + 1] = If[vs < 0, (-1)*V[n + 1], V[n + 1]];

Can be rewritten as:

V[n+1]= With[{tmp=rk[x[n], y[n], z[n], V[n]]}, Sign[V[n]*tmp]*tmp]
  • $\begingroup$ Thank you very much.Right, I am here because of learning how to think functionally when using Mathematica not having my program done by other users :) In addition, I am fairly new to Mathematica. $\endgroup$
    – cesm
    Jan 10 '13 at 9:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.