本文整理汇总了C#中IFunction.Derivative方法的典型用法代码示例。如果您正苦于以下问题:C# IFunction.Derivative方法的具体用法?C# IFunction.Derivative怎么用?C# IFunction.Derivative使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IFunction
的用法示例。
在下文中一共展示了IFunction.Derivative方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TryToFindRoot
public static bool TryToFindRoot(IFunction f, double start, double end, double guess, out double x) {
ValidateArg.IsNotNull(f, "f");
System.Diagnostics.Debug.Assert(start >= f.ParStart && end <= f.ParEnd);
System.Diagnostics.Debug.Assert(start <= guess && end >= guess);
int numberOfBoundaryCrossings = 0;
const int maxNumberOfBoundaryCrossings = 10;
int numberOfTotalReps = 0;
const int maxNumberOfTotalReps = 100;
x = guess;
double dx;
bool abort = false;
do {
var fp = f.Derivative(x);
if (Math.Abs(fp) < ApproximateComparer.Tolerance) {
abort = true;
break;
}
dx = -f[x] / fp;
x += dx;
if (x < start - ApproximateComparer.DistanceEpsilon) {
x = start;
numberOfBoundaryCrossings++;
} else if (x > end + ApproximateComparer.DistanceEpsilon) {
x = end;
numberOfBoundaryCrossings++;
}
numberOfTotalReps++;
abort = numberOfBoundaryCrossings >= maxNumberOfBoundaryCrossings ||
numberOfTotalReps >= maxNumberOfTotalReps || dx == 0;
} while (Math.Abs(dx) >= ApproximateComparer.Tolerance && !abort);
if (abort) {
//may be the initial guess was just OK
if (Math.Abs(f[guess]) < ApproximateComparer.DistanceEpsilon) {
x = guess;
return true;
}
return false;
}
if (x < start)
x = start;
else if (x > end )
x = end;
return true;
}