Programming an almost linear graph

Well...

First, you should put the data in Excel, make the graph, then add a linear aproximation. That way, you can find out where exactly is the graph not linear. Use one or two decimal places for the aproximation as the Arduino won't like it too much if you use more than three.

Once you find the interval where the line is linear, get the equation and program it. Then the part that will most likely not be linear are the ends. Ok, imagine that 4 intervals in the beginning are not linear. You can make a linear aproximation inside the interval...

in pseudo code:

value = readADC(0);
if (value == 0 ||value < 0.12)
    final_val = Linearize (value, 0, 0.12); 
else{
   if (value >=0.12 || value < 0.38)
       final_val = Linearize (value, 0.12, 0.38); 
...
...

See what I mean??

Hmmm i think i got a bit your idea, but this code Linearize (value, 0, 0.12) is it a library in arduino???

tnx

The linear approx graph is attached, i didnt understand this code but Linearize (value, 0, 0.12)

tnx

grams = 17.299 volts^2 + 4068.3 volts - 488.45

R^2 = 0.9996

The linear fit is just as good though. I think that this is about as good as your data would allow you to estimate.

The best linear fit that goes through [0.12, 0] is grams = 4129.4 volts - 495.53 with an R^2 of 0.9995

Wow gardner you lost me.
R^2 - whats that?
tnx

R-squared is a correlation coefficient that measures how well the mathematical curve fits the actual data.

R-squared close to 1.0 means you have a really pretty close fit.

Ok didn't do this in mathematics engineering....
I found some help from this site http://wwweng.uwyo.edu/classes/meref/Curve_Fitting.pdf
Did you work out r2 using excel?

tnx

a job for - Arduino Playground - MultiMap

Yes. All in Excel. It's not SAS, but I have it handy. I've done a bit of fiddling with your data and I have a couple of observations.
(1) I think your last data point [4.620,19035] is bogus
(2) I think that the underlying volts/gram relationship is linear to well within the accuracy of the measurements you've made, and
(3) I think that the source of the errors is A/D sampling error, specifically in the imperfect values in the A/D's resistance ladder.

The orange sawtooth series is the difference between the linear regression and the actual gram values. I bet you a buck, that the saw tooths correlate with the switching of specific bits of the A/D value and that would be an indication of error in the resistor ladder network used by the A/D sampler.

The error curve does not have any simple shape that would indicate a correctable non-linearity in the volt/gram function.

Hehe tnx gardner for the info :). But I have not used an A/D converter. That is still to be used....
I am using a load cell; giving output voltage for the weigh on it.
The instruments used is an oscilloscope (Voltage peak+) and a very good power supply. Even the weights 1kg are exactly 1kg since I made a spent some time putting exactly 1kg of sand.

And yes i think that the last point is bogus...
Ill hook up the equipment again and take other readings but I think it is quite a good almost linear graph.

Another question is this, when you wrote :
** grams = 17.299 volts^2 + 4068.3 volts - 488.45**
How did you work that out??

With excel I managed to get this summary:

SUMMARY OUTPUT

Regression Statistics
Multiple R 0.999765814
R Square 0.999531683
Adjusted R Square 0.999505665
Standard Error 0.031847119
Observations 20

ANOVA
df SS MS F Significance F
Regression 1 38.9645237 38.9645237 38417.49623 2.01014E-31
Residual 18 0.018256302 0.001014239
Total 19 38.98278

Coefficients Standard Error t Stat P-value Lower 95% Upper 95% Lower 95.0% Upper 95.0%
Intercept 0.122532035 0.01375446 8.908531055 5.12634E-08 0.093634987 0.151429083 0.093634987 0.151429083
X Variable 1 0.000241939 1.23436E-06 196.0038169 2.01014E-31 0.000239346 0.000244533 0.000239346 0.000244533

Meaning that it is quite a good almost linear graph, but still I need to be accurate in this since I am refilling a cars a/c tank.

tnx again

There's a "display equation" property on the trendline in Excel. Also a "display R-squared". This is simply the quadratic that Excel fit. Incidentally, that was with the last point included, and when I chuck that one out the curve it fits is different.

Even the weights 1kg are exactly 1kg since I made a spent some time putting exactly 1kg of sand.

I would look to errors in these weights then. Do you have a bucket with 10 Kg, one with 5, etc? The error chart looks like a difference in the weight of the 10 Kg bucket vs the 5 Kg + 3 Kg + 2 Kg. At the points where you swap the small weights out for the one large one is where you get these sawtooths. It really does look like quantization noise of some type, and quantization of your physical weights could be a factor. The mean error is something like 50 grams. Are your weights accurate to that precision for sure? What instrument did you use to measure them?

Clicking on my excel graph - treadline gives me this equation and not the one you gave me (which i think is better, since this is for the linear equation) y = 0.0002x + 0.1225

What I did was I got 20kgs of sand, and put 1kg, 1kg, 1kg ..... into seperated plastic bags, so I have 20 plastic bags each 1kg. and exactly 1kg since I used a good electronic weighing scale to measure them.

The error could be since the base on which placing these plastic bags wasnt that good. Look at the photos attached you will understand by what I mean I think

What is your recommandation?? Do I use the linear equation in my code??

tnx

Full load pic
Note I used this and not gas since gas is really expensive and I wouldn't be happy using it for testing

There are two functions found so far:

A: grams = 17.299 volts^2 + 4068.3 volts - 488.45
B: grams = 4129.4 volts - 495.53

both can be implemented by the Arduino.
A is the better one, but the difference is small
B will be twice as fast as it need 3 FP operations while A needs 6 FP operations,

Question: Can you quantify the consequences for using A over B or vice versa? Or how bad is it?

Can you please explain were you are getting these functions:
A: grams = 17.299 volts^2 + 4068.3 volts - 488.45
B: grams = 4129.4 volts - 495.53 ???

My excel graph only displays my linear function as y = 0.0002x + 0.1225

Hmm i dont know exactly what will happen, what Im doing is refilling a cars a/c. Normally it wouldn't matter if there is a +/- 20grams difference in the amount refilled....

tnx

Can you please explain were you are getting these functions:

These were mentioned in previous posts. I did not check the data, but the point I wanted to make was if the data is so linear why not use it as if it was linear. Unless the consequences of the simplification is not acceptable.

think your formula is quite OK by the way.

+/- 20 grams:

There is globally a delta of 0.25 Volt per 1000 gram => that means that a 0.01Volt (measurement accuracy) step stands for 40 grams!

Yes i know, but i dont know how these equations are not shown on my excel....

I checked:
your formula presents volts as a function of grams. Exchange the colums and you find something like f(x) = 4131 * x + 502 (rounded to integer values)

AAA ok hehe tnx :). The only thing i dont know is how did you get the equation of the nonlinear, with excel i am only making the equation of the graph when it is linear...

There is globally a delta of 0.25 Volt per 1000 gram => that means that a 0.01Volt (measurement accuracy) step stands for 40 grams!

Hmm...so it has to be accurate since such a small diff in volt results in 40 grams!!!

What do you reckon?
tnx

The difference in the functions is:

grams = 4129.4 volts - 495.53
volts = 0.0002 grams + 0.1225

Looking again at your original data, a large source of error is rounding. It looks like all the values are rounded to an even number in the second decimal, that is to 1 part in 50, or +/-1%. There may in fact be something even more interesting going on with these values than simple rounding too. Look at the sequence of the 1st decimal place in your sequence of measurements: 0, 3, 5, 8, 0, 3, 5, 8, 0, 3, 5, 8. Weird huh?

If we take volts = 2.4209E-4 x grams + 0.120 that gives us a volt/grams curve that varies by about 1% from the measured data -- within the error bars of the data you've given us.

I think the very slightly better R^2 value for the quadratic is spurious and the true relationship is simply linear.