文档详情

工程常用算法作业第一次.docx

发布:2018-05-25约字共13页下载文档
文本预览下载声明
《工程常用算法》综合实践作业一 作业名称:非线性方程求根 完成日期: 2016年3月 28 组号:52 班级 学号 姓名 主要工作说明 自评成绩2013071421 辛超俊 设计算法,编写程序 A2013071524 张宁 用Visio软件绘制流程图 作业题目: 已知方程,请分别用二分法、牛顿法和割线法求此方程的根。误差限取:。注意先确定出方程的有根区间 程序框图: 牛顿法: 二分法: 割线法: 牛顿法源程序: #includestdio.h #includemath.h #includestring.h double NextNumber(double a)//产生下一个数 { return a-(a*a*a+2*a*a-exp(a))/(3*a*a+4*a); } double func(double x)//原函数 { return x*x*x+2*x*x-exp(x); } void main() { double a=1;//A(k) int i=1; double fx=0; double b=NextNumber(1);//A(k+1) double ex=pow(10.0,-12);//精度 fx=func(b); printf(第%2d次 X=%.12f f(x)=%.12f\n,i,a,fx); while(fabs(a-b)exfx!=0) { i++; a=b; b=NextNumber(a); fx=func(b); printf(第%2d次 X=%.12f f(x)=%.12f\n,i,a,fx); } getchar(); } 牛顿法运行结果: 二分法源程序: #includestdio.h #includemath.h #includestring.h double func(double x)//原函数 { return x*x*x+2*x*x-exp(x); } /* 输入参数:区间[a,b]、区间精度ex、函数精度ey */ void main(void) { double a=0;//前一个数 double b=5;//后一个数 double c=0;//中间的数 double fx=0;//函数结果 int i=1; double ex=pow(10.0,-12); c=(a+b)/2.0; fx=func(c); printf(第%2d次 X=%.12f f(x)=%.12f\n,i,c,fx); while((b-a)exfx!=0) { i++; c=(a+b)/2.0; fx=func(c); if(fx==0) { b=c; printf(第%2d次 X=%.12f f(x)=%.12f\n,i,c,fx); break; } else { if(fx0) b=c; else a=c; printf(第%2d次 X=%.12f f(x)=%.12f\n,i,c,fx); } } getchar(); }} 二分法运行结果: 割线法源程序: #includestdio.h #includemath.h #includestring.h double func(double x)//原函数 { return x*x*x+2*x*x-exp(x); } double NextNumber(double a,double b)//产生下一个数 { double fx=b-func(b)*(b-a)/(func(b)-func(a)); return fx; } void main() { double a=0;//A(k-1) double b=3.0;//A(k) int i=1; double c=0;// A(k+1) double ex=pow(10.0,-12);//精度 double fx=0; c=NextNumber(b,a);// fx=func(c); printf(第%d次 X=%.12f f(x)=%.12f\n,i,
显示全部
相似文档