Help to solve a mathematical equation

Hello,

I'm a french arduino user,
I try to solve a mathematical equation
This one:
— x will be a variable
— y the solution

I've read the arduino reference but i don't understand :frowning:
In the arduino language, i have sqrt(), pow() but i don't known how i can add the little "3" (linked to the square root) ?

Thanks a lot
Have a good day
Goos

The square-root symbol with the 3 over it means cube root. Raising to the 1/3 power is the same.

float y(float x)
   {
   return 8.0 * pow(x, 1.0/3.0) - 1.3;
   }

Or you could use logs.

Oh, and depending on how far that cube root symbol was supposed to extend the answer might be

float y(float x)
   {
   return 8.0 * pow(x-1.3, 1.0/3.0);
   }

Or you could use logs.

Only if x > 0 or you should handle the sign separately!!

float y(float x)
{
  if (x == 0.0) return -1.3;
  int sign = -1 * (x < 0.0 );  // true == 1 false == 0
  return  = sign * 8 * exp (log(sign * x) / 3) - 1.3;
}

robtillaart:

Or you could use logs.

Only if x > 0 or you should handle the sign separately!!

Now, let's not lose our heads here!

For standard C and C++ math library functions (including avr-libc implementations), a domain error occurs if the argument of the log function is negative, and a domain error occurs if the first argument of the pow function is finite and negative and the second argument is finite and not an integer. (The functions return the floating point constant nan---means "Not a Number".)

There's a real good reason for these things: The real-valued mathematical functions are not defined for these arguments. Finding the log of the absolute value of a negative number and then attaching the minus sign in a program is not a recipe for success, I'm thinking.

Regards,

Dave

Footnote:
Although the mathematical definition of the power of a positive number is defined in terms of exponential and log functions, why would a programmer call two functions when the single call to the pow() function does the job?

Furthermore, in general...

The pow() function can calculate the power of a negative base to an integer power, but using the log() does not compute for these cases.

Hi Dave,

Very true what you said but It depends (as allways).

The cubic root (CR) from -8 = -2 because -2 * -2 * -2 = -8 . So solutions for CR exists. For every number in R- the CR is an element from R- and for every number in R+ the CR is an element from R+. Furthermore abs (CR(x)) == abs(CR(-x))

// R- = all negative real numbers

Because the log() function cannot handle negative values it is valid and mathematically correct (in the case of cubic root) to do the math in the R+ domain and correct the sign afterwards. In fact this trick is mathematically valid with all uneven roots. (uneven numbers are integer by definition)

I agree that this kind of "tricks" with math are more error sensitive, one must understand the math behind the code and document it well - which I didn't - to prevent introducing bugs when the code is upgraded or changed.

Although the mathematical definition of the power of a positive number is defined in terms of exponential and log functions, why would a programmer call two functions when the single call to the pow() function does the job?

Normally if pow() can do the job, stick with pow(),

  • unless you need to have control over how the calculation is made e.g. because of the numerical stability.

Thanks a lot for all your reply
I refined my equation and i have test in arduino ... it work fine, but inverted :frowning:

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

void loop()
{

float result=0;
for (float i=0; i <= 256; i++ ){

result = 25.0 * pow(i, 1.0/3.0) - 16.0397;

Serial.print(i);
Serial.print(" - ");
Serial.print(result);
Serial.println(" ");

delay(100);

}
}

First curve (the false)

The second curve (the right)

My curve work between 0 > 100 in the X and Y axis,
I need it beetween 1000 in X and 256 in Y but i don't known how ...

The two curves (normal & inverted)

I think I need this equation () but I fail to turn it into y= ...

You are very nice,
Thank you very much for your answers.

Good evening
Goos

Your post is not clear and I think (at least) two images are missing?

Sorry, i have changed my picture hoster :cold_sweat:
Its more clear now ?

Thanks !

Goos:
I think I need this equation () but I fail to turn it into y= ...

Well...

If x = 25*cube root(y) - 16.0397

Then...
cube root(y) = (x + 16.0397) / 25, right?

So...

y = ((x + 16.0397)/25) to the power 3, right?

Then couldn't the code be

y = pow((x+16.0397)/25.0, 3.0);

Regards,

Dave

looking at the graphs Dave's formula is the correct one.

As calculation is an exact integer power the formula could also be written without the pow() function

float y(float x)
{
  float val = (x + 16.0397)/25;
  return val * val * val;
}

What still is not clear to me is your following phrase, what do you mean by it?

I need it beetween 1000 in X and 256 in Y but i don't known how ...

Maybe you can post the whole code ?

Goos:
My curve work between 0 > 100 in the X and Y axis,
I need it beetween 1000 in X and 256 in Y but i don't known how ...

The Arduino program is generating data for 0..256. I think the program you are using to generate the graph is limiting the output range. Perhaps there is a way to override the default dimensions of the graph.

Thank you all ! 8)

You are very friendly.
I do not have time to continue to hardwork on before next weekend because
i have many things to do this week but I'll let you know as soon as possible !

I'll post the final code soon (although I'm not a professional) !

I have test with this two solutions, for looking the returned value by serial:
Booth work great
— The range is now between 0 ... 256 (on X & Y axis)

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

void loop()
{

  float val=0;
   for (int i=0; i <= 256; i++ ){

float result = (i + 17)/42.9948; // robtillaart solution
float val = result * result * result; // robtillaart solution

Serial.print("Level > ");
Serial.print(i);
Serial.print(" - Value > ");
Serial.print(val);
Serial.println(" ");


delay(10);

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

void loop()
{

  float val=0;
   for (int i=0; i <= 256; i++ ){

val = pow((i+17)/42.9948, 3.0); // davekw7x solution

Serial.print("Level > ");
Serial.print(i);
Serial.print(" - Value > ");
Serial.print(val);
Serial.println(" ");


delay(10);

   } 
}

Have a good day

Hi Goos

I try to solve a mathematical equation
This one:
— x will be a variable
— y the solution

Root = 2197/512000

But somehow I don't think that is/was what you were looking for.

Please be aware that the next equation (after isolating x):

f(x) = ((x+16.0397)/25)^3

will pass the 1000 limit at x = 234 since f(234) = 1000.476...

You did say somthing about limiting the equation.

-Fletcher