?? 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 + -