




已閱讀5頁(yè),還剩14頁(yè)未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
計(jì)算方法實(shí)驗(yàn)題目:班級(jí):學(xué)號(hào):姓名:19計(jì)算方法與實(shí)習(xí)實(shí)驗(yàn)報(bào)告目錄計(jì)算方法實(shí)驗(yàn)11 實(shí)驗(yàn)?zāi)康?2 實(shí)驗(yàn)步驟32.1環(huán)境配置:32.2添加頭文件32.3主要模塊33 代碼43.1主程序部分43.2多項(xiàng)式方程部分43.3核心算法部分73.4數(shù)據(jù)結(jié)構(gòu)部分114運(yùn)行結(jié)果164.1二分法運(yùn)行結(jié)果164.2牛頓迭代法運(yùn)行結(jié)果174.3割線(xiàn)法運(yùn)行結(jié)果18邊界情況調(diào)試185總結(jié)20輸入輸出20二分法20牛頓迭代法20割線(xiàn)法206參考資料201 實(shí)驗(yàn)?zāi)康?. 通過(guò)編程加深二分法、牛頓法和割線(xiàn)法求解多項(xiàng)式方程的理解2. 觀察上述三種方法的計(jì)算穩(wěn)定性和求解精度并比較各種方法利弊2 實(shí)驗(yàn)步驟2.1環(huán)境配置:VS2013,C+控制臺(tái)程序2.2添加頭文件#include stdio.h#include stdlib.h#include stdafx.h2.3主要模塊程序一共分成三層,最底層是數(shù)據(jù)結(jié)構(gòu)部分,負(fù)責(zé)存儲(chǔ)數(shù)據(jù),第二層是交互部分,即多項(xiàng)式方程部分,負(fù)責(zé)輸入輸出獲得數(shù)據(jù),最上層是核心的算法部分,負(fù)責(zé)處理已獲得的數(shù)據(jù)。主函數(shù)負(fù)責(zé)獲取方程系數(shù)并顯示,算法和方程作為后臺(tái)程序,順序表作為存儲(chǔ)手段。3 代碼3.1主程序部分/ squencelistandlinklist1.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。/#include stdio.h#include stdlib.h#include stdafx.h#include squencelist.h#include equation.h#include/主程序/int _tmain(int argc, _TCHAR* argv)GetEquation();ShowMenu();return 0;3.2多項(xiàng)式方程部分l 方程部分頭文件#ifndef _EQUATION_H#define _EQUATION_H#include squencelist.h#include stdio.h#include stdlib.hextern 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);#endifl 方程部分CPP文件#include stdafx.h#include equation.h#include math.h#include algorithm.h#include squencelist.h/全局變量int highestx=0;sequenlist *B;sequenlist *D;/多項(xiàng)式函數(shù)/double Function(sequenlist *A,double x)double f = 0.00;int i;for (i = 1; i last; i+)f = f + A-datai * pow(x, A-last - i);return f;/多項(xiàng)式函數(shù)系數(shù)/void GetEquation(void)int j = 0;int x = 0;B = InitList();cout 方程最高項(xiàng)次數(shù)(如y=x3最高項(xiàng)次數(shù)為3): highestx;cout 輸入方程系數(shù),輸入00結(jié)束(如y=x2+2x+1輸入1 2 1 00): x;while (x != 00)for (j = 1; j 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.割線(xiàn)法求解 x;switch (x)case 1:cout 請(qǐng)輸入?yún)^(qū)間上下限(如【a,b】輸入a b): a b;cout 請(qǐng)輸入根的容許誤差和函數(shù)的容許誤差(若只有一個(gè)誤差則另一項(xiàng)為0): ex ef;res = Dichotomy(a,b,ex,ef);break;case 2:cout 請(qǐng)輸入初始值x0: x0;cout 請(qǐng)輸入根的容許誤差和函數(shù)的容許誤差(若只有一個(gè)誤差則另一項(xiàng)為0): ex ef;res = Newtonsmethod(x0, ex, ef);break;case 3:cout 請(qǐng)輸入初始值x0,x1: x0 x1;cout 請(qǐng)輸入根的容許誤差和函數(shù)的容許誤差(若只有一個(gè)誤差則另一項(xiàng)為0): ex ef;res = Cutmethod(x0, x1, ex, ef);break;default:break;/打印輸出函數(shù)/void printfunction(sequenlist *A)int i;cout f=;for (i = 1; i last; i+)if (A-datai+1 0)cout datai * x last - i;else cout datai * x last - i +;cout datai endl;/函數(shù)微分/sequenlist Derivative(sequenlist *A, int highestx)int i;D = InitList();for (i = 1; i 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#include stdlib.hint 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 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 二分次數(shù) t x tt f(x) 0)cout 無(wú)法使用二分法 endl;flag = 1;while (1)if (fa = 0)xn = xa;cout n+ tt xn tt Function(B, xn) endl;break;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 (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 迭代次數(shù) 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)cout 導(dǎo)數(shù)為零 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) 3000)cout 迭代失敗 endl;return 0;return xn;/割線(xiàn)法/double Cutmethod(double x1, double x0, double ex, double ef)double xn, fn, f0, f1;int n = 1, flag = 0;cout /割線(xiàn)法/ endl;cout 迭代次數(shù) t x 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) 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 = 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) 3000)cout 迭代失敗 endl;return 0;return xn;3.4數(shù)據(jù)結(jié)構(gòu)部分l 數(shù)據(jù)結(jié)構(gòu)頭文件#ifndef _SQUENCELIST_H#define _SQUENCELIST_H#include stdio.h#include stdlib.h#include stdafx.h#includeusing namespace std;#define maxsize 1024/*sequenlist*/typedef int datatype;typedef structdatatype datamaxsize;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 nodelinkdatatype data;struct node*next;linklist;linklist* CreateListF();#endifl 數(shù)據(jù)結(jié)構(gòu)CPP文件#include stdafx.h#include squencelist.h/數(shù)據(jù)結(jié)構(gòu)部分/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 表已滿(mǎn) last; j = i; j-)L-dataj + 1 = L-dataj;L-datai = x;L-last+;return 1;int Delete(sequenlist*L, int i)int j;if (iL-last)cout 非法刪除位置 endl;return 0;for (j = i; j last; j+)L-dataj = L-dataj + 1;L-last-;return 1;int Locate(sequenlist*L, datatype x)int i = 1;while (i last)if (L-datai != x)i+;else return i;return 0;/*順序表中刪除所有元素為x的結(jié)點(diǎn)*/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 last; i+)cout datai ;cout last;m = B-last;while (j = n) & (j dataj = B-dataj)i = 0;if (A-dataj dataj)i = -1;break;if (A-dataj B-dataj)i = 1;break;j+;if (i != 0)return i;elseif (m n)i = 1;if (n m)i = -1;if (m = n)i = 0;return i;void Invert
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 中藥診所轉(zhuǎn)讓合同范本
- 高壓電工作業(yè)試題(含參考答案)
- 美容技能鑒定模擬題(含參考答案)
- 人事行政年終總結(jié)及下半年計(jì)劃
- 地下排污管道施工方案
- 頂管豎井施工方案
- 陽(yáng)極爐溜槽施工方案
- 市政施工方案
- 2025年精細(xì)化學(xué)品:日用化學(xué)品項(xiàng)目發(fā)展計(jì)劃
- 社區(qū)婦聯(lián)工作總結(jié)
- 初中數(shù)學(xué)人教八年級(jí)上冊(cè)軸對(duì)稱(chēng)-課題學(xué)習(xí)最短路徑問(wèn)題將軍飲馬PPT
- 電子技術(shù)基礎(chǔ)數(shù)字部分(第五版)(康華光)第一章課件
- 外語(yǔ)教師科研立項(xiàng)申報(bào)及特點(diǎn)分析課件
- 質(zhì)量管理小組活動(dòng)準(zhǔn)則TCAQ10201-2020
- 支氣管肺炎完整版課件
- 譯林英語(yǔ)五年級(jí)下冊(cè)單詞表(孩子自己默寫(xiě)不用提)
- DLT 1055-2021 火力發(fā)電廠汽輪機(jī)技術(shù)監(jiān)督導(dǎo)則
- 杭州房建工程監(jiān)理大綱范本
- 現(xiàn)代交換原理與技術(shù)課件:第5章 分組交換技術(shù)
- Q∕GDW 12157-2021 應(yīng)急培訓(xùn)演練基地建設(shè)與評(píng)價(jià)規(guī)范
- 勵(lì)磁系統(tǒng)報(bào)告(共25頁(yè))
評(píng)論
0/150
提交評(píng)論