展开全部
namespace 二分法求方程解
{
class Program
{
static void Main(string[] args)
{
double err = 0.0000001;//这个值越小算出来的越精确
double result = ErFen(1.0, 2.0, err);
Console.WriteLine(result);
}
static double ErFen(double a, double b, double err)
{
double c = (a + b) / 2;
while (Math.Abs(f(c)) > err)
{
if (f(a) * f(c) < 0)
{
b = c;
}
if (f(c) * f(b) < 0)
{
a = c;
}
c = (a + b) / 2;
}
return c;
}
static double f(double x)
{
return x * x - 2;
}
}
}
{
class Program
{
static void Main(string[] args)
{
double err = 0.0000001;//这个值越小算出来的越精确
double result = ErFen(1.0, 2.0, err);
Console.WriteLine(result);
}
static double ErFen(double a, double b, double err)
{
double c = (a + b) / 2;
while (Math.Abs(f(c)) > err)
{
if (f(a) * f(c) < 0)
{
b = c;
}
if (f(c) * f(b) < 0)
{
a = c;
}
c = (a + b) / 2;
}
return c;
}
static double f(double x)
{
return x * x - 2;
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询