I am testing the Extreme Optimization C# library, precisely the nonlinear system solvers. As an example I find that I have to pass the solver the nonlinear system in the following shape:
Func<Vector, double>[] f = {
x => Math.Exp(x[0])*Math.Cos(x[1]) - x[0]*x[0] + x[1]*x[1],
x => Math.Exp(x[0])*Math.Sin(x[1]) - 2*x[0]*x[1]
};
The problem is that the system I try to solve cannot be specified at design time. It is a nonlinear system composed by the load flow equations to solve an electrical circuit of altern current (AC). The equations are composed by a number of variables that depend of the number of nodes in the grid, which is especified by the user, the equations are these:
So basically I have 2 equations per node, this is 2*n equations, which cannot be composed in a single line bacause they depend of i,j indices, therefore I have to have 2 nested for loops to create the P and Q equations.
Is there a way to create Func<Vector, double>[] f = { equation system of variable lenght };
?
I have seen the post Creating a function dynamically at run-time, but it doesn't answer my quetion (I believe)
//************************EDIT**************************************
The creation of the equations is something like this:
For (int i=0; i< n; i++){
double A=0.0;
double B=0.0;
For (int j=0; j< n; j++){
A+= G[i,j]*Math.Cos(Theta[i,j]) + B[i,j]*Math.Sin(Theta[i,j])
B+= G[i,j]*Math.Sin(Theta[i,j]) + B[i,j]*Math.Cos(Theta[i,j])
}
P[i]=V[i]*A;
Q[i]=V[i]*B;
}
Ofcourse A and B contain the variables, and this loop formulation as it is makes little sense.
Thanks in advance.