




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、计算方法实验题目:班级:学号:姓名:19计算方法与实习实验报告目录计算方法实验11 实验目的32 实验步骤32.1环境配置:32.2添加头文件32.3主要模块33 代码43.1主程序部分43.2多项式方程部分43.3核心算法部分73.4数据结构部分114运行结果164.1二分法运行结果164.2牛顿迭代法运行结果174.3割线法运行结果18边界情况调试185总结20输入输出20二分法20牛顿迭代法20割线法206参考资料201 实验目的1. 通过编程加深二分法、牛顿法和割线法求解多项式方程的理解2. 观察上述三种方法的计算稳定性和求解精度并比较各种方法利弊2 实验步骤2.1环境配置:VS201
2、3,C+控制台程序2.2添加头文件#include "stdio.h"#include "stdlib.h"#include "stdafx.h"2.3主要模块程序一共分成三层,最底层是数据结构部分,负责存储数据,第二层是交互部分,即多项式方程部分,负责输入输出获得数据,最上层是核心的算法部分,负责处理已获得的数据。主函数负责获取方程系数并显示,算法和方程作为后台程序,顺序表作为存储手段。3 代码3.1主程序部分/ squencelistandlinklist1.cpp : 定义控制台应用程序的入口点。/#include "
3、stdio.h"#include "stdlib.h"#include "stdafx.h"#include "squencelist.h"#include "equation.h"#include<iostream>/主程序/int _tmain(int argc, _TCHAR* argv)GetEquation();ShowMenu();return 0;3.2多项式方程部分l 方程部分头文件#ifndef _EQUATION_H#define _EQUATION_H#include &
4、quot;squencelist.h"#include "stdio.h"#include "stdlib.h"extern int highestx;extern sequenlist *B;extern sequenlist *D;double Function(sequenlist *B, double x);void GetEquation(void);void ShowMenu(void);void printfunction(sequenlist *A);sequenlist Derivative(sequenlist *A, in
5、t highestx);#endifl 方程部分CPP文件#include "stdafx.h"#include "equation.h"#include "math.h"#include "algorithm.h"#include "squencelist.h"/全局变量int highestx=0;sequenlist *B;sequenlist *D;/多项式函数/double Function(sequenlist *A,double x)double f = 0.00;int i;fo
6、r (i = 1; i <= A->last; i+)f = f + A->datai * pow(x, A->last - i);return f;/多项式函数系数/void GetEquation(void)int j = 0;int x = 0;B = InitList();cout << "方程最高项次数(如y=x3最高项次数为3):" << endl;cin >> highestx;cout << "输入方程系数,输入00结束(如y=x2+2x+1输入1 2 1 00):"
7、 << endl;cin >> x;while (x != 00)for (j = 1; j <= highestx + 1; j+)if (!Insert(B, x, j)exit(0);cin >> x;j = 1;printfunction(B);/显示交互/void ShowMenu(void)int x;double a, b, ex, ef, res, x0,x1;cout << "选择求解方程根的方法:" << endl;cout << "1.二分法求解" <
8、;< endl;cout << "2.牛顿迭代法求解" << endl;cout << "3.割线法求解" << endl;cin >> x;switch (x)case 1:cout << "请输入区间上下限(如【a,b】输入a b):" << endl;cin >> a >> b;cout << "请输入根的容许误差和函数的容许误差(若只有一个误差则另一项为0):" << e
9、ndl;cin >> ex >> ef;res = Dichotomy(a,b,ex,ef);break;case 2:cout << "请输入初始值x0:" << endl;cin >> x0;cout << "请输入根的容许误差和函数的容许误差(若只有一个误差则另一项为0):" << endl;cin >> ex >> ef;res = Newtonsmethod(x0, ex, ef);break;case 3:cout << &
10、quot;请输入初始值x0,x1:" << endl;cin >> x0 >> x1;cout << "请输入根的容许误差和函数的容许误差(若只有一个误差则另一项为0):" << endl;cin >> ex >> ef;res = Cutmethod(x0, x1, ex, ef);break;default:break;/打印输出函数/void printfunction(sequenlist *A)int i;cout << "f="for (
11、i = 1; i < A->last; i+)if (A->datai+1 < 0)cout << A->datai << "*" << "x" << A->last - i;else cout << A->datai << "*" << "x" << A->last - i << "+"cout << A->datai &
12、lt;< endl;/函数微分/sequenlist Derivative(sequenlist *A, int highestx)int i;D = InitList();for (i = 1; i <= A->last; i+)Insert(D,( A->datai * (A->last - i), i);D->last = A->last - 1;return *D;3.3核心算法部分l 算法部分头文件#ifndef _ALGORITHM_H#define _ALGORITHM_H#include "stdio.h"#incl
13、ude "stdlib.h"int Judge(double x1, double x0, double e);double Dichotomy(double xa, double xb, double ex, double ef);double Newtonsmethod(double x0, double ex, double ef);double Cutmethod(double x1, double x0, double ex, double ef);#endifl 算法部分CPP文件#include "algorithm.h"#include
14、"stdafx.h"#include "squencelist.h"#include "equation.h"/误差判别/inline int Judge(double x1, double x0, double e)if (e = 0)return 0;if (x1 = 0)if (fabs(x0) < e)return 1;else return 0;if (x0 = 0)if (fabs(x1) < e)return 1;else return 0;if (fabs(x1 - x0) < e)return 1;
15、else return 0;/二分法/double Dichotomy(double xa, double xb, double ex, double ef)cout << "/二分法/" << endl;double xn = 0, fn = 0, fa = Function(B, xa), fb = Function(B, xb);double a, b;int n = 1, flag = 0;a = xa;b = xb;cout << "二分次数" << "t" <<
16、'x' << "tt" << "f(x)" << endl;if (fa*fb > 0)cout << "无法使用二分法" << endl;flag = 1;while (1)if (fa = 0)xn = xa;cout << n+ << "tt" << xn << "tt" << Function(B, xn) << endl;break;
17、if (fb = 0)xn = xb;cout << n+ << "tt" << xn << "tt" << Function(B, xn) << endl;break;xn = (a + b) / 2;fn = Function(B, xn);cout << n+ << "tt" << xn << "tt" << Function(B, xn) << endl;if (f
18、n = 0)break;if (fn*fa < 0)b = xn;flag = (Judge(a, b, ex) | Judge(fn, 0, ef);else if (fn*fb < 0)a = xn;flag = (Judge(a, b, ex) | Judge(fn, 0, ef);if (flag)break;return xn;/牛顿迭代法/double Newtonsmethod(double x0, double ex, double ef)double xn = 0, fn = 0, dfn = 0;int n = 1, flag = 0;cout <<
19、 "/牛顿迭代法/" << endl;cout << "迭代次数" << "t" << "x" << "tt" << "f(x)" << endl;*D = Derivative(B, highestx);fn = Function(B, x0);dfn = Function(D, x0);if (fabs(fn) < ef)flag = 1;while (1)if (dfn = 0)c
20、out << "导数为零" << endl;break;xn = x0 - fn / dfn;fn = Function(B, x0);dfn = Function(D, x0);flag = (Judge(xn, x0, ex) | Judge(fn, 0, ef);if (flag)break;x0 = xn;cout << n+ << "tt" << xn << "tt" << Function(B, xn) << endl;if (
21、n > 3000)cout << "迭代失败" << endl;return 0;return xn;/割线法/double Cutmethod(double x1, double x0, double ex, double ef)double xn, fn, f0, f1;int n = 1, flag = 0;cout << "/割线法/" << endl;cout << "迭代次数" << "t" << "x&
22、quot; << "tt" << "f(x)" << endl;*D = Derivative(B, highestx);f0 = Function(B, x0);f1 = Function(B, x1);if (fabs(f0) < ef)cout << n+ << "tt" << x0 << "tt" << Function(B, x0) << endl;return x0;if (fabs(f1)
23、 < ef)cout << n+ << "tt" << x1 << "tt" << Function(B, x1) << endl;return x1;if (Judge(x1, x0, ex)cout << n+ << "tt" << x1 << "tt" << Function(B, x1) << endl;return x1;while (!flag)xn =
24、x1 - f1*(x1 - x0) / (f1 - f0);fn = Function(B, xn);flag = (Judge(xn, x1, ex) | Judge(fn, 0, ef);cout << n+ << "tt" << xn << "tt" << Function(B, xn) << endl;x0 = x1;x1 = xn;if (n > 3000)cout << "迭代失败" << endl;return 0;re
25、turn xn;3.4数据结构部分l 数据结构头文件#ifndef _SQUENCELIST_H#define _SQUENCELIST_H#include "stdio.h"#include "stdlib.h"#include "stdafx.h"#include<iostream>using namespace std;#define maxsize 1024/*sequenlist*/typedef int datatype;typedef structdatatype datamaxsize;int last;s
26、equenlist;sequenlist *InitList();int Length(sequenlist*);int Insert(sequenlist*, datatype, int);int Delete(sequenlist*, int);int Locate(sequenlist*, datatype);void del_node(sequenlist*, datatype);void PrintList(sequenlist*);int Compare_L(sequenlist*, sequenlist*);void Invert(sequenlist*);/*linklist*
27、/typedef char linkdatatype;typedef struct nodelinkdatatype data;struct node*next;linklist;linklist* CreateListF();#endifl 数据结构CPP文件#include "stdafx.h"#include "squencelist.h"/数据结构部分/sequenlist/sequenlist *InitList()sequenlist*L = (sequenlist*)malloc(sizeof(sequenlist);L->last
28、= 0;return L;/sequenlist*L = new sequenlist;int Length(sequenlist*L)return L->last;int Insert(sequenlist*L, datatype x, int i)int j;if (L->last >= maxsize - 1)cout << "表已满" << endl;return 0;for (j = L->last; j >= i; j-)L->dataj + 1 = L->dataj;L->datai =
29、x;L->last+;return 1;int Delete(sequenlist*L, int i)int j;if (i<1) | (i>L->last)cout << "非法删除位置" << endl;return 0;for (j = i; j <= L->last; j+)L->dataj = L->dataj + 1;L->last-;return 1;int Locate(sequenlist*L, datatype x)int i = 1;while (i <= L->
30、last)if (L->datai != x)i+;else return i;return 0;/*顺序表中删除所有元素为x的结点*/void del_node(sequenlist*L, datatype x)int i = Locate(L, x);while (i != 0)if (!Delete(L, i)break;i = Locate(L, x);void PrintList(sequenlist*L)int i = 1;for (i = 1; i <= L->last; i+)cout << L->datai << ' 'cout << endl;int Compare_L(sequenlist*A, sequenlist*B)int j = 1;int i = 0;int n, m;n = A->last;m = B->last;while (j <= n) && (j <= m)if
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 专业知识试题及答案
- 砂石料购销合同范本
- 会计学专业试题及答案
- 宾馆酒店消防安全测试题及答案解析
- 证券从业考试境外毕业及答案解析
- 证券从业资格考试加群及答案解析
- 师德师风考试题库及答案
- 银行招聘考试试题及答案
- 2025年起重机械指挥考试题库及起重机械指挥解析
- 小黑课堂模拟试题及答案
- 【MOOC】电工电子实验基础-东南大学 中国大学慕课MOOC答案
- 《专利池与公共利益》课件
- 第1课-远古时期的人类活动【同步练习】
- 演唱会招商方案
- 河北信息技术学业水平考试试题集
- 压力容器使用单位安全总监题库
- 创业合伙人五份协议书模板
- 建筑工程消防查验检查表
- 应征公民政治审查表
- FZ∕T 71006-2021 山羊绒针织绒线
- 慢性创面的治疗及护理课件
评论
0/150
提交评论