LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

how to improve while loop

I have this while loop that I want to make improvement on.

Does anyone have any suggestions on what to do?

The loop start by setting a STARTING Input Amplitude on the Frequency Generator.

per iterations up to 40 only, it will increase about 0.002 (input adjustment)

Then read the final RMS reading. If FINAL RMS reading is within 0.1259  +- 0.00073 then stop the loop.

This is a Linear.

Can anyone suggest a way to make this easier?

Running the loop 40 times to get to the desired value is not ideal.

Any sugestions would be great!

thanks

Untitled.png

 

 

Best regards,
Krispiekream
0 Kudos
Message 1 of 18
(3,678 Views)

Use a binary search!

 

Take the entire range of your adjustment

 

Divide it by 1/2 of your tolerance

 

Find the next higher power of 2

 

Start at 2^n / 2 (0b1000...0) Add or subtract the next significant bit depending on the sign of the error.  Repeat the loop until all bits have been added or subtracted (+/- 1)

Select the value with the least error from the output loop

 

n-1 steps every time or, conditionally exit the for loop if within tolerance if you get want "Close enough" rather than Closest


"Should be" isn't "Is" -Jay
Message 2 of 18
(3,658 Views)

Why do you use the sign to get -1, 0 or 1? Why not increase\decrease proportionally? Bigger difference, bigger correction seems more logical. Maybe even use a PI or PID controller? Doing it like you are doing it now could also mean you're simply moving up and down one Input Measurement until coincidently you hit the spot. You might want to stop the loop if you're going down after going up or visa versa.

 

You might want to add some output's with auto indexing, so you see what steps are taken.

0 Kudos
Message 3 of 18
(3,581 Views)

Frequency +Adj Input A Measured Want
 0.000 10000.000 0.002 0.272 0.097 0.126
 1.000 10000.000 0.002 0.274 0.097 0.126
 2.000 10000.000 0.002 0.276 0.098 0.126
 3.000 10000.000 0.002 0.278 0.099 0.126
 4.000 10000.000 0.002 0.280 0.099 0.126
 5.000 10000.000 0.002 0.282 0.100 0.126
 6.000 10000.000 0.002 0.284 0.101 0.126
 7.000 10000.000 0.002 0.286 0.102 0.126
 8.000 10000.000 0.002 0.288 0.102 0.126
 9.000 10000.000 0.002 0.290 0.103 0.126
 10.000 10000.000 0.002 0.292 0.104 0.126
 11.000 10000.000 0.002 0.294 0.104 0.126
 12.000 10000.000 0.002 0.296 0.105 0.126
 13.000 10000.000 0.002 0.298 0.106 0.126
 14.000 10000.000 0.002 0.300 0.107 0.126
 15.000 10000.000 0.002 0.302 0.107 0.126
 16.000 10000.000 0.002 0.304 0.108 0.126
 17.000 10000.000 0.002 0.306 0.109 0.126
 18.000 10000.000 0.002 0.308 0.109 0.126
 19.000 10000.000 0.002 0.310 0.110 0.126
 20.000 10000.000 0.002 0.312 0.111 0.126
 21.000 10000.000 0.002 0.314 0.112 0.126
 22.000 10000.000 0.002 0.316 0.112 0.126
 23.000 10000.000 0.002 0.318 0.113 0.126
 24.000 10000.000 0.002 0.320 0.114 0.126
 25.000 10000.000 0.002 0.322 0.114 0.126
 26.000 10000.000 0.002 0.324 0.115 0.126
 27.000 10000.000 0.002 0.326 0.116 0.126
 28.000 10000.000 0.002 0.328 0.117 0.126
 29.000 10000.000 0.002 0.330 0.117 0.126
 30.000 10000.000 0.002 0.332 0.118 0.126
 31.000 10000.000 0.002 0.334 0.119 0.126
 32.000 10000.000 0.002 0.336 0.119 0.126
 33.000 10000.000 0.002 0.338 0.120 0.126
 34.000 10000.000 0.002 0.340 0.121 0.126
 35.000 10000.000 0.002 0.342 0.122 0.126
 36.000 10000.000 0.002 0.344 0.122 0.126
 37.000 10000.000 0.002 0.346 0.123 0.126
 38.000 10000.000 0.002 0.348 0.124 0.126
 39.000 10000.000 0.002 0.350 0.124 0.126
 40.000 10000.000 0.002 0.352 0.125 0.126

 

basically its doing that right now.

starts at input Amplitude of 0.272, measured 0.097 (not close enough). increase amplitude to 0.274 (+0.002 from 0.272).

Do this 40 times until we get close to 0.126

 

Best regards,
Krispiekream
0 Kudos
Message 4 of 18
(3,534 Views)

So, with a binary search you can do it in 6 steps ( or fewer)

Binary Search.png

Excuse the sloppy fast demo


"Should be" isn't "Is" -Jay
Message 5 of 18
(3,510 Views)

thanks for the visual!

I'll try it out. thank you!

What is optional SP?

Best regards,
Krispiekream
0 Kudos
Message 6 of 18
(3,505 Views)

In my snippet the code in the flat frame decoration could be used to exit the for loop early if a setpoint is found that is within tolerance.  The other situation is to always find the BEST setpoint rather than one that is "Close enough"  It depends on what you really want- Accuracy or a potential throughput increase.  Either way you save a bunch of time over a linear search.


"Should be" isn't "Is" -Jay
0 Kudos
Message 7 of 18
(3,497 Views)

 0.000 10000.000 0.002 0.272 0.096
 1.000 10000.000 0.002 0.336 0.119
 2.000 10000.000 0.002 0.368 0.130
 3.000 10000.000 0.002 0.384 0.136
 4.000 10000.000 0.002 0.376 0.133
 5.000 10000.000 0.002 0.372 0.132

 

I think I am getting closer. But not quite there yet. still working on it to see whats wrong

 

Best regards,
Krispiekream
Download All
0 Kudos
Message 8 of 18
(3,464 Views)

Try that again with the setpoint and the error in the file


"Should be" isn't "Is" -Jay
0 Kudos
Message 9 of 18
(3,453 Views)

Looking at that data, I'd say there is a linear relationship between input and output. So once you know +10 at the input equals +7 at the output (you can get this from two measurements), calculating the correct can be done in one step, using linear interpolation. This would be very similar to the binary search. Just instead of dividing the steps, you can make a much better estimate.

 

If you've done this once, you know the relation. So the next time, you can set the value in just one step if you're lucky.

0 Kudos
Message 10 of 18
(3,401 Views)