 # Programming an almost linear graph

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.

[quote author=Daniel Formosa link=topic=57745.msg415242#msg415242 date=1302113136] with excel i am only making the equation of the graph when it is linear [/quote]

The equation will show for "polynomial" too, at least in any version of Excel I have used for many years.

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?

No, why ? if you look at the delta's between measurement you see that it increases ~1Volt per 4 KG so after 4 steps it repeats ...

Hehe yes you are right looking at the sequence of the 1st decimal place in the sequence of measurements: 0, 3, 5, 8, 0, 3, 5, 8, 0, 3, 5, 8. And since the last value is 4.62 setting it to 4.82 will make it even more linear....

It is supposed to be a linear relationship i think, even the opamp i am using costs a bit in order to give linear results under large amplifications, since the orignal voltage are mV and these have been amplified to V having a gain of 1000 and more.

Thank you for the advice, i think I will use it as linear. I will mention all these comments (errors) in my thesis tnx :)

It looks like your last value may be an outlier, as you suggest. Can you add one more bag of sand, to see if it is an end condition that affects the higher values? Or do you bump up against a voltage limit if you add another bag?

Ye adding another bag would result in a voltage limit of 5V since the opamp i am using, works in such a way; giving it a +5V and -5V will limit it to +5V, just as i want....

That makes sense. Were it me, though, I'd add one more weight and see what happens. See if it pins your values at 5 Volts. Or if what you are seeing with your last value now is some sort of nonlinearity as the value approaches 5 Volts.

All in all, though, your results do look very linear. Should be no problem using that linear equation. As was mentioned, using a quadratic function would probably be less accurate, not more.

robtillaart:
No, why ?
if you look at the delta’s between measurement you see that it increases ~1Volt per 4 KG so after 4 steps it repeats …

I see, you’re right. There’s nothing fishy about the first digit.

But there is something odd about the data. If you look at the sample to sample voltage delta, it does follow a strange pattern:

``````Grams	Measured	Delta
0	0.120
1035	0.380	0.260
2035	0.580	0.200
3035	0.840	0.260
4035	1.080	0.240
5035	1.340	0.260
6035	1.580	0.240
7035	1.840	0.260
8035	2.080	0.240
9035	2.340	0.260
10035	2.540	0.200
11035	2.800	0.260
12035	3.040	0.240
13035	3.300	0.260
14035	3.540	0.240
15035	3.800	0.260
16035	4.000	0.200
17035	4.260	0.260
18035	4.500	0.240
19035	4.620	0.120
``````

The delta is mostly 0.20, 0.24 or 0.26. But what are the 0.20 steps? Simple rounding doesn’t feel like it would account for this.

I am not an expert on robust statistics, but some messing around with the data gives me a slope of 4130 grams/volt. This is based on calculating the slope based on all the combination of points provided and choosing the median. Plugging this back in and solving for the intercept yields a median intercept of 0.127 (and this is basically what least-squares regression says anyhow). So a computed value of volts = grams/4130 + 0.127 fits with R^2 = 0.9997. Rounding this computed value to 1/50th gives a pretty exact model for what the provided measurements are:

``````Grams	Rounded	Delta
0	0.120
1035	0.380	0.260
2035	0.620	0.240
3035	0.860	0.240
4035	1.100	0.240
5035	1.340	0.240
6035	1.600	0.260
7035	1.840	0.240
8035	2.080	0.240
9035	2.320	0.240
10035	2.560	0.240
11035	2.800	0.240
12035	3.040	0.240
13035	3.280	0.240
14035	3.520	0.240
15035	3.760	0.240
16035	4.020	0.260
17035	4.260	0.240
18035	4.500	0.240
19035	4.740	0.240
``````

The points where the measured value steps by only 0.20 bother me, and I can’t help guessing that there is some other quantization error (other than rounding) going on. The difference between the step of 0.20 and the expected ~0.242 is an error of 0.042V or just over 170g.

Here’s a chart showing the raw data, the curve I fit, the apparent error in the raw data vs a linear regression and the quantization error vs linear regression caused by numerical rounding to 0.02. Make of it what you will. [quote author=Daniel Formosa link=topic=57745.msg415263#msg415263 date=1302114467] Hehe yes you are right looking at the sequence of the 1st decimal place in the sequence of measurements: 0, 3, 5, 8, 0, 3, 5, 8, 0, 3, 5, 8. And since the last value is 4.62 setting it to 4.82 will make it even more linear....

It is supposed to be a linear relationship i think, even the opamp i am using costs a bit in order to give linear results under large amplifications, since the orignal voltage are mV and these have been amplified to V having a gain of 1000 and more.

Thank you for the advice, i think I will use it as linear. I will mention all these comments (errors) in my thesis tnx :) [/quote]

You could use two op-amps... it would make the circuit a bit more complex, but you could also use them to filter the signal... if needed.

I think the best is to assume it as a linear load cell... What are the rated limits of the load cell? It is quite normal to see the load cell have some linearity problems in the beginning and end of the scale (like my teacher said: "Linearity is an illusion"). But with a 10 bit ADC, you'll probably get a bigger error from that.

What is the precision of the reading you're planning to have? 1,2,3 decimal places?

Since you have all the gear you need either way, do this. Program the Arduino to use a linear scale. Use one of the equations you found. Then compare readings with the scale you have and different values of sand and plot the results. You can then check if the "non-linearity", is really affecting your measurements or not.

From experience, having an R-squared of 0.9995 is as good as one. Especially considering you're using 10 bits...

Since nobody else picked up on this...

Daniel Said in an early post:

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

"[u]Good[/u]" is not an acceptable engineering or statistical term.

Saying that scale is accurate on a 1Kg measurement to +- 0.05 grams -- or whatever is acceptable.

When you are dealing with numbers....

The Relative error adds absolutely The absolute error adds absolutely...

Is the scale error given as a percent? Is it given as +- so many grams...

Regardless ... if each bag is off by 2 grams... then adding five bags gives an error of +- 10 grams...

If each bag is off by 2% -- because that is the rating on the scale... then if you have 5 bags in a bucket -- what is the error? (HINT: It is not 2%.)

If you always use the same bags for your 5Kg weight -- then you help to "constrain" the error.

If you always "randomize" the bags that is another issue.

If you choose the bags in a pattern -- that is another issue again.

Basic stats and engineering principles...

In your calculations -- if you multiply or divide -- the percentage error (relative error) adds absolutely. In additions or subtractions -- the absolute error adds absolutely.

[u]In something like this you need to calculate the error at the same time you calculate your answer.[/u]

Edit: You can verify this by getting (access to) a weigh scale which can measure different combination of 5Kg bags -- you will likely get some different weights.

What I left out as well is another statistic about a measuring device... the repeatability. If you make the same measurement 10 times -- do you always get the same answer.

I have done this exercise many times in industrial and scientific environments... They are not trivial issues.

Best wishes...

Well, the electronic scale used to put 1kg of sand wasn’t that accurate. It was just a weighing scale bought from ebay used for other purposes, it doesn’t have a datasheet or anything…so that might be the problem as well. Fortunately it seems to be a sensitive weighing scale since it displays a change in 1 gram for example but it aint specific.

tnx for your interest. tomorrow il set up anoither experiment and take the readings again.

There's a big difference between precision and accuracy...

You can buy (Or borrow) a set of standard weights used to calibrate scales.

They should be easy to find in the university. Getting them might be another issue as labs don't like to loan equipment.

Doing all this work and pretending that you knew the weights of the bags was not a great use of time. :P

Bringing the scale to the weights, calibrating the scale, and then returning it to where you work might upset the calibration.

You really need to have the calibration weights handy to test the scale as you proceed through the weighing session.

WillR: Getting them might be another issue as labs don't like to loan equipment.

There's a good reason for that...

Also, don't forget, if you have a plate on the scale, the measured value should remain the same regardless of where you set the weight. I have no idea of the name of this test... but it is used in scales verification and calibration too.