07-26-2023 10:57 PM
I have a main 1D array Ambient Temperatures [1 2 3 4 5 6 7 8... i] and another 1D array A_corrections (5 elements) [a b c d e].
The equation is this: Result = a * i + b * i^2 + c * i^3 + d * i^4 + e * i^5. where i is an element of the array.
How do I configure this in Labview?
I tried setting up a for loop, with N = 5. The power function takes i as an index, and an element from the array. Then I am stuck..
Thank you.
Solved! Go to Solution.
07-26-2023 11:15 PM
My pseudocode/logic is:
Within the loop, the array element i is powered by the index 1 and multiplied with the first element of the 1D array (a). The output is x.
Then, the index is incremented by 1 and the array element i is powered by this index and multiplied with the second element (b) This output is added to x.
This process repeats until it reaches the last element of the 1D array(e). The final output is the array.
07-27-2023 12:44 AM
I am at this:
now for each inner loop, I need to add to the previous loop. Not sure how to proceed.
07-27-2023 01:11 AM - edited 07-27-2023 01:13 AM
Hi beginner,
@Programming_beginner wrote:
I have a main 1D array Ambient Temperatures [1 2 3 4 5 6 7 8... i] and another 1D array A_corrections (5 elements) [a b c d e].
The equation is this: Result = a * i + b * i^2 + c * i^3 + d * i^4 + e * i^5. where i is an element of the array.
So you want to calculate a polynomial evaluation for each input array element?
Then you should do just that:
That function is polymorph and can handle scalar and 1D inputs…
07-27-2023 01:42 AM - edited 07-27-2023 01:57 AM
Got it. The value is correct. How do I repeat this for each value of an array? [1,2,3 ...... , ith element]
^The polynomial trick works but I manage to code this.
Edit:
07-27-2023 03:04 AM - edited 07-27-2023 03:04 AM
Hi beginner,
@Programming_beginner wrote:^The polynomial trick works but I manage to code this.
Edit:
Like this:
07-27-2023 09:11 PM
This is my final solution.
The shift register is to hold the previous value for summation (like a * i), then for next loop, this is summed with ( b * i ^2), which is then summed with ( c * i ^3) ....
It's pretty precise to 4d.p. I follow strictly with regards to the powers. (instead of just multiply again, though I will try to use your method for the future, GerdW)
07-27-2023 11:21 PM - edited 07-27-2023 11:28 PM
@Programming_beginner wrote:
This is my final solution.
No, just no!!! (The term "final" should not exist here!)
First of all the A array belongs before the outer loop because it is not supposed to change during the run (make the tunnel non-indexing!). If it is before the loop, the compiler can assume that is does not change during the execution and can apply further optimization!
Gerd already suggested the polynomial evaluation, but forgot to mention that you need to include the 0th term (i.e. i^0), which is zero here because your expansion does not have an offset. (maybe it should!)
So that's the first choice! (top in image)
If you want to do it explicit, one loop is sufficient! (middle, yes it can be more optimized))
Compare that with the cleaned up solution of your version (bottom).
Of course all are much more precise than just four decimal digits and all give exactly the same result (within the limits of DBL math)!
07-28-2023 12:25 PM
A few more words about performance:
If [# of temps] is M and [# of factors] is N
Array operations have a significantly higher chance to take advantage of SIMD instructions, and will thus perform better. Of course polynomial evaluation is the most optimized for large inputs.
(These results are for a very old laptop. Result might be slightly different on other hardware. YMMV)
Some real world benchmarks (5M temps):
Curiously, for smaller inputs (e.g. 500 temps):
For even smaller inputs (e.g. 10temps)