Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
2  
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. –  Verbeia Jan 9 '13 at 22:37
    
@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. –  cesm Jan 9 '13 at 22:50
2  
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. –  Verbeia Jan 9 '13 at 23:43
    
@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. –  cesm Jan 10 '13 at 0:01
1  
I'm still waiting for a complete executable code sample. What is rk, what is Fa, and how to you call curve? –  Mr.Wizard Jan 10 '13 at 9:41
add comment

1 Answer 1

up vote 4 down vote accepted

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]
share|improve this answer
    
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. –  cesm Jan 10 '13 at 9:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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