Hello,
I am looking for my Arduino (uno or mega-not yet ran on mega) to perform a 3rd order polynomial calculation. Im not doing anything too complicated other than taking a A-D reading and obtaining a control output.
I am using float for all input parameters as follows (note "x3" etc relates as x to the power of 3 etc)
float x3 = 15.62500; // 3rd order value from excel Array LINEST calc
float x2 = -2343.81608; // 2nd order value from excel Array LINEST calc
float x = 117196.20227; // 1st order value from excel Array LINEST calc
float c = -1953394.88388; // constant value from excel Array LINEST calc
These values give me a solution for "y" given an input of "x".
On the whole its working well and behaving as i would expect. However, when i provide the same input value of X on consecutive runs i get slightly different results of Y.
This is likely due to the inherent error of float calculations.
During the calc, i believe i stay within the ranges, the values during calculation range close too 6,000,000 and to just close to -6,000,000
Now, i have revised the number of decimal places (not yet ran on arduino) down which keeps the output within the region of what i want, my hope was fewer decimal places may lead to less error?
float x3 = 15.625;
float x2 = -2343.816;
float x = 117196.202;
float c = -1953395.08;
To keep numbers sensible, ive already scaled the analogue input using map to a range of 48.0000-52.0000 which is the X value for my input.
My analogues are correct and the mapping is correct so im sure the error is float maths.
I've done alot in excel to try to remove all decimal places (eg x 1000 on everything) but this screws up the polynomial maths. Or, i end up with very large numbers or very small ones. This is where i found the scale range of 48-52 worked well for me.
The application for reference is to take pressure (800-1000mBar) and convert to a control signal for a blower. Using op-amps, ive scaled 0-1024 as 798.6 to 1003.4 mBar. I then map 0-1024 to 48.0000-52.0000 (because this corresponds to the mV output of the sensor and all the maths is sensibly manageable).
Perhaps i can use some numbers as float and others as double?
Is there an inbuilt polynomial function ive not seen?
Processing speed is not an issue, in fact i do a millis() comparison at the end over the start of the loop and its generally 0-1ms which is fine for me.
Any advise on handling large numbers with better maths accuracy would be good.
Thanks,
Stuart