Hi everyone. I got one question to ask. How to write a coding if I have the y value , then how to find x.
Elementary algebra. Search phrase "quadratic equation"
First check
b*b - 4*a*c >= 0
if false, there is no x that fulfills the equation.
If larger 0, there are two solutions...
Is it for school ?
Perhaps the assignment is to "find" x by searching for it.
I think this is the curve (when x can also be negative):
There are two values for x when the y is known. I would first try to find the bottom, then search for higher values for x and then for lower values for x.
A sketch which is a start to "find" x. It is not complete.
// For: https://forum.arduino.cc/t/basic-mathematical-equationcoding/1073852
// "Find" x by searching for it.
float bottom_x;
float bottom_y;
void setup()
{
Serial.begin(115200);
Serial.setTimeout(100); // 100 ms timeout for serial input
// Find the bottom between -1000 and +1000 with step 1.0
bottom_x = -1000.0;
bottom_y = Calc(bottom_x);
for( float x=-1000.0; x<1000.0; x+=1.0)
{
float y = Calc(x);
if( y < bottom_y)
{
bottom_y = y;
bottom_x = x;
}
}
// Find a more accurate bottom with step 0.001
for( float x=bottom_x-1.0; x<bottom_x+1.0; x+=0.0001)
{
float y = Calc(x);
if( y < bottom_y) // new lower bottom ?
{
bottom_y = y;
bottom_x = x;
}
}
Serial.print( "The bottom of the curve is at x=");
Serial.print(bottom_x);
Serial.print(" (y=");
Serial.print(bottom_y);
Serial.println(")");
Serial.println("Enter a value for y (integer value)");
}
void loop()
{
if( Serial.available() > 0)
{
// using parseInt, even though I don't like that function
long y_int = Serial.parseInt();
while( Serial.available() > 0)
Serial.read(); // remove trailing CR or LF
float y_target = float( y_int);
if( y_target < bottom_y)
{
Serial.println("That value is not in the curve");
Serial.println("Enter a good value for y (integer value)");
}
else
{
// search upward
Serial.print("Searching upward ...");
delay(1000);
Serial.println("... not implemented yet");
// search downward
Serial.print("Searching downward ...");
delay(1000);
Serial.println("... not implemented yet");
Serial.println("Enter a new value for y (integer value)");
}
}
}
float Calc( float x)
{
float y = (0.0227*x*x) - (3.1401 * x) + 177.35;
return( y);
}
Try the sketch in Wokwi simulator:
Formula by @red_car is wrong....
Should be: (after eddit acc. to post #10 and #12)
x1 = (-b + sqrt(b*b - 4*a*c)) / (2*a)
x2 = (-b - sqrt(b*b - 4*a*c)) / (2*a)
for real numbers only.
X equals minus b, plus or minus the square root of, b squared minus 4 *a *c.
ALL over 2a.
And you must check the conditions given by @build_1971 to make sure you have 2 real solutions.
Tom...
Yes agreed... I cut & pasted the wrong thing.
But...
/ 2a ?
Beware that ^
does NOT do exponentiation in C! Try:
x1 =-b + sqrt(b*b - 4*a*c);
You are right! I have updated post #7 accordingly.
You are right! The ^ operator is indeed reserved for or operation in C). In order to prevent further confusion, I have updated post #7 accordingly.
No, it's reserved for the XOR operation.
Error on error...
Thanks for your comment.
... I will stop making errors and do nothing instead...
I guess no one will be deriving the quadratic equation using the method of least squares.
a7
I just curl up in the foetal position in the corner and let it blow over.
Hang on I'll see if any of my High School maths teachers are still on this mortal coil.
Tom..
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.