LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Problem Summing through a For Loop

Solved!
Go to solution

I have the following diagram set up to do the sum in a fourier series, given the arrays containing the coefficients.  The coefficients are determined externally, loaded into an array, then "n" is changed for each iteration, and the results summed with every other "n" from 1 to 5 right now.


Everything looks good, but the loop is not summing the results.  Can anyone point me in the right direction to fix this?  The whole thing is in a case structure right now so that I can run the vi, load the coefficients, then run the for loop.

0 Kudos
Message 1 of 7
(3,216 Views)

Replace the output of the + function inside the for loop into a shift register (right-click on the tunnel), and connect the input of the + to the left hand tunnel of the shift register.

 

To spot your error use execution highlighting.

 

Ton

Free Code Capture Tool! Version 2.1.3 with comments, web-upload, back-save and snippets!
Nederlandse LabVIEW user groep www.lvug.nl
My LabVIEW Ideas

LabVIEW, programming like it should be!
0 Kudos
Message 2 of 7
(3,178 Views)

Your feedback node is useless for what you want to do because it will not iterate with the for loop, only the value of the last iteration of the loop will be pass to it. So the result you have is the last loop iteration value (not added with the previous iteration values).

 

To solve this do as TCPlomp says, replace the feedback node by a shift register.

 

Ben

shiftregister.png

 

0 Kudos
Message 3 of 7
(3,150 Views)

Additional note on feedback node. The feedback node is initialized only when you compile or load the VI. So if you don't close the VI it will keep in memory its last value. If you run consecutively the code in the snippet of my previous post the indicator at the output of the loop with the feedback node will indicate 5, 10, 15, ...

 

So, feedback node should be avoided as much as possible.

 

Ben

0 Kudos
Message 4 of 7
(3,138 Views)
Solution
Accepted by topic author krwlz101

ben64 wrote:

 

So, feedback node should be avoided as much as possible.

 


I happen to disagree with that statement (as well as the sentiment).  I don't "avoid" very many things in LV, not even locals.  I understand the options and apply the best one at any given time.  It happens that locals are not the best option in almost all cases.  Feedback nodes are far more useful than locals, and you are missing a lot by avoiding them.  They are to be respected, understood, but not feared.  In the simple use cases they can often be exchanged for the common shift register and LV will do it for you.  Feedback nodes have other important features that make them valuable in certain cases.  The ability to exist without the loop, so no more single iteration while loops simply to hold a shift register is one nice example.  Initialization with an array is another.  Read the help and some forum topics for more discussion.

 

In this case, the fundamental flaw is that the FN needs to be inside the loop.  Fixing ben64's example shows how it is done.

 

FeedbackNode.png

 

No difference between SR and FN in this case, but no reason to avoid it.  In fact, in most new code I write I tend to favor FN since they often shorten wiring and everything I can do with a SR I can do with a FN but not vice versa.

Message 5 of 7
(3,123 Views)

Thanks for all the responses, and I've figured out the loop now.  I went with Darin's solution, after considering the shift registers, and ultimately the logic change that was made by going with them.

 

I tend to prefer feedback nodes as well, as they are more intuitive to look at.  Thanks again all for the advice.

0 Kudos
Message 6 of 7
(3,119 Views)

ben64 wrote:

Additional note on feedback node. The feedback node is initialized only when you compile or load the VI. So if you don't close the VI it will keep in memory its last value. If you run consecutively the code in the snippet of my previous post the indicator at the output of the loop with the feedback node will indicate 5, 10, 15, ...

 

So, feedback node should be avoided as much as possible.

 

Ben


Ben-

Perhaps a late post but.  Feedback node initialization can be accomplished with MORE control than the SR has.  If you wire the initializer you can select, from the FBN properties, to initialize globally, only on compile or load or every time a loop runs (if in a loop.)


"Should be" isn't "Is" -Jay
Message 7 of 7
(2,958 Views)