牛顿迭代法改造!!!!
2017-05-30 11:37
323 查看
参考:
百度百科 牛顿迭代法
迭代次数可以用控制精度代替,迭代次数无依据
// TestNewdun.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "math.h"
#include "stdlib.h"
double func(double x) //函数
{
return x*x*x*x-3*x*x*x+1.5*x*x-4.0;
}
double func1(double x) //导函数
{
return 4*x*x*x-9*x*x+3*x;
}
int Newton(double *x,double precision,int maxcyc) //迭代次数
{
double x1,x0;
int k;
x0=*x;
for(k=0;k<maxcyc;k++) //可以 while(1)
{
if(func1(x0)==0.0)//若通过初值,函数返回值为0
{
printf("迭代过程中导数为0!\n");
return 0;
}
x1=x0-func(x0)/func1(x0);//进行牛顿迭代计算
if(fabs(x1-x0)<precision || fabs(func(x1))<precision)////达到结束条件
{
*x=x1; //返回结果
return 1;
}
else //未达到结束条件
{
x0=x1; //准备下一次迭代
}
}
printf("迭代次数超过预期!\n"); //迭代次数达到,仍没有达到精度
return 0;
}
int main()
{
double x,precision;
int maxcyc;
printf("输入初始迭代值x0:");
scanf("%lf",&x);
printf("输入最大迭代次数:");
scanf("%d",&maxcyc);
printf("迭代要求的精度:");
scanf("%lf",&precision);
if(Newton(&x,precision,maxcyc)==1) //若函数返回值为1
{
printf("该值附近的根为:%lf\n",x);
}
else //若函数返回值为0
{
printf("迭代失败!\n");
}/**/
system("pause");
return 0;
}
百度百科 牛顿迭代法
迭代次数可以用控制精度代替,迭代次数无依据
// TestNewdun.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "math.h"
#include "stdlib.h"
double func(double x) //函数
{
return x*x*x*x-3*x*x*x+1.5*x*x-4.0;
}
double func1(double x) //导函数
{
return 4*x*x*x-9*x*x+3*x;
}
int Newton(double *x,double precision,int maxcyc) //迭代次数
{
double x1,x0;
int k;
x0=*x;
for(k=0;k<maxcyc;k++) //可以 while(1)
{
if(func1(x0)==0.0)//若通过初值,函数返回值为0
{
printf("迭代过程中导数为0!\n");
return 0;
}
x1=x0-func(x0)/func1(x0);//进行牛顿迭代计算
if(fabs(x1-x0)<precision || fabs(func(x1))<precision)////达到结束条件
{
*x=x1; //返回结果
return 1;
}
else //未达到结束条件
{
x0=x1; //准备下一次迭代
}
}
printf("迭代次数超过预期!\n"); //迭代次数达到,仍没有达到精度
return 0;
}
int main()
{
double x,precision;
int maxcyc;
printf("输入初始迭代值x0:");
scanf("%lf",&x);
printf("输入最大迭代次数:");
scanf("%d",&maxcyc);
printf("迭代要求的精度:");
scanf("%lf",&precision);
if(Newton(&x,precision,maxcyc)==1) //若函数返回值为1
{
printf("该值附近的根为:%lf\n",x);
}
else //若函数返回值为0
{
printf("迭代失败!\n");
}/**/
system("pause");
return 0;
}
相关文章推荐
- 牛顿迭代法(Newton's Method)
- Android借用Arouter进行组件化旧城改造。
- 淘淘商城系列——网页静态化——把jsp改造为freemarker模板
- dubbo对传统ssm进行服务化改造的思路以及一些分布式工具实现的思路
- 怎样将自己的工程改造成库,并上传到Github作为依赖
- 改造:Ueditor编辑器插入外链视频支持手机播放和上传本地音乐
- .csv大报表改造-oracle创建事务级临时表
- HBase写入性能改造(续)--MemStore、flush、compact参数调优及压缩卡的使用【转】
- bzoj 1749: [usaco2005 open]Landscaping 地形改造
- 老旧电商系统升级改造日记 - 1. 背景概述
- java微服务架构中改造时,引用旧jpa实体引发的血案
- 改造Vmware里的FastDFS为Docker
- HBase写入性能改造(续)--MemStore、flush、compact参数调优及压缩卡的使用
- appium改造思路
- 改造升级财务付款系统
- 《开源框架那点事儿25》:对框架模板引擎实现方式的改造实录
- .net项目架构改造之搭建基于java环境配置一览【上】
- 基于CAS的单点登录SSO[3]: 改造cas-overlays-template支持Redis存储Ticket
- [js高手之路]面向对象+设计模式+继承一步步改造简单的四则运算
- 树莓派-改造插线板,支持手机平板控制开关