I have a function called Bisection method that Accepts 4 parameters , delegate of a function , start and end of interval and user guess of the solution. Here is the function:
public static double Bisection_method (MyFun fun , double start , double
end, double? guess)
{
if ( fun(start) * fun(end) > 0 )
{
Console.WriteLine("wrong Entry");
return -1;
}
double avg,tolerance,sign;
avg = (guess.HasValue) ? guess.Value : ( (start + end) / 2 );
do
{
tolerance = Math.Abs ( fun(end) - fun(start) );
sign = fun(start) * fun(avg);
if (sign < 0)
end = avg;
else if (sign > 0)
start = avg;
else
{
if (fun(start) == 0)
return start;
else return end;
}
avg = (start + end) / 2;
}
while ( tolerance > 0.0001 );
return end;
}
Now there is some cases I want to handle:
1- Can we enter the start of the interval a negative number ? and if so, how can we handle sqrt ?
2- If the user enters the interval start from zero, and the function I'm passing to the delegate has Ln() or Log(), how can we handle this ?