If you restrict yourself to the linear functions shown above, you can generalize this function as
3x - 5y + z = 5
will become
a[0]*x[0] + a[1]*x[1] + a[2]*x[2] = c
with a = { 3, -5, 1 } and c = 5 .
Ie you need a list (or array) of constant factors List<double> a; and the variable list List<double?> x; plus a constant on the right side of double c;
public double Solve(IList<double> a, IList<double?> x, double c) { int unknowns = 0; int unkonwnIndex = 0;
Example:
3x - 5y + z = 5 5 - (- 5y + z) x = -------------- 3
As you can see from the example, the solution consists of subtracting the sum of all members except the unknown member from the constant, and then dividing by the coefficient of the unknown. Therefore, my decision remembers the index of the unknown.
You can generalize with such powers, considering that you have an equation
a[0]*x[0]^p[0] + a[1]*x[1]^p[1] + a[2]*x[2]^p[2] = c
you need an additional parameter IList<int> p , and the result will be
return Math.Pow((c - sum) / a[unknownIndex], 1.0 / p[unknownIndex]);
as x ^ (1/n) equals nth-root(x) .
If you use doubling for authority, you can even represent functions such as
5 7*x^3 + --- + 4*sqrt(z) = 11 y^2 a = { 7, 5, 4 }, p = { 3, -2, 0.5 }, c = 11
because
1 x^(-n) = --- x^n
and
nth-root(x) = x^(1/n)
However, you cannot find the roots of true nonlinear polynomials of type x^2 - 5x = 7 . The algorithm shown above only works if the unknown appears exactly once in the equation.