 # Programming parabolic function

Hi,

I am trying to implement a parabolic function in Arduino. The specs I want to create are: starting point: [0,0] middle point: [60,100] end point: [120,0] The function would be: (1/36)(-(X-60)^2)+100 When I add this function as an Arduino code:

((1/36)*((-pow(X-60,2))))+100;

It only gives the outcome: 100, when I print it.. What am I doing wrong?

You are using integer arithmetic. Use floats.

Unfortunately it still doesn’t work…

``````float RTCVar;
float seconds                          =   0;
int unsigned long   millisVar          =   0;

void setup() {
Serial.begin(9600);
}

void loop() {
seconds = floor((millis() - millisVar) / 1000);
RTCVar = ((1/36)*((-pow(seconds-60,2))))+100;
Serial.print(seconds);
Serial.print(" - ");
Serial.println(RTCVar);
delay(200);
}
``````

Your operands are still integers. Use floats. (hint: 1 is an integer, 1.0 is a float)

i.e. 1/36 = 0

1.0/36.0 = .027777777

A little bit of algebra changes that to

``````RTCvar = (120.*seconds - seconds*seconds)/36.;
``````

Pete

The pow() function will bite you in the ass, too. It expects float arguments and produces a float output. Raising a value to a power of two using pow() is incredibly wasteful. A bit shift will be orders of magnitude faster, if the value is a integer. If not, simply multiplying by the value is faster.

Of course, you are storing an integer value in a float, so you will have problems deciding whether to treat seconds as an int or a float.

bit shifting multiplies by 2, it does not square!

If you're squaring an integer, an integer multiply is faster than pow.

That is true, but it is more than a simple bit-shift.

(and pow() might have a trap for integer powers and do a multiply itself.)

el_supremo: A little bit of algebra changes that to

``````RTCvar = (120.*seconds - seconds*seconds)/36.;
``````

Pete

Finishing the job and eliminating one multiply:

``````RTCvar = (120.0 - seconds) * seconds / 36.0 ;
``````

Well indeed, this is a more easy way to create that parabola. Thank you all!!