该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
下面的代码是我刚才无聊写的。对于简单的一元多次方程的迭代
#include
#include
#include
#define MAXTIMES 5
typedef int times;
typedef double coefficient;
typedef struct _properties
{
coefficient x; //系数
times n;//次数
}properties; typedef properties equation_expression ;
equation_expression equ[MAXTIMES];
double diff(double x)
{
double ds = 0.0 ;
for(int i = 0 ;i< MAXTIMES ;i++)
{
if(equ[i].n ==0 )
continue;
else{
double xn = 1.0;
for(int j =0 ;j
xn *= x;
ds += equ[i].x *equ[i].n* xn;
}
}
return ds;
}
double equvalue(double x)
{
double ds = 0.0 ;
for(int i = 0 ;i< MAXTIMES ;i++)
{
if(equ[i].n ==0 )
ds += equ[i].x;
else
{
double xn = 1.0;
for(int j =0 ;j
xn *= x;
ds += xn * equ[i].x;
}
}
return ds;
}
//
void buildequfunction()
{
printf("input data like this a ,b a is coefficient b is times of equation\n");
//sorry dont realize; this time just do ax^2+ bx +c = 0
equ[0].x = 2.0;
equ[0].n = 2;
equ[1].x = 7.0;
equ[1].n = 1;
equ[2].x = 3.0;
equ[2].n = 0;
equ[3].x = 3.0;
equ[3].n = 3;
//this 3*x^3 +2x^2 + 7x +3 = 0
}
int main (void) {
memset(equ,0,sizeof(equ));
buildequfunction();
double error_control = 0.0000000001 ;
double _begin, _end ;
//so you should give a data to begin the game.and some times it will not work.
scanf("%lf",&_begin);
do{
_end = _begin - equvalue(_begin)/diff(_begin);
}while( (fabs(_end -_begin)> error_control)&& (_begin = _end));
printf("one %lf \n",_end);
}