分享
分销 收藏 举报 申诉 / 19
播放页_导航下方通栏广告

类型方程求根计算器实验报告一C--版.docx

  • 上传人:仙人****88
  • 文档编号:12071962
  • 上传时间:2025-09-06
  • 格式:DOCX
  • 页数:19
  • 大小:162.83KB
  • 下载积分:10 金币
  • 播放页_非在线预览资源立即下载上方广告
    配套讲稿:

    如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。

    特殊限制:

    部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。

    关 键  词:
    方程 求根 计算器 实验 报告
    资源描述:
    计算方法实验 题目: 班级: 学号: 姓名: 19 计算方法与实习实验报告 目录 计算方法实验 1 1 实验目的 3 2 实验步骤 3 2.1环境配置: 3 2.2添加头文件 3 2.3主要模块 3 3 代码 4 3.1主程序部分 4 3.2多项式方程部分 4 3.3核心算法部分 7 3.4数据结构部分 11 4运行结果 16 4.1二分法运行结果 16 4.2牛顿迭代法运行结果 17 4.3割线法运行结果 18 边界情况调试 18 5总结 20 输入输出 20 二分法 20 牛顿迭代法 20 割线法 20 6参考资料 20 1 实验目的 1. 通过编程加深二分法、牛顿法和割线法求解多项式方程的理解 2. 观察上述三种方法的计算稳定性和求解精度并比较各种方法利弊 2 实验步骤 2.1环境配置: VS2013,C++控制台程序 2.2添加头文件 #include "stdio.h" #include "stdlib.h" #include "stdafx.h" 2.3主要模块 程序一共分成三层,最底层是数据结构部分,负责存储数据,第二层是交互部分,即多项式方程部分,负责输入输出获得数据,最上层是核心的算法部分,负责处理已获得的数据。 主函数负责获取方程系数并显示,算法和方程作为后台程序,顺序表作为存储手段。 3 代码 3.1主程序部分 // squencelistandlinklist1.cpp : 定义控制台应用程序的入口点。 // #include "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 "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, int highestx); #endif l 方程部分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; for (i = 1; i <= A->last; i++) f = f + A->data[i] * pow(x, A->last - i); return f; } ////////////////////////多项式函数系数///////////////////////// void GetEquation(void) { int j = 0; int x = 0; B = InitList(); cout << "方程最高项次数(如y=x^3最高项次数为3):" << endl; cin >> highestx; cout << "输入方程系数,输入00结束(如y=x^2+2x+1输入1 2 1 00):" << 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.二分法求解" << endl; cout << "2.牛顿迭代法求解" << endl; cout << "3.割线法求解" << endl; cin >> x; switch (x) { case 1: cout << "请输入区间上下限(如【a,b】输入a b):" << endl; cin >> a >> b; cout << "请输入根的容许误差和函数的容许误差(若只有一个误差则另一项为0):" << endl; 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 << "请输入初始值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 (i = 1; i < A->last; i++) { if (A->data[i+1] < 0) cout << A->data[i] << "*" << "x^" << A->last - i; else cout << A->data[i] << "*" << "x^" << A->last - i << "+"; } cout << A->data[i] << endl; } //////////////////////////函数微分///////////////////////////// sequenlist Derivative(sequenlist *A, int highestx) { int i; D = InitList(); for (i = 1; i <= A->last; i++) Insert(D,( A->data[i] * (A->last - i)), i); D->last = A->last - 1; return *D; } 3.3核心算法部分 l 算法部分头文件 #ifndef _ALGORITHM_H #define _ALGORITHM_H #include "stdio.h" #include "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); #endif l 算法部分CPP文件 #include "algorithm.h" #include "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; 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" << 'x' << "\t\t" << "f(x)" << endl; if (fa*fb > 0) { cout << "无法使用二分法" << endl; flag = 1; } while (1) { if (fa == 0) { xn = xa; cout << n++ << "\t\t" << xn << "\t\t" << Function(B, xn) << endl; break; } if (fb == 0) { xn = xb; cout << n++ << "\t\t" << xn << "\t\t" << Function(B, xn) << endl; break; } xn = (a + b) / 2; fn = Function(B, xn); cout << n++ << "\t\t" << xn << "\t\t" << Function(B, xn) << endl; if (fn == 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 << "///////////////////牛顿迭代法///////////////////" << endl; cout << "迭代次数" << "\t" << "x" << "\t\t" << "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) { cout << "导数为零" << 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++ << "\t\t" << xn << "\t\t" << Function(B, xn) << endl; if (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" << "\t\t" << "f(x)" << endl; *D = Derivative(B, highestx); f0 = Function(B, x0); f1 = Function(B, x1); if (fabs(f0) < ef) { cout << n++ << "\t\t" << x0 << "\t\t" << Function(B, x0) << endl; return x0; } if (fabs(f1) < ef) { cout << n++ << "\t\t" << x1 << "\t\t" << Function(B, x1) << endl; return x1; } if (Judge(x1, x0, ex)) { cout << n++ << "\t\t" << x1 << "\t\t" << Function(B, x1) << endl; return x1; } while (!flag) { xn = x1 - f1*(x1 - x0) / (f1 - f0); fn = Function(B, xn); flag = (Judge(xn, x1, ex) || Judge(fn, 0, ef)); cout << n++ << "\t\t" << xn << "\t\t" << Function(B, xn) << endl; x0 = x1; x1 = xn; if (n > 3000) { cout << "迭代失败" << endl; return 0; } } return 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 struct { datatype data[maxsize]; int last; }sequenlist; 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 */ typedef char linkdatatype; typedef struct node { linkdatatype data; struct node*next; }linklist; linklist* CreateListF(); #endif l 数据结构CPP文件 #include "stdafx.h" #include "squencelist.h" ///////////////////////////////////数据结构部分/////////////////////////////////////////// ///////////////////////////////////sequenlist/////////////////////////////////////////// sequenlist *InitList() { sequenlist*L = (sequenlist*)malloc(sizeof(sequenlist)); L->last = 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->data[j + 1] = L->data[j]; L->data[i] = 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->data[j] = L->data[j + 1]; L->last--; return 1; } int Locate(sequenlist*L, datatype x) { int i = 1; while (i <= L->last) { if (L->data[i] != 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->data[i] << ' '; 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 (A->data[j] == B->data[j])i = 0; if (A->data[j] < B->data[j]) { i = -1; break; } if (A->data[j] > B->data[j]) { i = 1; break; } j++; } if (i != 0)return i; else { if (m < n)i = 1; if (n < m)i = -1; if (m == n)i = 0; return i; } } void Invert(sequenlist*L) { int i; int temp; for (i = 1; i <= L->last / 2; i++) { temp = L->data[i]; L->data[i] = L->data[L->last + 1 - i]; L->data[L->last + 1 - i] = temp; } } ///////////////////////////////////linklist/////////////////////////////////////////// linklist* CreateListF() { linklist *head, *p; char ch; head = (linklist*)malloc(sizeof(linklist)); head->next = NULL; cin >> ch; while (ch != '\n') { p = (linklist*)malloc(sizeof(linklist)); p->data = ch; p->next = head->next; head->next = p; } return head; } 4运行结果 4.1二分法运行结果 4.2牛顿迭代法运行结果 4.3割线法运行结果 边界情况调试 l 二分法方程的根导数也为零的情况 l 牛顿迭代法导数为零的情况 5总结 这次的程序设计题看似简单实则临界代码区很多,调试时很容易出错。 C++的输入输出数据流对于不定数字的输入显得很麻烦,在输入方程系数的时候对于任意阶数的方程由于C++没有动态扩展内存的功能几乎不能交互,设定一个很大的宏显然浪费空间。于是想先确定最高阶数并保存在变量中,系数的读取则使用循环,由于C++读取数据的特点还能防止输入的系数个数和需要的个数不相符,使用00作为结束标志不会和0冲突。 因为方程的阶数是不定的,所以考虑顺序表的存储方式。 6参考资料 《计算方法与实习》孙志忠等著 第五版---东南大学出版社 《软件技术基础》周大为等著 第一版---西安电子科技大学出版社
    展开阅读全文
    提示  咨信网温馨提示:
    1、咨信平台为文档C2C交易模式,即用户上传的文档直接被用户下载,收益归上传人(含作者)所有;本站仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。所展示的作品文档包括内容和图片全部来源于网络用户和作者上传投稿,我们不确定上传用户享有完全著作权,根据《信息网络传播权保护条例》,如果侵犯了您的版权、权益或隐私,请联系我们,核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
    2、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据,个别因单元格分列造成显示页码不一将协商解决,平台无法对文档的真实性、完整性、权威性、准确性、专业性及其观点立场做任何保证或承诺,下载前须认真查看,确认无误后再购买,务必慎重购买;若有违法违纪将进行移交司法处理,若涉侵权平台将进行基本处罚并下架。
    3、本站所有内容均由用户上传,付费前请自行鉴别,如您付费,意味着您已接受本站规则且自行承担风险,本站不进行额外附加服务,虚拟产品一经售出概不退款(未进行购买下载可退充值款),文档一经付费(服务费)、不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
    4、如你看到网页展示的文档有www.zixin.com.cn水印,是因预览和防盗链等技术需要对页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有水印标识(原文档上传前个别存留的除外),下载后原文更清晰;试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓;PPT和DOC文档可被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;PDF文档不管是原文档转换或图片扫描而得,本站不作要求视为允许,下载前可先查看【教您几个在下载文档中可以更好的避免被坑】。
    5、本文档所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用;网站提供的党政主题相关内容(国旗、国徽、党徽--等)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
    6、文档遇到问题,请及时联系平台进行协调解决,联系【微信客服】、【QQ客服】,若有其他问题请点击或扫码反馈【服务填表】;文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“【版权申诉】”,意见反馈和侵权处理邮箱:1219186828@qq.com;也可以拔打客服电话:0574-28810668;投诉电话:18658249818。

    开通VIP折扣优惠下载文档

    自信AI创作助手
    关于本文
    本文标题:方程求根计算器实验报告一C--版.docx
    链接地址:https://www.zixin.com.cn/doc/12071962.html
    页脚通栏广告

    Copyright ©2010-2025   All Rights Reserved  宁波自信网络信息技术有限公司 版权所有   |  客服电话:0574-28810668    微信客服:咨信网客服    投诉电话:18658249818   

    违法和不良信息举报邮箱:help@zixin.com.cn    文档合作和网站合作邮箱:fuwu@zixin.com.cn    意见反馈和侵权处理邮箱:1219186828@qq.com   | 证照中心

    12321jubao.png12321网络举报中心 电话:010-12321  jubao.png中国互联网举报中心 电话:12377   gongan.png浙公网安备33021202000488号  icp.png浙ICP备2021020529号-1 浙B2-20240490   


    关注我们 :微信公众号  抖音  微博  LOFTER               

    自信网络  |  ZixinNetwork