📄 newton.cpp
字号:
#include "stdio.h"
#include "math.h"
#define N 10000
int flag=1;
double Function(double x);
double Newton(double x0,double e)
{
int counter=0;
double x,y0,y1;
double temp=e+1;
flag=1;
while(temp>e)
{
if(counter>N)
{
flag=0;
printf("Newton序列发散\n");
break;
}
y0=Function(x0);
y1=Function(x0-y0);
x=x0-pow(y0,2)/(y0-y1);
temp=fabs(x-x0);
x0=x;
counter++;
}
return x;
}
double Function(double x)
{
double y=pow(x,3)/3-x;
return y;
}
/*
void main()
{
int i=1;
double x0,x,delt;
double e=0.5*pow(10,-5);
while(flag)
{
x0=0.01*i;
x=Newton(x0,e);
if(fabs(x)>e) flag=0;
if(flag)
{
delt=0.01*i;
printf("初值为%.2f时,解得x=%f\n",delt,x);
i++;
}
}
delt=0.01*i;
printf("使Newton序列收敛于根x2的最大区间为:(-%f,%f)\n",delt,delt);
}*/
void main()
{
double x0[7]={-100,-5,-0.7,0.3,0.7,7,100};
double e=0.5*pow(10,-5);
double x;
for(int i=0;i<7;i++)
{
printf("x0=%f时,",x0[i]);
x=Newton(x0[i],e);
if(flag) printf("Newton序列收敛于%f\n",x);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -