编程采用牛顿法求方程编程采用牛顿法求方程2X^4-3X^3+4X^2-6X+3=0在X1.2附近的根
展开全部
解为1
/*=======================================================
*Author :wacs5
*Date :20081209(YYYYMMDD)
*Function :牛顿迭代法求方程的根
*=======================================================*/
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define MAX_DIEDAI_TIME 200
main()
{
int n=0;
double x=1.2; /*初值*/
double jingdu=1e-6; /*精度*/
double function(double x);
double d2function(double x);
double newton_diedai(double x0,int *n,double jingdu);
system("cls");
x=newton_diedai(x,&n,jingdu);
printf("x=%.7lf\ty=%.7lf\n",x,function(x));
getch();
return 0;
}
/*====================
想要求解的方程的表达式
======================*/
double function(double x)
{
return 2*x*x*x*x-3*x*x*x+4*x*x-6*x+3;
}
/*===========================
想要求解的方程的表达式的导数
=============================*/
double d2function(double x)
{
return 8*x*x*x-9*x*x+8*x-6;
}
/*=============================================
牛顿迭代法解方程组的解
x0为迭代的初值,n为迭代次数,jingdu为精度
function为求根代数式,d2functoin为其导数
返回最终符合一定精度的根
*/
double newton_diedai(double x0,int *n,double jingdu)
{
double x,temp;
temp=d2function(x0);
if (fabs(temp)>1e-10) /*防止除数为0*/
{
x=x0-function(x0)/temp;
printf("n=%d\tx=%.5lf\n",*n,x);
}
else
{
printf("error:div 0:\nPress any key to exit:");
getch();
exit(1);
}
if (++(*n)>MAX_DIEDAI_TIME)
{
printf("diedai time:%d > MAX_DIEDAI_TIME:\nPress any key to exit:",*n);
getch();
exit(1);
}
temp=function(x);
if (fabs(temp)<jingdu)
return x;
else
return newton_diedai(x,n,jingdu);
}
/*=======================================================
*Author :wacs5
*Date :20081209(YYYYMMDD)
*Function :牛顿迭代法求方程的根
*=======================================================*/
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define MAX_DIEDAI_TIME 200
main()
{
int n=0;
double x=1.2; /*初值*/
double jingdu=1e-6; /*精度*/
double function(double x);
double d2function(double x);
double newton_diedai(double x0,int *n,double jingdu);
system("cls");
x=newton_diedai(x,&n,jingdu);
printf("x=%.7lf\ty=%.7lf\n",x,function(x));
getch();
return 0;
}
/*====================
想要求解的方程的表达式
======================*/
double function(double x)
{
return 2*x*x*x*x-3*x*x*x+4*x*x-6*x+3;
}
/*===========================
想要求解的方程的表达式的导数
=============================*/
double d2function(double x)
{
return 8*x*x*x-9*x*x+8*x-6;
}
/*=============================================
牛顿迭代法解方程组的解
x0为迭代的初值,n为迭代次数,jingdu为精度
function为求根代数式,d2functoin为其导数
返回最终符合一定精度的根
*/
double newton_diedai(double x0,int *n,double jingdu)
{
double x,temp;
temp=d2function(x0);
if (fabs(temp)>1e-10) /*防止除数为0*/
{
x=x0-function(x0)/temp;
printf("n=%d\tx=%.5lf\n",*n,x);
}
else
{
printf("error:div 0:\nPress any key to exit:");
getch();
exit(1);
}
if (++(*n)>MAX_DIEDAI_TIME)
{
printf("diedai time:%d > MAX_DIEDAI_TIME:\nPress any key to exit:",*n);
getch();
exit(1);
}
temp=function(x);
if (fabs(temp)<jingdu)
return x;
else
return newton_diedai(x,n,jingdu);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询