📄 newton.c
字号:
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define PRECISION 0.01
#define MAX_Number 100
float f(float x) //函数fx)
{
return(x*x*x-2*x*x-4*x-7);
}
float df(float x) //函数f(x)的导数f'(x)
{
return(3*x*x-4*x-4);
}
void NewtonIterative()
{
int k;
float x0,x;
printf("\n\nInput Initial Value:\nx0=");
scanf("%f",&x);
k=1;
do{
x0=x;
x=x0-f(x0)/df(x0);
printf("\nx%d=%f",k,x);
++k;
}while(fabs(x-x0)>PRECISION&&k<MAX_Number);
if(k>=MAX_Number)
printf("Simple Iterative failed(k=%d)",k);
else
{
printf("\n\nIterative times k=%d",k);
printf("\nRoot x=%f",x);
}
}
int main()
{
NewtonIterative();
printf("\n\nPress any dey to quit! \n");
getch();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -