Fitting a complex curve

is it possible to fit a quartic regression 4rth order with arduino ?

y = e-dx+cx^2 +bx^3+ax^4

y=3.99915 - 0.03974209x + 0.0001509082x^2 - 0.0000002300598x^3 + 0.0000000001262231x^4

Of course it is...

Regards,
Ray L.

You may have trouble preserving this level of precision (0.0000000001262231) with an Arduino float.

Why do you want to fit a curve using Arduino? Give us the big picture.

Many arduinos (e.g. Uno) only have six digit precision in floats.

I Made a current sensor using an LM358 and a SHUNT resistor for my back up power supply.

following are the values that i received after loading .

ADC Ampere
224 0.4
412 0.8
535 1.0
607 1.2
672 1.4
729 1.6
734 1.8
745 2.0

am trying to find an equation to fit the ADC>Ampere



curve.png

plotted those values in excel and looks to me a 3rd order curve fits nicely with coefficients within the arduino float realm! :slight_smile:

Capture.PNG

alternatively...

why not just create a lookup table for current vs ADC and use that on your arduino?

Capture.PNG

@sibin_lal, what are you using for a voltage reference? (Please say it is not USB power.)

@Coding Badly i use a Arduino NANO which is powered by an external 9V linear regulator . i guess the voltage reference still is 5v . i do not have any external reference.

@sherzaad thank you :slight_smile: 3rd degree would do but I still don't have a fair algorithm. :confused:

sibin_lal:
following are the values that i received after loading .

Are those readings reproducible?

@Coding Badly yes they evidently seems to be reproducing the values :slight_smile: the curve is stable

sibin_lal:
@sherzaad thank you :slight_smile: 3rd degree would do but I still don't have a fair algorithm. :confused:

something like this you mean?

void setup() {
  uint16_t adc_read;
  float calc_current;

  Serial.begin(115200);

  analogRead(A0);
  adc_read = analogRead(A0);

  //calc current (mA) = (0.6715*adc_read)-(0.0001*(adc_read^2))
  calc_current = (0.6715 - (0.0001 * adc_read)) * adc_read;

  Serial.print("calc current (mA): ");
  Serial.println(calc_current);
}

void loop() {
  // put your main code here, to run repeatedly:

}

I've replotted the value for ADC vs I this time and to me a quadratic seems to do the job! :slight_smile:

Blackfin:
You may have trouble preserving this level of precision (0.0000000001262231) with an Arduino float.

= 1.262231 x10-10

Looks fine to me. 7 digits of precision. Maybe the last digit might get mangled by a single-precision float.

sherzaad:
I've replotted the value for ADC vs I this time and to me a quadratic seems to do the job! :slight_smile:

No residuals?

I Made a current sensor using an LM358 and a SHUNT resistor

If that is at all nonlinear, something is wrong. Your amplifier design could be wrong, the resistor is heating up, etc.

Post a schematic diagram with power supply details, component values and ratings (hand drawn, not Fritzing). Note that the LM358 is not a rail-to-rail amplifier and the common mode input voltage may not exceed Vcc-2 V.

sherzaad:
something like this you mean?

void setup() {

uint16_t adc_read;
  float calc_current;

Serial.begin(115200);

analogRead(A0);
  adc_read = analogRead(A0);

//calc current (mA) = (0.6715adc_read)-(0.0001(adc_read^2))
  calc_current = (0.6715 - (0.0001 * adc_read)) * adc_read;

Serial.print("calc current (mA): ");
  Serial.println(calc_current);
}

void loop() {
  // put your main code here, to run repeatedly:

}




I've replotted the value for ADC vs I this time and to me a quadratic seems to do the job! :)

![Capture.PNG|758x296](upload://mFg3bdXg7wEzcZP3XVuWQyZF8EM.png)

Thank you sherzaad sorry for the late replay .

with the code you suggested am getting non-variable values

1.5 to 2 amps = 2.01
0.6 to 1.5 amps =1.34
and
less than 0.6 =0.67

i am thinking about removing the (inaccurate) load current value from display and go for an over current disconnect .

jremington:
If that is at all nonlinear, something is wrong. Your amplifier design could be wrong, the resistor is heating up, etc.

Post a schematic diagram with power supply details, component values and ratings (hand drawn, not Fritzing). Note that the LM358 is not a rail-to-rail amplifier and the common mode input voltage may not exceed Vcc-2 V.

probably its the circuit .



R SHUNT.jpg

The circuits is not good. Explanation here

need a small reference voltage to pedestal the output to a known value greater than the amplifier’s minimum output swing.

I'd add, that small reference required to shift up both inputs above specified Input offset voltage of the OPA (LM358).