Solving a simultaneous equation through code

Viewed 13813

This seems like an incredibly simple and silly question to ask, but everything I've found about it has been too complex for me to understand.

I have these two very basic simultaneous equations:

X = 2x + 2z
Y = z - x

Given that I know both X and Y, how would I go about finding x and z? It's very easy to do it by hand, but I have no idea how this would be done in code.

5 Answers

@Alexandre , you missed one condition.. Here is the final code

void SolveLinearEquations (float a,float b,float c,float d,float u,float v, float &x, float &y)
{
float f;
float g;
if (abs(a) > abs(c))
{
     f = u * c / a;
     g = b * c / a;
     y = (v - f) / (d - g);
     if(c != 0)
        x = (f - g * y) / c;
     else
        x = (u - b * y)/a;
}
else
{
    f = v * a / c;
     g = d * a / c;
     x = (u - f) / (b - g);
     if (a != 0)
        y = (f - g * x) / a ;
     else
        y = (v - d * x)/c;
}
}
Related