⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rootneu.cpp

📁 从出版社求得的经典数值算法
💻 CPP
字号:
/////////////////////////////////////////////////////
//	程序5.3  Newton迭代

#include	<stdio.h>
#include	<conio.h>
#include	<math.h>

#define PRECISION	0.0000001
#define MAX_Number	10000

float f(float x)  //函数f(x)
{
	return(x*x*x-x-1);
}

float df(float x) //函数f(x)的导数f'(x)
{
	return(3*x*x-1);
}

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);
	}
}

void main()
{
	NewtonIterative();

	printf("\n\n\007Press any key to quit!\n");
	getch();
}

/*
	运行结果:(注意,要运行其它例子,请在程序中修改f(x)和df(x))

Input Initial Value:
x0=1

x1=1.500000
x2=1.347826
x3=1.325200
x4=1.324718
x5=1.324718
x6=1.324718

Iterative times k=7
Root x=1.324718

Press any key to quit!
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -