📄 newton.cpp
字号:
// newton.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream.h>
#include<math.h>
int main(int argc, char* argv[])
{
double x0,x;
double f(double x);
double f1(double x);
double newton(double f(double x),double f1(double x),double x0,int max=20);
x0=2;
x=newton(f,f1,x0);
cout<<"x*x*x-2*x*x+x-12 The roof is:"<<x<<endl;
cin.get();
cin.get();
return 0;
}
//原函数
double f(double x)
{
return x*x*x-2*x*x+x-12;
};
//导函数
double f1(double x)
{
return 3*x*x-4*x+1;
};
//牛顿切线法求根
double newton(double f(double x),double f1(double x),double x0,int max=20)
{
int i,j;
j=0;
double x[50];
i=0;
x[0]=x0;
for(i=0;i<max;i++)
{
x[i+1]=x[i]-f(x[i])/f1(x[i]);
if((x[i+1]-x[i])<=0.00001 && (x[i+1]-x[i])>=-0.00001) break;
};
return x[i+1];
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -