用迭代法和牛顿法求解方程x=e^x在x=0.5附近的一个根,要求精确到小数点后三位
展开全部
x=0.57224982960923
程序如下:
using System;
namespace Test
{
class Program
{
static void Main(string[] args)
{
double a =0.4;
double b =0.6;
double c = 0.0;
double d = 0.0;
while(true)
{
if (Function(a) * Function(b) < 0)
{
c = 0.5 * (a + b);
d = Function(c);
}
else
{
System.Console.WriteLine("请调整根区间……");
break;
}
if (d*Function(a) > 0)
{
a = c;
}
if (d* Function(b) > 0)
{
b = c;
}
if (Math.Abs(d) <= 1e-15)
{
System.Console.WriteLine(c);
break;
}
}
System.Console.ReadKey();
}
static double Function(double x)
{
return FunctionExp(x);
}
static double FunctionExp(double x)
{
return Math.Exp(x) - x-1.2;
}
}
}
程序如下:
using System;
namespace Test
{
class Program
{
static void Main(string[] args)
{
double a =0.4;
double b =0.6;
double c = 0.0;
double d = 0.0;
while(true)
{
if (Function(a) * Function(b) < 0)
{
c = 0.5 * (a + b);
d = Function(c);
}
else
{
System.Console.WriteLine("请调整根区间……");
break;
}
if (d*Function(a) > 0)
{
a = c;
}
if (d* Function(b) > 0)
{
b = c;
}
if (Math.Abs(d) <= 1e-15)
{
System.Console.WriteLine(c);
break;
}
}
System.Console.ReadKey();
}
static double Function(double x)
{
return FunctionExp(x);
}
static double FunctionExp(double x)
{
return Math.Exp(x) - x-1.2;
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<stdio.h>
#include<math.h>
double f(double x)
{
return (exp(x)-x);
}
double f1(double x)
{
return (exp(x)-1);
}
void main()
{
double x=0.5,x1,eps=1e-6;
do
{
x1=x;
x=x1-f(x1)/f1(x1);
}while(fabs(f(x))>=eps||fabs(x-x1)>=eps);
printf("x=%.3lf\n",x);
}
#include<math.h>
double f(double x)
{
return (exp(x)-x);
}
double f1(double x)
{
return (exp(x)-1);
}
void main()
{
double x=0.5,x1,eps=1e-6;
do
{
x1=x;
x=x1-f(x1)/f1(x1);
}while(fabs(f(x))>=eps||fabs(x-x1)>=eps);
printf("x=%.3lf\n",x);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询