public double DfDx(TwoDArrayFunctionWithTwoDArrayOfVariablesWithArguments Func, object Arguments, double[,] x, int FuncIndex1, int FuncIndex2, int xIndex1, int xIndex2, double h) { int n1 = x.GetLength(0); int n2 = x.GetLength(1); double[,] XPrev = new double[n1, n2]; double[,] XNext = new double[n1, n2]; double Prev, Next; for (int i = 0; i < n1; i++) { for (int j = 0; j < n2; j++) { XPrev[i, j] = x[i, j]; XNext[i, j] = x[i, j]; if (i == xIndex1) { if (j == xIndex2) { XPrev[i, j] -= h; XNext[i, j] += h; } } } } Prev = Func(XPrev, Arguments)[FuncIndex1, FuncIndex2]; Next = Func(XNext, Arguments)[FuncIndex1, FuncIndex2]; return((Next - Prev) / (2.0D * h)); }
public double[,,,] DfDx(TwoDArrayFunctionWithTwoDArrayOfVariablesWithArguments Func, object Arguments, double[,] x) { ///<sumary> /// retruns an array with all derivatives of the function /// </summary> int n1 = x.GetLength(0); int n2 = x.GetLength(1); int m1 = Func(x, Arguments).GetLength(0); int m2 = Func(x, Arguments).GetLength(1); double[,,,] D = new double[m1, m2, n1, n2]; for (int i = 0; i < m1; i++) { for (int j = 0; j < m2; j++) { for (int k = 0; k < n1; k++) { for (int l = 0; l < n2; l++) { D[i, j, k, l] = DfDx(Func, Arguments, x, i, j, k, l); } } } } return(D); }
public double DfDx(TwoDArrayFunctionWithTwoDArrayOfVariablesWithArguments Func, object Arguments, double[,] x, int FuncIndex1, int FuncIndex2, int xIndex1, int xIndex2, double InitialStepSize, double Precision) { double precision = Precision; // precision to evaluate derivative double h = InitialStepSize; // initial step size double c = 10.0D; // factor do divide h (i.e., h_new =h_old/c) double eps = 1.0E-100D; // small number int maxSteps = 100; // maximum times to divid h double devOld, devNew; // old derivative double calcPrecision; // new derivative // calculate initial derivative devOld = this.DfDx(Func, Arguments, x, FuncIndex1, FuncIndex2, xIndex1, xIndex2, h); devNew = devOld; for (int i = 0; i < maxSteps; i++) { // reduce h h /= c; // calculate new derivative devNew = this.DfDx(Func, Arguments, x, FuncIndex1, FuncIndex2, xIndex1, xIndex2, h); //calculate precision if (Math.Abs(devNew) > eps) { calcPrecision = Math.Abs((devNew - devOld) / devNew); } else { // don't divide by devNew if devNew is small calcPrecision = Math.Abs(devNew - devOld); } // stop if precision is met if (calcPrecision < precision) { //Console.Write("number of steps = "+i.ToString()+"\n"); //Console.Write("Calculated precision = "+calcPrecision.ToString()+"\n"); return(devNew); } devOld = devNew; } return(devNew); }
public double DfDx(TwoDArrayFunctionWithTwoDArrayOfVariablesWithArguments Func, object Arguments, double[,] x, int FuncIndex1, int FuncIndex2, int xIndex1, int xIndex2) { return(DfDx(Func, Arguments, x, FuncIndex1, FuncIndex2, xIndex1, xIndex2, 1.0E-2D, 1.0E-14D)); }