华南理工大学《计算方法》实验报告
计算方法实验报告
![计算方法实验报告](https://img.taocdn.com/s3/m/e4ec51be710abb68a98271fe910ef12d2bf9a960.png)
计算方法实验报告计算方法实验报告概述:计算方法是一门研究如何用计算机解决数学问题的学科。
在本次实验中,我们将学习和应用几种常见的计算方法,包括数值逼近、插值、数值积分和常微分方程求解。
通过实验,我们将深入了解这些方法的原理、应用场景以及其在计算机科学和工程领域的重要性。
数值逼近:数值逼近是一种通过使用近似值来计算复杂函数的方法。
在实验中,我们通过使用泰勒级数展开和牛顿迭代法等数值逼近技术,来计算函数的近似值。
这些方法在科学计算和工程领域中广泛应用,例如在信号处理、图像处理和优化问题中。
插值:插值是一种通过已知数据点来估算未知数据点的方法。
在实验中,我们将学习和应用拉格朗日插值和牛顿插值等方法,以及使用这些方法来构造函数的近似曲线。
插值技术在数据分析、图像处理和计算机图形学等领域中具有重要的应用价值。
数值积分:数值积分是一种通过将函数曲线划分为小矩形或梯形来估算函数的积分值的方法。
在实验中,我们将学习和应用矩形法和梯形法等数值积分技术,以及使用这些方法来计算函数的近似积分值。
数值积分在物理学、金融学和统计学等领域中被广泛使用。
常微分方程求解:常微分方程求解是一种通过数值方法来求解微分方程的方法。
在实验中,我们将学习和应用欧拉法和龙格-库塔法等常微分方程求解技术,以及使用这些方法来求解一些常见的微分方程。
常微分方程求解在物理学、生物学和工程学等领域中具有广泛的应用。
实验结果:通过实验,我们成功地应用了数值逼近、插值、数值积分和常微分方程求解等计算方法。
我们得到了准确的结果,并且在不同的应用场景中验证了这些方法的有效性和可靠性。
这些实验结果将对我们进一步理解和应用计算方法提供重要的指导和支持。
结论:计算方法是计算机科学和工程领域中的重要学科,它提供了解决复杂数学问题的有效工具和方法。
通过本次实验,我们深入了解了数值逼近、插值、数值积分和常微分方程求解等计算方法的原理和应用。
这些方法在科学研究、工程设计和数据分析等领域中具有广泛的应用价值。
计算方法_实验报告
![计算方法_实验报告](https://img.taocdn.com/s3/m/abac4148905f804d2b160b4e767f5acfa1c7839f.png)
一、实验目的1. 理解并掌握计算方法的基本概念和原理;2. 学会使用计算方法解决实际问题;3. 提高编程能力和算法设计能力。
二、实验内容本次实验主要涉及以下内容:1. 线性方程组的求解;2. 多项式插值;3. 牛顿法求函数零点;4. 矩阵的特征值和特征向量求解。
三、实验环境1. 操作系统:Windows 102. 编程语言:Python3.83. 科学计算库:NumPy、SciPy四、实验步骤及结果分析1. 线性方程组的求解(1)实验步骤a. 导入NumPy库;b. 定义系数矩阵A和增广矩阵b;c. 使用NumPy的linalg.solve()函数求解线性方程组。
(2)实验结果设系数矩阵A和增广矩阵b如下:A = [[2, 1], [1, 2]]b = [3, 2]解得:x = [1, 1]2. 多项式插值(1)实验步骤a. 导入NumPy库;b. 定义插值点x和对应的函数值y;c. 使用NumPy的polyfit()函数进行多项式拟合;d. 使用poly1d()函数创建多项式对象;e. 使用多项式对象计算插值点对应的函数值。
(2)实验结果设插值点x和对应的函数值y如下:x = [1, 2, 3, 4, 5]y = [1, 4, 9, 16, 25]拟合得到的二次多项式为:f(x) = x^2 + 1在x = 3时,插值得到的函数值为f(3) = 10。
3. 牛顿法求函数零点(1)实验步骤a. 导入NumPy库;b. 定义函数f(x)和导数f'(x);c. 设置初始值x0;d. 使用牛顿迭代公式进行迭代计算;e. 判断迭代结果是否满足精度要求。
(2)实验结果设函数f(x) = x^2 - 2x - 3,初始值x0 = 1。
经过6次迭代,得到函数零点x ≈ 3。
4. 矩阵的特征值和特征向量求解(1)实验步骤a. 导入NumPy库;b. 定义系数矩阵A;c. 使用NumPy的linalg.eig()函数求解特征值和特征向量。
大学计算方法实验报告
![大学计算方法实验报告](https://img.taocdn.com/s3/m/eebd9515bed5b9f3f90f1cb9.png)
《计算方法》实验报告实验题目实验报告1:非线性方程组的求解···················P1~2实验报告2:线性方程组解法·······················P3~4 实验报告3:Lagrange 插值多项式··················P5~7姓名:学号:班级:指导老师:时间:专业 序号 日期实验报告1:非线性方程组的求解【实验目的】1.用MATLAB 来实践进行牛顿法的变形,即对牛顿法进行了修正,使其应用更为方便,掌握用MATLAB 运用割线法求解非线性方程组。
2.运用MATLAB 进行隐函数作图。
【实验内容】[方法] 设a,b 为迭代初值,求两点(a,f(a)) 与 (b,f(b)) 的连线(割线)与 x 轴的交点记为 c ,再把迭代初值换成 b,c,重复计算.[要求] 把下面程序复制为新的 M-文件,去掉开头的 %再把 '?' 部分改写正确就是一个完整的程序,找前面一个例子试算【解】在牛顿迭代公式中用差商代替导数。
带入初值(a,f(a)),(b,f(b)),两点的连线与x 轴的交点作为c ,再把迭代初值换为b ,c ,重复计算。
【计算机求解】以y= x-exp(-x)为例初值a=0,b=1,误差不超过1.0*10^(-5)进行计算。
《计算方法》实验报告材料
![《计算方法》实验报告材料](https://img.taocdn.com/s3/m/10185075a22d7375a417866fb84ae45c3b35c22c.png)
《计算方法》实验报告材料引言:计算方法是一门应用数学的基础课程,通过实验教学,能够帮助学生更好地理解和掌握各种数值计算的方法和技巧。
本次实验旨在通过编程实现一些常用的数值计算方法,并通过对比分析实验结果,验证方法的有效性和可行性。
实验一:插值算法插值算法是利用已知的数据点,构建一个连续函数以逼近数据的方法。
本次实验中使用的插值算法为拉格朗日插值和牛顿插值。
通过编程实现这两种算法,并选取若干个数据点进行测试,得到插值函数的结果。
通过比较原始数据和插值函数的结果,可以验证插值算法的准确性和可行性。
实验二:方程求解方程求解是数值计算中的一个重要问题,求解非线性方程、线性方程组和特征值问题等都需要采用相应的迭代方法。
本次实验中,我们实现了常用的牛顿迭代法和二分法,并选择数学问题进行求解。
通过比较实验结果和理论值的误差,可以验证求解方法的精确性和可行性。
实验三:数值积分数值积分是利用数值方法对定积分进行近似求解的过程。
本次实验中,我们实现了矩形法、梯形法和辛普森法等常用的数值积分方法,并选取若干函数进行数值积分的计算。
通过比较数值积分的结果和解析解或数值解的误差,可以验证数值积分方法的准确性和可行性。
实验四:常微分方程求解常微分方程求解是数值计算中的一个重要问题,常常涉及到物理、化学、生物等科学领域。
本次实验中,我们实现了欧拉方法和龙格-库塔方法等常用的常微分方程求解算法,并选取若干常微分方程进行求解。
通过比较数值解和解析解或数值解的误差,可以验证常微分方程求解方法的精确性和可行性。
实验五:线性方程组求解线性方程组求解是数值计算中的一个重要问题,常常涉及到矩阵的运算和迭代方法。
本次实验中,我们实现了高斯消元法和追赶法等常用的线性方程组求解算法,并选择一些矩阵进行求解。
通过比较数值解和解析解或数值解的误差,可以验证线性方程组求解方法的精确性和可行性。
结论:通过本次实验,我们掌握了插值算法、方程求解、数值积分、常微分方程求解和线性方程组求解等常用的计算方法。
计算方法上机实验报告
![计算方法上机实验报告](https://img.taocdn.com/s3/m/3ac4bca8700abb68a882fb1e.png)
. 《计算方法》上机实验报告班级:XXXXXX小组成员:XXXXXXXXXXXXXXXXXXXXXXXXXXXX任课教师:XXX二〇一八年五月二十五日前言通过进行多次的上机实验,我们结合课本上的内容以及老师对我们的指导,能够较为熟练地掌握Newton 迭代法、Jacobi 迭代法、Gauss-Seidel 迭代法、Newton 插值法、Lagrange 插值法和Gauss 求积公式等六种算法的原理和使用方法,并参考课本例题进行了MATLAB 程序的编写。
以下为本次上机实验报告,按照实验内容共分为六部分。
实验一:一、实验名称及题目: Newton 迭代法例2.7(P38):应用Newton 迭代法求在附近的数值解,并使其满足.二、解题思路:设'x 是0)(=x f 的根,选取0x 作为'x 初始近似值,过点())(,00x f x 做曲线)(x f y =的切线L ,L 的方程为))((')(000x x x f x f y -+=,求出L 与x 轴交点的横坐标)(')(0001x f x f x x -=,称1x 为'x 的一次近似值,过点))(,(11x f x 做曲线)(x f y =的切线,求该切线与x 轴的横坐标)(')(1112x f x f x x -=称2x 为'x 的二次近似值,重复以上过程,得'x 的近似值序列{}n x ,把)(')(1n n n n x f x f x x -=+称为'x 的1+n 次近似值,这种求解方法就是牛顿迭代法。
三、Matlab 程序代码:function newton_iteration(x0,tol) syms z %定义自变量 format long %定义精度 f=z*z*z-z-1; f1=diff(f);%求导 y=subs(f,z,x0);y1=subs(f1,z,x0);%向函数中代值 x1=x0-y/y1; k=1;while abs(x1-x0)>=tol x0=x1;y=subs(f,z,x0); y1=subs(f1,z,x0); x1=x0-y/y1;k=k+1; endx=double(x1) K四、运行结果:实验二:一、实验名称及题目:Jacobi 迭代法例3.7(P74):试利用Jacobi 迭代公式求解方程组要求数值解为方程组的精确解. 二、解题思路:首先将方程组中的系数矩阵A 分解成三部分,即:U D L A ++=,D 为对角阵,L 为下三角矩阵,U 为上三角矩阵。
《计算方法》课内实验报告(实验2)2014
![《计算方法》课内实验报告(实验2)2014](https://img.taocdn.com/s3/m/2e41d38c71fe910ef12df83d.png)
《计算方法》课内实验报告学生姓名:张靖2012309010111及学号:学院:理学院班级:信计121课程名称:计算方法实验题目:插值法与函数逼近指导教师周硕教授姓名及职称:朱振菊实验师2014年11月03日目录一、实验题目 (1)二、实验目的 (1)三、实验内容 (1)四、实验结果 (2)五、实验体会或遇到问题 (8)一、实验题目1.熟悉matlab 编写及运行数值计算程序的方法。
2.进一步理解数值积分的基础理论。
3.进一步掌握应用不同的数值积分方法求解给定的积分并给出数据结果及误差分析。
二、实验目的1.熟悉matlab 编写及运行数值计算程序的方法。
2.进一步理解插值法及函数逼近方法的理论基础。
3.进一步掌握给定数据后应用插值法及函数逼近方法进行数据处理并给出图示结果的实际操作过程。
三、实验内容1.分别用复合梯形求积公式及复合辛普森求积公式计算积分xdx x ln 1⎰,要求计算精度达到410-,给出计算结果并比较两种方法的计算节点数. 2.用龙贝格求积方法计算积分dx x x ⎰+3021,使误差不超过510-.3.用3=n 的高斯-勒让德公式计算积分⎰31sin x e x ,给出计算结果.4.用辛普森公式 (取2==M N ) 计算二重积分.5.005.00dydx e x y ⎰⎰-四、 实验结果1.问题1:计算结果如下表表1问题1求解表复合梯形求积公式:取1210-,n=,n为迭代次数,当迭代12次后,精度达到4 n-;节点数为21=4095复合辛普森求积公式:取1000010-,节点数为n=,n为区间数,取精度为4n+=。
1100012.问题2:计算结果如下表表2问题2求解表龙贝格数值积分:给定被积函数0,被积上限3,精度为510-,龙贝格积分表中行的最大数目13,计算出龙贝格数值积分近似解为10.20759362。
3.问题3:计算结果如下表表3问题3求解表高斯-勒让德积分公式:取3n = ,节点横坐标k x 取,n k A 取585999,,,2n 阶导数e sin x x -,求得高斯-勒让德积分近似解为10.94840256。
计算方法实验报告
![计算方法实验报告](https://img.taocdn.com/s3/m/a03b0afe941ea76e58fa04cd.png)
实验一:误差传播与算法稳定性实验目的:体会稳定性在选择算法中的地位。
实验内容:考虑一个简单的由积分定义的序列10I ,0,1,10nn x dx n a x==+⎰其中a 为参数,分别对0.05a =及15a =按下列两种方法计算。
方案1:用递推公式11,1,2,,10n n I aI n n-=-+= 递推初值可由积分直接得01lna I a+= 方案2:用递推公式111(),,1,,1n n I I n N N a n-=-+=-根据估计式当1n a n ≥+时,11(1)(1)(1)n I a n a n <<+++或当01n a n ≤<+时,11(1)(1)n I a n n<≤++ 取递推初值 当1n a n ≥+时, 11121()2(1)(1)(1)2(1)(1)N N a I I a N a N a a N +≈+=+++++ 当01n a n ≤<+时,111()2(1)(1)N N I I a N N≈+++ 实验要求:列出结果,并对其稳定性进行分析比较,说明原因。
实验二:非线性方程数值解法实验目的:探讨不同方法的计算效果和各自特点 实验内容:应用算法(1)牛顿法;(2)割线法 实验要求:(1)用上述各种方法,分别计算下面的两个例子。
在达到精度相同的前提下,比较其迭代次数。
(I )31080x x +-=,取00x =;(II) 2281(0.1)sin 1.060x x x -+++=,取00x =;(2) 取其它的初值0x ,结果如何?反复选取不同的初值,比较其结果; (3) 总结归纳你的实验结果,试说明各种方法的特点。
实验三:选主元高斯消去法----主元的选取与算法的稳定性问题提出:Gauss 消去法是我们在线性代数中已经熟悉的。
但由于计算机的数值运算是在一个有限的浮点数集合上进行的,如何才能确保Gauss 消去法作为数值算法的稳定性呢?Gauss 消去法从理论算法到数值算法,其关键是主元的选择。
计算方法实验报告
![计算方法实验报告](https://img.taocdn.com/s3/m/2bd571b5551810a6f424863f.png)
实验报告一、求方程f(x)=x^3-sinx-12x+1的全部根, ε=1e -6 1、 用一般迭代法; 2、 用牛顿迭代法;并比较两种迭代的收敛速度。
一、首先,由题可求得:12cos 3)(2'--=x x x f . 其次,分析得到其根所在的区间。
① 令()0=x f ,可得到x x x sin 1123=+-.② 用一阶导数分析得到1123+-x x 和x sin 两个函数的增减区间;再用二阶导数分析得到两个函数的拐点以及凹凸区间.③ 在直角坐标轴上描摹出01123=+-x x 和0sin =x 的图,在图上可以看到他们的交点,然后估计交点所在的区间,即是所要求的根的区间。
经过估计,得到根所在的区间为[]3,4--,[]1,0和[]4,3.1、 一般迭代法 (1)算法步骤:设ε为给定的允许精度,迭代法的计算步骤为:① 选定初值0x .由()0=x f 确定函数()x g ,得等价形式()x g x =. ② 计算()0x g .由迭代公式得()01x g x =.③ 如果ε≤-01x x ,则迭代结束,取1x 为解的近似值;否则,用1x 代替0x ,重复步骤②和步骤③. (2)程序代码: ① 在区间[]3,4--内, 代码: clcx0=-3.5; %初值0xiter_max=100; %迭代的最大次数 ep=1e-6; %允许精度 ε k=0;while k<=iter_max %k 从0开始到iter_max 循环 x1=(sin(x0)+12*x0-1).^(1/3); %代入0x ,算出1x 的值 if abs(x1-x0)<ep %01x x -与允许精度作比较 break ; %条件ε≤-01x x 成立,跳出循环 endx0=x1; %条件ε≤-01x x 不成立,用1x 代替0x k=k+1; %k 加1 endx_star=x1, iter=k %1x 为解的近似值,iter 为迭代次数 运行结果:x_star = -3.4101 ;iter =14②在区间[]1,0内,代码: clcx0=0.5; %初值0xiter_max=100; %迭代的最大次数 ep=1e-6; %允许精度ε k=0;while k<=iter_max %k 从0开始到iter_max 循环 x1=(1/12)*(x0.^3-sin(x0)+1); %代入0x ,算出1x 的值 if abs(x1-x0)<ep %01x x -与允许精度作比较 break ; %条件ε≤-01x x 成立,跳出循环endx0=x1; %条件ε≤-01x x 不成立,用1x 代替0x k=k+1; %k 加1 endx_star=x1, iter=k %1x 为解的近似值,iter 为迭代次数 结果:x_star = 0.07696;iter =6③在区间[]4,3内, 代码: clcx0=3.5; %初值0xiter_max=100; %迭代的最大次数 ep=1e-6; %允许精度ε k=0;while k<=iter_max %k 从0开始到iter_max 循环 x1=(sin(x0)+12*x0-1).^(1/3); %代入0x ,算出1x 的值 if abs(x1-x0)<ep %01x x -与允许精度作比较 break ; %条件ε≤-01x x 成立,跳出循环 endx0=x1; %条件ε≤-01x x 不成立,用1x 代替0x k=k+1; %k 加1 endx_star=x1, iter=k %1x 为解的近似值,iter 为迭代次数 运行结果:x_star = 3.4101 ;iter =102、 牛顿迭代法 (1)算法步骤:① 选定初值0x ,计算()0x f ,()0'x f .② 按公式()()k k k k x f x f x x '1-=+迭代,得新的近似值1+k x ,并计算()1+k x f ,()1'+k x f . ③ 对于给定的允许精度ε,如果ε≤-+k k x x 1,则终止迭代,取1*+≈k x x ;否则,1+=k k ,在转回步骤②计算.(2)程序代码: ①在区间[]3,4--内, clcx1=-3.5; %初值1x k=0;while k<=100 %k 从0开始到100循环 x0=x1; %将初值1x 赋给0xf0=x0.^3-sin(x0)-12*x0+1; %计算()0x f f1=3*x0.^2-cos(x0)-12; %计算()0'x f x1=x0-f0/f1; %计算得到新的近似值1xif abs(x1-x0)< 1.0e-6 %01x x -与允许精度作比较 break ; %条件ε≤-01x x 成立,跳出循环 endk=k+1; %条件ε≤-01x x 不成立,k 加1 endx_star=x1, iter=k %1x 为解的近似值,iter 为迭代次数 运行结果:x_star = -3.4911;iter =2②在区间[]1,0内,clcx1=0.5; %初值1x k=0;while k<=100 %k 从0开始到100循环 x0=x1; %将初值1x 赋给0xf0=x0.^3-sin(x0)-12*x0+1; %计算()0x f f1=3*x0.^2-cos(x0)-12; %计算()0'x f x1=x0-f0/f1; %计算得到新的近似值1xif abs(x1-x0)< 1.0e-6 %01x x -与允许精度作比较 break ; %条件ε≤-01x x 成立,跳出循环 endk=k+1; %条件ε≤-01x x 不成立,k 加1 endx_star=x1, iter=k %1x 为解的近似值,iter 为迭代次数 运行结果:x_star =0.07696 ;iter =3③在区间[]4,3内, clcx1=3.5; %初值1x k=0;while k<=100 %k 从0开始到100循环 x0=x1; %将初值1x 赋给0xf0=x0.^3-sin(x0)-12*x0+1; %计算()0x f f1=3*x0.^2-cos(x0)-12; %计算()0'x f x1=x0-f0/f1; %计算得到新的近似值1xif abs(x1-x0)< 1.0e-6 %01x x -与允许精度作比较 break ; %条件ε≤-01x x 成立,跳出循环 endk=k+1; %条件ε≤-01x x 不成立,k 加1 endx_star=x1, iter=k %1x 为解的近似值,iter 为迭代次数 运行结果:x_star =3.4101;iter =33、运行结果:4、结果分析:从这题的结果可以看出,牛顿迭代法的迭代速度比一般迭代法的速度要快,牛顿法的迭代次数比较少。
计算方法实验报告
![计算方法实验报告](https://img.taocdn.com/s3/m/82515ee4b1717fd5360cba1aa8114431b90d8ea4.png)
计算方法实验报告实验目的:1.掌握计算方法的基本概念和算法;2.熟悉常见计算方法的实现步骤和注意事项;3.学会使用计算方法解决实际问题。
实验内容:1.实现二分法;2.实现牛顿迭代法;3.实现高斯消去法。
实验步骤:1.实现二分法:1.1定义函数f(x);1.2 确定初始区间[a, b]和精度tol;1.3计算区间中点c;1.4判断f(a)和f(c)的符号关系并更新区间;1.5重复步骤1.3和1.4直到满足精度要求。
2.实现牛顿迭代法:2.1定义函数f(x);2.2定义函数f的导数;2.3给定初始点x0;2.4计算f(x0)和f'(x0);2.5计算下一个点的近似值x1=x0-f(x0)/f'(x0);2.6重复步骤2.4和2.5直到满足收敛条件。
3.实现高斯消去法:3.1输入线性方程组的系数矩阵A和右端向量b;3.2构造增广矩阵[A,b];3.3进行主元素消去,得到梯形矩阵U和新的右端向量b;3.4回代求解,得到解向量x。
实验结果分析:1.二分法的主要优点是收敛稳定,但需要事先给定初始区间;2.牛顿迭代法的主要优点是收敛速度快,但需要事先给定初始点和收敛条件;3.高斯消去法的主要优点是适用于任何线性方程组,但需要事先进行主元素消去和回代的操作。
实验总结:通过本次实验,我深入理解了计算方法的基本概念和算法,并掌握了二分法、牛顿迭代法和高斯消去法的实现步骤和注意事项。
这些方法在解决实际问题中具有重要的应用价值。
实验过程中,我也遇到了一些困难和挑战,例如初始值的选择和收敛条件的判断。
通过不断的调试和优化,最终成功解决了这些问题。
本次实验不仅提高了我的编程能力,也增加了我的数学建模能力。
希望今后能够继续深入学习计算方法,并将其应用于更加复杂的实际问题中。
计算方法实验报告
![计算方法实验报告](https://img.taocdn.com/s3/m/2f87916866ec102de2bd960590c69ec3d4bbdb68.png)
1. 熟悉并掌握常用的计算方法,包括数值积分、数值微分、线性方程组求解等。
2. 培养运用计算机进行数值计算的能力。
3. 增强对数值计算误差的分析和判断能力。
二、实验环境1. 操作系统:Windows 102. 编程语言:Python3.83. 库:NumPy、SciPy、Matplotlib三、实验内容1. 数值积分(1)函数:f(x) = x^2(2)区间:[0, 1](3)方法:梯形法、辛普森法、复合梯形法2. 数值微分(1)函数:f(x) = e^x(2)点:x = 1(3)方法:有限差分法、中点法、牛顿法3. 线性方程组求解(1)方程组:2x + 3y - z = 8-x + 2y + 2z = -3x - y + 3z = 5(2)方法:高斯消元法、LU分解法1. 数值积分(1)编写函数f(x) = x^2(2)定义积分区间[0, 1](3)实现梯形法、辛普森法、复合梯形法(4)计算积分结果2. 数值微分(1)编写函数f(x) = e^x(2)定义点x = 1(3)实现有限差分法、中点法、牛顿法(4)计算导数结果3. 线性方程组求解(1)定义方程组系数矩阵A和常数向量b(2)实现高斯消元法、LU分解法(3)求解方程组(4)输出解向量x五、实验结果与分析1. 数值积分(1)梯形法:积分结果约为1.6667(2)辛普森法:积分结果约为1.6447(3)复合梯形法:积分结果约为1.6458分析:三种方法计算结果接近,但辛普森法误差最小。
2. 数值微分(1)有限差分法:导数结果约为2.7183(2)中点法:导数结果约为2.7183(3)牛顿法:导数结果约为2.7183分析:三种方法计算结果一致,误差较小。
3. 线性方程组求解(1)高斯消元法:解向量x = [2, 1, 1](2)LU分解法:解向量x = [2, 1, 1]分析:两种方法求解结果一致,且解向量正确。
六、实验总结本次实验通过Python编程,实现了数值积分、数值微分和线性方程组求解。
《计算方法》实验报告
![《计算方法》实验报告](https://img.taocdn.com/s3/m/88b2bc6ef68a6529647d27284b73f242336c31fe.png)
《计算方法》实验报告一、实验目的本次《计算方法》实验的主要目的是通过实际操作和编程实现,深入理解和掌握常见的计算方法在解决数学问题中的应用。
通过实验,提高我们运用数学知识和计算机技术解决实际问题的能力,培养我们的逻辑思维和创新能力。
二、实验环境本次实验使用的编程语言为 Python,使用的开发工具为 PyCharm。
实验运行的操作系统为 Windows 10。
三、实验内容与步骤1、线性方程组的求解实验内容:使用高斯消元法和LU分解法求解线性方程组。
实验步骤:首先,定义线性方程组的系数矩阵和常数向量。
对于高斯消元法,通过逐步消元将系数矩阵化为上三角矩阵,然后回代求解。
对于 LU 分解法,将系数矩阵分解为下三角矩阵 L 和上三角矩阵 U,然后通过向前和向后代换求解。
2、插值与拟合实验内容:使用拉格朗日插值法、牛顿插值法进行插值计算,并使用最小二乘法进行曲线拟合。
实验步骤:对于拉格朗日插值法和牛顿插值法,根据给定的节点数据计算插值多项式。
对于最小二乘法,根据给定的数据点和拟合函数形式,计算拟合参数。
3、数值积分实验内容:使用矩形法、梯形法和辛普森法计算定积分。
实验步骤:定义被积函数和积分区间。
对于矩形法,将积分区间等分为若干小区间,每个小区间用矩形面积近似积分值。
梯形法通过构建梯形来近似积分值。
辛普森法利用抛物线来近似积分值。
4、常微分方程的数值解法实验内容:使用欧拉法和改进的欧拉法求解常微分方程。
实验步骤:给定常微分方程和初始条件。
按照欧拉法和改进的欧拉法的公式进行迭代计算,得到数值解。
四、实验结果与分析1、线性方程组的求解高斯消元法和 LU 分解法都能成功求解线性方程组,但在计算效率和数值稳定性上可能存在差异。
对于规模较大的线性方程组,LU 分解法通常更具优势。
实验中通过对比不同方法求解相同线性方程组的结果,验证了算法的正确性。
2、插值与拟合拉格朗日插值法和牛顿插值法在给定节点处能够准确插值,但对于节点之外的区域,可能会出现较大偏差。
计算方法实验报告(20210313193700)
![计算方法实验报告(20210313193700)](https://img.taocdn.com/s3/m/1a30b4c00b4e767f5bcfce51.png)
实验报告一、求方程f(x)=x A3-sinx-12x+1 的全部根,€=le-61、用一般迭代法;2、用牛顿迭代法;并比较两种迭代的收敛速度。
一、首先,由题可求得:/W =3x2-cosx-12 .其次,分析得到苴根所在的区间。
①令f(x)- 0,可得到%3 -12x4-1 = sinx.②用一阶导数分析得到X3-12X+ 1和sinx两个函数的增减区间:再用二阶导数分析得到两个函数的拐点以及凹凸区间.③在直角坐标轴上描墓出X3-12X+1= 0和sinx = 0的图,在图上可以看到他们的交点, 然后估计交点所在的区间,即是所要求的根的区间。
经过估计,得到根所在的区间为[-4-3], [0,1]和[3,4].1、一般迭代法(1)算法步骤:设e为给定的允许精度,迭代法的计算步骤为:①选定初值A().|h /(A)=0确定函数g(x),得等价形式x = g(x).②计算gg).由迭代公式得X|=g(x。
).③如果|x,-x0|<^,则迭代结束,取“为解的近似值;否则,用“代替儿,重复步骤②和步骤③.(2)程丿了:代码:①在区间[-4,-3]内,代码:clcx0=-3•5; $初值心iter_max=100; $迭代的最大次数ep=le-6; $允许精度£k=0;while k<=iter_max $]<从0开始至ljiter_max循坯;xl=(sin(x0)+12*x0-l) ."(1/3); 貂弋入儿,算出旺的值if abs (xl-xO) <ep %|x, -x0\与允许精度作比较break;匕条件卜|一对5£成立,跳出循环endxO=xl; 务条件|Xj -X Q\<£不成立,用“代替兀0k=k+l; %kiniendx_star=xl, iter=k 为解的近似值,iter为迭代次数运行结果:x_star = -3.4101 : iter =14②在区间[0,1]内,代码:clcxO=O . 5; $初值旺iter_max=100; $迭代的最大次数ep=le-6; $允许精度gk=0;while k<=iter_max $1<从0开始至ljiter_max循坯;xl=(l/12) * (x0.^3-sin(x0) +1); 貂弋入心,算岀“的值if abs (xl-xO) <ep %|xj -x0|与允许精度作比较break; 务条件\x{ -x()| < s成立,跳出循环endxO=xl;匕条件|x, -X Q\<£不成立,用“代替儿k=k+l; Ek 力mendx_star=xl, iter=k 为解的近似值,iter为迭代次数结果:x_star = 0.07696 ; iter =6③在区间[3,4]内,代码:clcx0=3.5; &初值心iter_max=100; $迭代的最大次数ep=le-6; $允许精度£k=0;while k<=iter_max 靱从0开始至ljiter_max循环xl=(sin(x0)+12*x0-l) (1/3); 訂弋入心,算出“的值if abs(xl-xO)<ep 引召-对与允许精度作比较break; 匕条件-x()| <£•成立,跳岀循环endxO=xl; $条件|Xj -x0| < £不成立,用X]代替X。
华科计算方法上机实验报告
![华科计算方法上机实验报告](https://img.taocdn.com/s3/m/dbb71385cc22bcd126ff0c62.png)
《计算方法》实验报告专业及班级 : 姓名:学号:日期:2013年6月12日一,方程求根1.用牛顿迭代法求解下列方程的正根(1)log(1+x)-x^2=0 x=7.468818e-001求解代码:牛顿迭代法的函数M文件:function x=Newt_n(f_name,x0)x=x0;xb=x+1;k=0;n=50;del_x=0.01;while abs(x-xb)>0.000001k=k+1;xb=x;if k>=n break;end;y=feval(f_name,x);y_driv=(feval(f_name,x+del_x)-y)/del_x;x=xb-y/y_driv;fprintf('k=%3.0f,x=%12.5e,y=%12.5e,yd=%12.5e\n',k,x,y,y_driv); end;fprintf('\n Final answer=%12.6e\n',x);待求方程的函数M文件:function y=eqn_1(x)y=log(x+1)-x^2;运行结果:Newt_n('eqn_1',1)k= 1,x=7.96954e-001,y=-3.06853e-001,yd=-1.51125e+000k= 2,x=7.50200e-001,y=-4.90424e-002,yd=-1.04895e+000k= 3,x=7.46936e-001,y=-3.07003e-003,yd=-9.40663e-001k= 4,x=7.46882e-001,y=-5.03365e-005,yd=-9.33074e-001k= 5,x=7.46882e-001,y=-6.30906e-007,yd=-9.32949e-001Final answer=7.468818e-001(2)e^x-5x=0 x=6.052671e-001待解方程:function y=eqn_1(x)y=exp(x)-5*x^2;运行结果:Newt_n('eqn_2',2)k= 1,x=1.00102e+000,y=-1.26109e+001,yd=-1.26239e+001 k= 2,x=6.88531e-001,y=-2.28918e+000,yd=-7.32552e+000 k= 3,x=6.11607e-001,y=-3.79584e-001,yd=-4.93453e+000 k= 4,x=6.05365e-001,y=-2.69226e-002,yd=-4.31343e+000 k= 5,x=6.05268e-001,y=-4.13312e-004,yd=-4.26254e+000 k= 6,x=6.05267e-001,y=-3.99547e-006,yd=-4.26175e+000 Final answer=6.052671e-001ans =0.6053(3)x^3+2x-1=0 x=4.533977e-001待解方程:function y=eqn_1(x)y=x^3+2*x-1;运行结果:Newt_n('eqn_3',3)k= 1,x=1.89997e+000,y=3.20000e+001,yd=2.90901e+001k= 2,x=1.15047e+000,y=9.65861e+000,yd=1.28868e+001k= 3,x=6.80277e-001,y=2.82368e+000,yd=6.00536e+000k= 4,x=4.82154e-001,y=6.75369e-001,yd=3.40884e+000k= 5,x=4.53984e-001,y=7.63945e-002,yd=2.71198e+000k= 6,x=4.53401e-001,y=1.53570e-003,yd=2.63202e+000k= 7,x=4.53398e-001,y=8.46834e-006,yd=2.63042e+000k= 8,x=4.53398e-001,y=4.41262e-008,yd=2.63041e+000 Final answer=4.533977e-001ans =0.4534(4)(x+2)^0.5-x=0 x=2.0000待解方程:function y=eqn_1(x)y=(x+2)^0.5-x;运行结果:Newt_n('eqn_4',1)k= 1,x=2.02879e+000,y=7.32051e-001,yd=-7.11565e-001 k= 2,x=2.00002e+000,y=-2.16052e-002,yd=-7.51049e-001 k= 3,x=2.00000e+000,y=-1.72788e-005,yd=-7.50157e-001 k= 4,x=2.00000e+000,y=-3.60276e-009,yd=-7.50156e-001 Final answer=2.000000e+000ans =2.00002.先用图解法确定初始点,然后再求方程 x sin x −1 = 0 x ∈[0,10]的所有根。
计算方法实验报告
![计算方法实验报告](https://img.taocdn.com/s3/m/ae930ffed0d233d4b14e697b.png)
y_0=y_1;
n--;
if (n%3==0) printf("\n");
}
}
程序运行结果:
%有效的算法%
#include<stdio.h>
#include<math.h>
void main()
{
double y_0=log(5),y_1;
int n=1;
计算方法实验报告
班级:学号:姓名:成绩:
1 舍入误差及稳定性
一、实验目的
(1)通过上机编程,复习巩固以前所学程序设计语言及上机操作指令;
(2)通过上机计算,了解舍入误差所引起的数值不稳定性
二、实验内容
1、用两种不同的顺序计算 ,分析其误差的变化
2、已知连分数 ,利用下面的算法计算 :
写一程序,读入 计算并打印()程序设计:#include<stdio.h>
#include<math.h>
void main()
{
float n=0.0;
int i;
float m=0.0;
int j;
for(i=1;i<=10000;i++)//顺序计算
{
n=n+1.0/(i*i);
}
printf("%-100f",n);
N=30000时:
②用从大到小顺序的C语言算法分别计算S1000,S10000,S30000,结果如下:
N=1000时:
N=10000时:
N=30000时:
四、总结
通过这次上机,学习了解了舍入误差在不同算法时对结果的影响不同,稳定的算法才能获得正确的结果。
计算方法课程实践实验报告范文
![计算方法课程实践实验报告范文](https://img.taocdn.com/s3/m/d555b3e4d0f34693daef5ef7ba0d4a7303766c58.png)
计算方法课程实践实验报告范文In this experiment, we were tasked with applying different computational methods to solve real-world problems. The experiment aimed to enhance our understanding of how these methods could be employed to tackle complex issues in various disciplines. 在这个实验中,我们的任务是应用不同的计算方法来解决现实世界中的问题。
这个实验旨在加强我们对这些方法如何在各种学科中应用来处理复杂问题的理解。
The first part of the experiment involved utilizing numerical methods to solve differential equations. We were given a set of initial conditions and parameters and asked to solve for the behavior of the system over time. This exercise provided valuable insight into the practical application of numerical methods in modeling dynamic systems. 实验的第一部分涉及利用数值方法来解决微分方程。
我们被给定了一组初始条件和参数,并被要求解出系统随时间的行为。
这个练习为模拟动态系统中数值方法的实际应用提供了宝贵的见解。
The second part of the experiment involved the application of optimization algorithms to solve a resource allocation problem. Wehad to determine the optimal distribution of resources to maximize a certain objective function while considering different constraints. This part of the experiment showcased the effectiveness of computational methods in finding optimal solutions for complex decision-making problems. 实验的第二部分涉及应用优化算法来解决资源分配问题。
华南理工大学《计算方法》实验报告
![华南理工大学《计算方法》实验报告](https://img.taocdn.com/s3/m/25a780a669dc5022aaea0076.png)
华南理工大学《计算方法》实验报告华南理工大学《计算方法》实验报告学院计算机科学与工程专业计算机科学与技术(全英创新班)学生姓名 -------学生学号 ------------指导教师布社辉课程编号 145022课程学分 3 分起始日期 2015年5月28日实验题目一Solution of Nonlinear Equations--Bracketing method and Newton-Raphson method姓名:xxxxxx学号:xxxxxxxxxxxx时间:2015年5月28日【题目】1.Find an approximation (accurate to 10 decimal places) for the interest rate I thatwill yield a total annuity value of $500,000 if 240 monthly payments of $300 are made.2.Consider a spherical ball of radius r = 15 cm that is constructed from a variety ofwhite oak that has a density of ρ = 0.710. How much of the ball (accurate to eight decimal places) will be submerged when it is placed in water?3.To approximate the fixed points (if any) of each function. Answers should beaccurate to 12 decimal places. Produce a graph of each function and the line y = x that clearly shows any fixed points.(a) g(x) = x5 − 3x3 − 2x2 + 2(b) g(x) = cos(sin(x))【实验分析】1.Assume that the value of the k-th month is , the value next month is, whereSo the total annuity value after 240 months is :To find the rate I that satisfied A=5000000, we can find the solution to the equationThe Bisection method can be used to find the solution of .2.The mass of water displaced when a sphere is submerged to a depth d is:The mass of the ball isAppl ying Archimedes’ law, , produce the following equations:So g(d)=, and Bisection method can be used to find the root.3.In this problem, the target is to find the fixed point by fixed point iteration. So wecan use fixed point iteration method to solve the problem.(a),so ,Plot the function g(x) and y=x, it can be seen that there are three fixed point, where ,It can be solved that:for,for,for,So, ,,, they are all repelling fixed point, fixed point iteration method cannot converge to them.(b). From the graph below we know that the fixed point liesbetween 0.5 and 1. Where . So the fixed point is attractive fixed point. I initially guess that p=0.75, and apply fixed point iteration method to find the solution.【实验过程和程序】1.The value of g(I) will change significantly but little change of I, so that it’sunwise to plot the figure about I and g(I). Instead, the interest will be bound by [0,1] by common sense. So I set the left end point of Bisection method to 0, right end point to 1. The precision is set to 1e-10.Code for g(I) is :Code for applying Bisection method is :2.The since the density of the ball is smaller than water, so depth submerged shouldnot be larger than 2r, and not less than 0. So I set the left end point of Bisection method to 0 and right to 2r.Code for g(d) is :Code for applying Bisection method is :3.(a)Code for function g:Code for applying fixed point iteration:Code for plot the graph:(b)Code of this problem is similar to the previous one, so it’s not illustrated here.【实验结果】1.Result for the program is :So the annual interest rate is 31.78710938%2.X-axis is the depth and Y-axis is for g(d). Blue line is the function value versusdepth. Red line is y=0. It can be seen from the graph that the solution is about 19.And by using Bisection method, the solution depth=19.306641cm is found, with8 significance bit.3.(a) No fixed point can be found. By solving this equation, we can find that x=2 isone of the solution. If we set the initial point to 2.00000000001 or 1.9999999, the result is the same:. If we set initial point to 2, the point can be found:. This is consistent with the conclusion discussed in the previous part.(b)The fixed point p=0.768169156736 is found:实验题目二Solution of Linear Systems--Gaussian elimination and pivoting姓名:xxxxxx学号:xxxxxxxxxxxx时间:2015年5月28日【题目】Find the sixth-degree polynomial y = a1 + a2x + a3x^2 + a4x^3 + a5x^4 + a6x^5 + a7x^6 that passes through (0, 1), (1, 3), (2, 2), (3, 1), (4, 3), (5, 2), and (6, 1). Use the plot command to plot the polynomial and the given points on the same graph. Explain any discrepancies in your graph.【实验分析】To find the coefficient of each term, we can substitute all the given point to the equations and construct an argument matrix AX=B. Then perform triangular factorization and solve the upper-triangular and lower triangular matrix by back substitution method.Since det(A) = 2.4883e+07 ≠ 0,there exist a unique solution for AX=B.【实验过程和程序】Code for construct coefficient matrix A:Code for construct matrix B:Solve X by triangular factorization and back substitution method:【实验结果】The result for coefficient matrix:The graph of the polynomial is shown below. Blue lines denotes the graph for the polynomial and red points are the sample points. This polynomial fits the points well.Iterat ion of Linear System’s solution--Seidel iteration姓名:xxxxxx学号:xxxxxxxxxxxx时间:2015年5月28日【题目】(a)Start with P0 = 0 and use Jacobi iteration to find Pk for k = 1, 2, 3. Will Jacobi iteration converge to the solution?(b) Start with P0 = 0 and use Gauss-Seidel iteration to find Pk for k = 1, 2, 3. Will Gauss-Seidel iteration converge to the solution?【实验分析】In this problem we only need to construct the coefficient matrix and matrix B and then apply the Jacobi Iteration method and Gauss-Seidel Iteration method to find the solution. SinceA satisfy strictly diagonally dominant, so the iteration steps will converge.【实验过程和程序】【实验结果】Points in first three iteration is shown below. Both this two method will converge.Interpolation--Spline interpolation姓名:xxxxxx学号:xxxxxxxxxxxx时间:2015年5月28日【题目】The measured temperatures during a 5-hour period in a suburb of Los Angeles on November 8 are given in the following table.(a)To construct a Lagrange interpolatory polynomial for the data in the table.(b)To estimate the average temperature during the given5-hour period.(c)Graph the data in the table and the polynomial frompart (a) on the same coordinate system. Discuss thepossible error that can result from using thepolynomial in part (a) to estimate the averagetemperature.【实验分析】(a)X=[1,2,3,4,5,6] Y=[66,66,65,64,63,63], the target is to construct a Lagrangeinterpolatory polynomial for X and Y. We can apply the Lagrange approximation method to solve it.(b)After we get the interpolatory polynomial, we can use a great amount of samplepoints and estimate the function value of them and take the mean.(c)Error contain in Lagrange interpolatory polynomial of 5-degree is:, where Error on the average temperature equals to the mean of , which is:Error ====240.06【实验过程和程序】(a)Construct X and Y, and solve it by Lagrange approximation:(b)After we get the polynomial function, we calculate function value on a set ofsample points and get their mean as the average temperature.(c)plot X_new and Y_new in part(b) is equivalent to plot P N(x). And error bound canonly be obtain by analyzing since f(x) is unknown.【实验结果】(a) We can get the result:Which means that:(b)By taking the mean, the average temperature is 64.5℉:(c)The graph for polynomial and the sample points is shown below. Error bound forthe average temperature is 240.06.实验题目五Curve Fitting--Least mean square error姓名:xxxxxx学号:xxxxxxxxxxxx时间:2015年5月28日【题目】1.The temperature cycle in a suburb of Los Angeles on November 8 is given in theaccompanying table. There are 24 data points.(a)Follow the procedure outlined in Example5.5 (use the fmins command) to find theleast-squares curve of the form f (x) = Acos(Bx)+C sin(Dx)+ E for the given set ofdata.(b)Determine E2( f ).(c)Plot the data and the least-squares curvefrom part (a) on the same coordinatesystem.【实验分析】(a)In this problem. The hour 1~midnight p.m. can be map to 1~12, 1~noon a.m. canbe map to 13~24. So hour and temperature can be used to fit the curve. To get the curve with minimum error, which is define as:The best A, B, C, D, E can be found when:(b)is defined as:In our case,【实验过程和程序】(a) The function of the error is:Using command, A B C D E can be solved. (b) By the function solved by part(a), we can calculate for each sample pointand then calculate :(c) Code to plot the line:【实验结果】(a)The result for parameter A B C D E is:So the least-squares curve is :(b)Error for f(x) is:(c)Graph for least-squares curve and sample points is:Notice that the result of function fminesarch is sensitive about the input seeds.Firstly the seed is [1 1 1 1 1], and get the result [0.1055 -2.1100 0.9490 -4.869861.0712]. In second step, seed is [0.1 2 1 -5 61] and result is [0.0932 2.11141.175 -5.127 61.0424]. By 4 iterations, I find a good fit of the data and get the seed[1 0.1 1 1 60].Numerical Integration--Automatically select the integration step of the trapezoidal method姓名:xxxxxx学号:xxxxxxxxxxxx时间:2015年5月28日【题目】(i) Approximate each integral using the compositetrapezoidal rule with M = 10.(ii) Approximate each integral using the compositeSimpson rule with M = 5.【实验分析】The formula of each function is given in this problem, so we just need to use composite trapezoidal rule and composite Simpson rule to solve them.【实验过程和程序】Function of the six equations:Code for calculation:【实验结果】Solution of Differential Equations--Euler’s Method and Heun’s Method姓名:xxxxxx学号:xxxxxxxxxxxx时间:2015年5月28日【题目】(Supplement) A skydiver jumps from a plane, and up to the moment he opens theparachute the air resistance is proportional to (v represents velocity). Assume that the time interval is [0,6] and hat the differential equation for the downward direction isover [0,6] with v(0)=0Use Euler’s method with h=0.05 and estimate v(6).【实验分析】The function of , where left end point is 0 and right is 6, with initial point v(0)=0. So we can apply Euler’s method directory to solve the problem.【实验过程和程序】Function of :Code of applying Euler’s method:【实验结果】….,so v(6)=10.0726.。
计算方法实验上机报告(完整版)
![计算方法实验上机报告(完整版)](https://img.taocdn.com/s3/m/159f2c2ae97101f69e3143323968011ca200f759.png)
简单迭代法#include<stdio.h>#include<math.h>#define x0 3.0#define MAXREPT 1000#define EPS 1E-6#define G(x) pow(12*x+sin(x)-1,1.0/3)void main(){int i;double x_k=x0,x_k1=x0;printf("k\txk\n");for(i=0;i<MAXREPT;i++){printf("%d\t%g\n",i,x_k1);x_k1=G(x_k);if (fabs(x_k1-x_k)<EPS){printf("THE ROOT IS x=%g,k=%d\n",x_k1,i);return;}x_k=x_k1;}printf("AFTER %d repeate,no solved.\n",MAXREPT);}结果牛顿迭代法一#include<stdio.h>#include<math.h>#define x0 3.0#define MAXREPT 1000#define EPS 1E-6#define G(x) x-(pow(x,3)-sin(x)-12*x+1)/(3*pow(x,2)-cos(x)-12) void main(){int i;double x_k=x0,x_k1=x0;printf("k\txk\n");for(i=0;i<MAXREPT;i++){printf("%d\t%g\n",i,x_k1);x_k1=G(x_k);if (fabs(x_k1-x_k)<EPS){printf("THE ROOT IS x=%g,k=%d\n",x_k1,i);return;}x_k=x_k1;}printf("AFTER %d repeate,no solved.\n",MAXREPT);}结果埃特金加速法#include<stdio.h>#include<math.h>#define x0 3.0#define MAXREPT 1000#define EPS 1E-6#define G(x) (pow(x,3)-sin(x)+1)/12void main(){int i;double x1=x0,x2=x0;double z,y;printf("k\tx1\tx2\txk\n");for(i=0;i<MAXREPT;i++){if(i==0)printf("%d\t\t\t%g\n",i,x2);elseprintf("%d\t%g\t%g\t%g\n",i,y,z,x2);y=G(x1);z=G(y);x2=z-((z-y)*(z-y))/(z-2*y+x1);if (fabs(x2-x1)<EPS){printf("THE ROOT IS x=%g,k=%d\n",x2,i);return;}x1=x2;}printf("AFTER %d repeate,no solved.\n",MAXREPT);} 结果牛顿迭代法二#include<stdio.h>#include<math.h>#define x0 1.5#define MAXREPT 1000#define EPS 1E-6#define G(x) x-(pow(x,3)+pow(x,2)-3*x-3)/(3*pow(x,2)+2*x-3) void main(){int i;double x_k=x0,x_k1=x0;printf("k\txk\n");for(i=0;i<MAXREPT;i++){printf("%d\t%g\n",i,x_k1);x_k1=G(x_k);if (fabs(x_k1-x_k)<EPS){printf("THE ROOT IS x=%g,k=%d\n",x_k1,i);return;}x_k=x_k1;}printf("AFTER %d repeate,no solved.\n",MAXREPT);}结果弦截法#include<stdio.h>#include<math.h>#define x0 0#define x1 1.5#define MAXREPT 1000#define EPS 1E-6#define G(x) pow(x,3)+pow(x,2)-3*x-3void main(){int i;double x_k=x0,x_k1=x1,x_k2=0;double y,z;printf("k\txk\n");for(i=0;i<MAXREPT;i++){printf("%d\t%g\n",i,x_k2);y=G(x_k);z=G(x_k1);x_k2=x_k1-(z*(x_k1-x_k))/(z-y);if (fabs(x_k2-x_k1)<EPS){printf("THE ROOT IS x=%g,k=%d\n",x_k2,i);return;}x_k=x_k1;x_k1=x_k2;}printf("AFTER %d repeate,no solved.\n",MAXREPT); } 结果高斯顺序消元法#include<stdio.h>#include<math.h>#define N 4static double aa[N][N+1]={{2,4,0,1,1},{3,8,2,2,3},{1,3,3,0,6},{2,5,2,2,3}}; int gauss(double a[][N+2],double x[]);void putout(double a[][N+2]);void main(){int i,j,det;double a[N+1][N+2],x[N+1];for(i=1;i<=N;i++)for(j=1;j<=N+1;j++)a[i][j]=aa[i-1][j-1];det=gauss(a,x);if(det!=0)for(i=1;i<=N;i++)printf(" x[%d]=%g",i,x[i]);printf("\n");}int gauss(double a[][N+2],double x[]){int i,j,k;double c;putout(a);for(k=1;k<=N-1;k++){ if(fabs(a[k][k])<1e-17){printf("\n pivot element is 0.fail!\n");return 0;}for(i=k+1;i<=N;i++){c=a[i][k]/a[k][k];for(j=k;j<=N+1;j++){a[i][j]=a[i][j]-c*a[k][j];}}putout(a);}if(fabs(a[N][N])<1e-17){printf("\n pivot element is 0.fail!\n");return 0;}for(k=N;k>=1;k--){x[k]=a[k][N+1];for(j=k+1;j<=N;j++){x[k]=x[k]-a[k][j]*x[j];}x[k]=x[k]/a[k][k];}return(1);}void putout(double a[][N+2]){for(int i=1;i<=N;i++){for(int j=1;j<=N+1;j++)printf("%-15g",a[i][j]);printf("\n");}printf("\n");}结果雅克比迭代法#include<stdio.h>#include<math.h>#define N 5#define EPS 0.5e-4static double aa[N][N]={{4,-1,0,-1,0},{-1,4,-1,0,-1},{0,-1,4,-1,0},{-1,0,-1,4,-1},{0,-1,0,-1,4}}; static double bb[N]={2,1,2,1,2};void main(){int i,j,k,NO;double a[N+1][N+1],b[N+1],x[N+1],y[N+1];double d,sum,max;for(i=1;i<=N;i++){for(j=1;j<=N;j++)a[i][j]=aa[i-1][j-1];b[i]=bb[i-1];}printf("\n 请输入最大迭代次数(尽量取大值!)NO:");scanf("%d",&NO);printf("\n");for(i=1;i<=N;i++)x[i]=0;k=0;printf(" k",' ');for(i=1;i<=N;i++)printf("%8cx[%d]",' ',i);printf("\n 0");for(i=1;i<=N;i++)printf("%12.8g",x[i]);printf("\n");do{for(i=1;i<=N;i++){sum=0.0;for(j=1;j<=N;j++)if(j!=i) sum=sum+a[i][j]*x[j];y[i]=(-sum+b[i])/a[i][i];}max=0.0;for(i=0;i<=N;i++){d=fabs(y[i]-x[i]);if(max<d) max=d;x[i]=y[i];}printf("%6d",k+1);for(i=1;i<=N;i++)printf("%12.8g",x[i]);printf("\n");k++;}while((max>=EPS)&&(k<NO));printf("\nk=%d\n",k);if(k>=NO) printf("\nfail!\n");elsefor(i=1;i<=N;i++)printf("x[%d]=%g\t",i,x[i]);}结果拉格朗日插值多项式#include<stdio.h>#include<math.h>#define N 4doublex[N]={0.56160,0.56280,0.56401,0.56521},y[N]={0.82741,0.82659,0.82577,0.82495}; void main(){double x=0.5635;double L(double xx);double lagBasis(int k,double xx);void output();output();printf("\n近似值L(%g)=%g\n",x,L(x));}double lagBasis(int k,double xx){double lb=1;int i;for(i=0;i<N;i++)if(i!=k) lb*=(xx-x[i])/(x[k]-x[i]);return lb;}double L(double xx){double s=0;int i;for(i=0;i<=N;i++)s+=lagBasis(i,xx)*y[i];return s;}void output(){int i;printf("\n各节点信息:\nxi:");for(i=0;i<N;i++)printf("\t%g",x[i]);printf("\nyi:");for(i=0;i<N;i++)printf("\t%g",y[i]);}结果牛顿插值多项式#include <math.h>#include <stdio.h>int a;#define M 4double x[M+1]={0.4,0.55,0.65,0.8,0.9},y[M+1]={0.41075,0.57815,0.69675,0.88811,1.02652}; void main(){double x;printf("输入x=");scanf("%lf",&x);printf("次数:");scanf("%d",&a);double N(double xx,int a);void output();output();printf("\n%d次牛顿插值多项式N(%g)=%g\n",a,x,N(x,a));}double N(double xx,int a){double s=y[0],d=1;int i,j;double df[M+1][M+1];for(i=0;i<=M;i++)df[i][0]=y[i];for(j=1;j<=a;j++)for(i=j;i<=a;i++)df[i][j]=(df[i][j-1]-df[i-1][j-1])/(x[i]-x[i-j]);printf("\nx\tf(x)\t");for(j=1;j<=a;j++) printf("%5d阶",j);for(i=0;i<=a;i++){printf("\n%g\t%g",x[i],y[i]);for(j=1;j<=i;j++)printf("\t%7.5g",df[i][j]);}for(i=1;i<=a;i++){d*=(xx-x[i-1]);s+=df[i][i]*d;}return s;}void output(){int i;printf("\n各节点信息:\nxi:");for(i=0;i<=M;i++)printf("\t%7g",x[i]);printf("\nyi:");for(i=0;i<=M;i++)printf("\t%7g",y[i]);}结果复合梯形公式#include<stdio.h>#include<math.h>#define f(x) 1/(x*x+1)#define Pi 3.1415926void main(){double a=0,b=1;double T,h,x;int n,i;printf("please input n:");scanf("%d",&n);h=(b-a)/n;x=a;T=0;for(i=1;i<n;i++){x+=h;T+=f(x);}T=(f(a)+2*T+f(b))*h/2;printf("T(%d)=%g\n",n,T);printf("The exact value is %g\n",Pi/4);}复合辛普森公式#include<stdio.h>#include<math.h>#define f(x) 1/(1+x*x)#define Pi 3.1415926void main(){double a=0,b=1;double S,h,x;int n,i;printf("please input Even n:");scanf("%d",&n);h=(b-a)/n;x=a; S=0;for(i=1;i<n;i++){x+=h;if(i%2==0) S+=2*f(x);else S+=4*f(x);}S=(f(a)+S+f(b))*h/3;printf("S(%d)=%g\n",n,S);printf("The exact value is %g\n",Pi/4);}龙贝格公式加速#include<stdio.h>#include<math.h>#define f(x) sin(x)/(1+x)#define M 3void main(){double a=0,b=1;double Long(double a,double b);printf("近似值I=%g\n",Long(a,b));}double Long(double a,double b){int n=1,i=1,j=1;double T[M+1][M+1],h,x,sum;h=b-a;T[0][0]=(f(a)+f(b))/2;for(j=1;j<=3;j++){x=a;h/=2;n*=2;sum=0;for(i=1;i<=n;i+=2){x=a+i*h;sum+=f(x);}T[j][0]=T[j-1][0]/2+h*sum;}for(i=1;i<=M;i++)for(j=1;j<=i;j++){T[i][j]=(pow(4,j)*T[i][j-1]-T[i-1][j-1])/(pow(4,j)-1);}printf("k\tT0\tT1\tT2\tT3\n");for(i=0;i<=M;i++){printf("%d",i);for(j=0;j<=i;j++)printf(" %g",T[i][j]);printf("\n");}return T[M][M];}。
计算方法实验报告
![计算方法实验报告](https://img.taocdn.com/s3/m/7e7c8599964bcf84b9d57bb7.png)
2019年计算方法(B)实验报告姓名:学号:专业:课程:计算方法(B)目录一、实验综述 (1)二、实验内容 (1)2.1 实验一 (1)2.2 实验二 (2)2.3 实验三 (3)2.4 实验四 (4)2.5 实验五 (6)三、思考总结 (7)附件A1 (8)附件A2 (9)附件A3 (10)附件A4 (12)附件A5 (14)一、实验综述计算方法在工程实践中得到了广泛的应用,是理工类研究生必备的知识技能。
按照2019年计算方法课程学习要求,本文对计算方法上机题目进行了算法设计、分析,利用matlab 2019b版本对算法进行实现,最终形成了实验报告。
以下为本次实验报告具体内容,包括五个实验部分和一个思考总结部分。
二、实验内容2.1 实验一2.1.1 实验题目用Jacobi迭代和Gauss-Seidel迭代解电流方程组,使各部分电流的误差均小于10-3。
2.1.2 算法分析a)首先列出方程组的系数矩阵A以及等式右端的矩阵b,A=[28,-3,0,0,0;-3,38,-10,0,-5;0,-10,25,-15,0;0,0,-15,45,0;0,-5,0,0,30 ];b=[10;0;0;0;0];为了验证A是否收敛,我们通过判断系数矩阵A是否为严格对角占优矩阵进行确定。
如果是,则可以进行Jacobi迭代和Gauss-Seidel迭代(利用matlab程序验证后,证明了矩阵A为严格对角占优矩阵);如果不是,则需要采用其他方法进行判断迭代是否收敛。
b)对矩阵A分裂成三部分,,其中D为A的对角矩阵,E为A的下三角矩阵的相反数,F为A的上三角矩阵的相反数。
c) Jacobi迭代。
取x得初始向量为x=[0;0;0;0;0],利用迭代公式进行循环计算,当的无穷范数小于10-3,即,停止循环。
d) Gauss-Seidel迭代。
取x得初始向量为x=[0;0;0;0;0],利用迭代公式进行循环计算,当的无穷范数小于10-3,即,停止循环。
计算方法实验报告
![计算方法实验报告](https://img.taocdn.com/s3/m/f4d5d70ea8114431b90dd8eb.png)
Matelab 实习三一.题目1.对于给函数f(x)=1/(1+25x^2)在区间[-1,1]上取Xi=-1+0.2i (i=0,1,…10),试求三次曲线拟合,试画出拟合曲线并打印方程,与第2章计算实习题2的结果比较。
试求3次、4次多项式的曲线拟合,再根据数据曲线形状,求一个另外函数的拟合曲线,用图示数据曲线及相应的三种拟合曲线。
3.使用快速傅里叶变换确定函数)cos()(2x x x f =在[-pi,pi]上的16次三角插值多项式。
二.实验理论1.,由此求出格拉姆矩阵G ,可写成矩阵形式:Ga=d 。
实验中,只要求出矩阵G 和向量d ,即可由Ga=d 得出ai(i=0,1,2 …n),进而可求出S(x)=∑ai*x^i.2.,可写成矩阵形式: Ga=d 。
实验中,只要求出矩阵G 和向量d ,即可由Ga=d 得出ai(i=0,1,2 …n)进而可求出S(x)=∑ai*x^i.3.FFT 计算公式如下:其中q=1,2,…p,k=0,1,…2^(p -q)-1,j=0,1,…2^(q -1)-1。
经过不断的二分迭代,可得到Ap(j).即Ck ,而ak=real(Ck),bk=image(Ck),∑-=+++=11)sin cos (21n k n k k a ky b ky a S三.实验程序1.function [ output_args ] = triRunge(m)n=m+1;x=zeros(1,11);y=zeros(1,11);x(1)=-1;for i =1:10x(i+1)=-1+0.2*i;end ```·,1,0,]),(==∑k d a k j j k ϕϕ ```·,1,0,]),(==∑k d a k j j k ϕϕ ```·,1,0,]),(==∑k d a k j j k ϕϕ ```·,1,0,]),(==∑k d a k j j k ϕϕ []12111111-q 11111)22(-)2()22(A )22()2()2(A -----------+++=++++++=+q k p q q q q q q p q q q q q q w j k A j k A j k j k A j k A j kfor i=1:11y(i)=1/(1+25*x(i)^2);endd=zeros(n,n); f=zeros(1,n); for i=1:nfor j=1:nfor k=1:11d(i,j)=d(i,j)+x(k)^(i+j-2);endendendfor i=1:nfor k=1:11f(i)=f(i)+x(k)^(i-1)*y(k);endenda=f/d; syms x;g=a(n)*x+a(n-1);for i=1:n-2g=g*x+a(n-2-i+1);endvpa(g);g=collect(g);g=vpa(g,3); ezplot(g,[-1,1]);gtext('拟合曲线');k=char(g);hold onezplot('1/(1+25*x^2)',[-1,1]);gtext('龙格函数1/(1+25x^2)');gtext(k)title('三次曲线拟合');end注:蓝色为3次拟合曲线,红色为龙格函数曲线可以看出:三次拟合曲线与龙格函数基本不重合,拟合效果差。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
华南理工大学《计算方法》实验报告华南理工大学《计算方法》实验报告学院计算机科学与工程专业计算机科学与技术(全英创新班)学生姓名 -------学生学号 ------------指导教师布社辉课程编号 145022课程学分 3 分起始日期 2015年5月28日实验题目一Solution of Nonlinear Equations--Bracketing method and Newton-Raphson method姓名:xxxxxx学号:xxxxxxxxxxxx时间:2015年5月28日【题目】1.Find an approximation (accurate to 10 decimal places) for the interest rate I thatwill yield a total annuity value of $500,000 if 240 monthly payments of $300 are made.2.Consider a spherical ball of radius r = 15 cm that is constructed from a variety ofwhite oak that has a density of ρ = 0.710. How much of the ball (accurate to eight decimal places) will be submerged when it is placed in water?3.To approximate the fixed points (if any) of each function. Answers should beaccurate to 12 decimal places. Produce a graph of each function and the line y = x that clearly shows any fixed points.(a) g(x) = x5 − 3x3 − 2x2 + 2(b) g(x) = cos(sin(x))【实验分析】1.Assume that the value of the k-th month is , the value next month is, whereSo the total annuity value after 240 months is :To find the rate I that satisfied A=5000000, we can find the solution to the equationThe Bisection method can be used to find the solution of .2.The mass of water displaced when a sphere is submerged to a depth d is:The mass of the ball isAppl ying Archimedes’ law, , produce the following equations:So g(d)=, and Bisection method can be used to find the root.3.In this problem, the target is to find the fixed point by fixed point iteration. So wecan use fixed point iteration method to solve the problem.(a),so ,Plot the function g(x) and y=x, it can be seen that there are three fixed point, where ,It can be solved that:for,for,for,So, ,,, they are all repelling fixed point, fixed point iteration method cannot converge to them.(b). From the graph below we know that the fixed point liesbetween 0.5 and 1. Where . So the fixed point is attractive fixed point. I initially guess that p=0.75, and apply fixed point iteration method to find the solution.【实验过程和程序】1.The value of g(I) will change significantly but little change of I, so that it’sunwise to plot the figure about I and g(I). Instead, the interest will be bound by [0,1] by common sense. So I set the left end point of Bisection method to 0, right end point to 1. The precision is set to 1e-10.Code for g(I) is :Code for applying Bisection method is :2.The since the density of the ball is smaller than water, so depth submerged shouldnot be larger than 2r, and not less than 0. So I set the left end point of Bisection method to 0 and right to 2r.Code for g(d) is :Code for applying Bisection method is :3.(a)Code for function g:Code for applying fixed point iteration:Code for plot the graph:(b)Code of this problem is similar to the previous one, so it’s not illustrated here.【实验结果】1.Result for the program is :So the annual interest rate is 31.78710938%2.X-axis is the depth and Y-axis is for g(d). Blue line is the function value versusdepth. Red line is y=0. It can be seen from the graph that the solution is about 19.And by using Bisection method, the solution depth=19.306641cm is found, with8 significance bit.3.(a) No fixed point can be found. By solving this equation, we can find that x=2 isone of the solution. If we set the initial point to 2.00000000001 or 1.9999999, the result is the same:. If we set initial point to 2, the point can be found:. This is consistent with the conclusion discussed in the previous part.(b)The fixed point p=0.768169156736 is found:实验题目二Solution of Linear Systems--Gaussian elimination and pivoting姓名:xxxxxx学号:xxxxxxxxxxxx时间:2015年5月28日【题目】Find the sixth-degree polynomial y = a1 + a2x + a3x^2 + a4x^3 + a5x^4 + a6x^5 + a7x^6 that passes through (0, 1), (1, 3), (2, 2), (3, 1), (4, 3), (5, 2), and (6, 1). Use the plot command to plot the polynomial and the given points on the same graph. Explain any discrepancies in your graph.【实验分析】To find the coefficient of each term, we can substitute all the given point to the equations and construct an argument matrix AX=B. Then perform triangular factorization and solve the upper-triangular and lower triangular matrix by back substitution method.Since det(A) = 2.4883e+07 ≠ 0,there exist a unique solution for AX=B.【实验过程和程序】Code for construct coefficient matrix A:Code for construct matrix B:Solve X by triangular factorization and back substitution method:【实验结果】The result for coefficient matrix:The graph of the polynomial is shown below. Blue lines denotes the graph for the polynomial and red points are the sample points. This polynomial fits the points well.Iterat ion of Linear System’s solution--Seidel iteration姓名:xxxxxx学号:xxxxxxxxxxxx时间:2015年5月28日【题目】(a)Start with P0 = 0 and use Jacobi iteration to find Pk for k = 1, 2, 3. Will Jacobi iteration converge to the solution?(b) Start with P0 = 0 and use Gauss-Seidel iteration to find Pk for k = 1, 2, 3. Will Gauss-Seidel iteration converge to the solution?【实验分析】In this problem we only need to construct the coefficient matrix and matrix B and then apply the Jacobi Iteration method and Gauss-Seidel Iteration method to find the solution. SinceA satisfy strictly diagonally dominant, so the iteration steps will converge.【实验过程和程序】【实验结果】Points in first three iteration is shown below. Both this two method will converge.Interpolation--Spline interpolation姓名:xxxxxx学号:xxxxxxxxxxxx时间:2015年5月28日【题目】The measured temperatures during a 5-hour period in a suburb of Los Angeles on November 8 are given in the following table.(a)To construct a Lagrange interpolatory polynomial for the data in the table.(b)To estimate the average temperature during the given5-hour period.(c)Graph the data in the table and the polynomial frompart (a) on the same coordinate system. Discuss thepossible error that can result from using thepolynomial in part (a) to estimate the averagetemperature.【实验分析】(a)X=[1,2,3,4,5,6] Y=[66,66,65,64,63,63], the target is to construct a Lagrangeinterpolatory polynomial for X and Y. We can apply the Lagrange approximation method to solve it.(b)After we get the interpolatory polynomial, we can use a great amount of samplepoints and estimate the function value of them and take the mean.(c)Error contain in Lagrange interpolatory polynomial of 5-degree is:, where Error on the average temperature equals to the mean of , which is:Error ====240.06【实验过程和程序】(a)Construct X and Y, and solve it by Lagrange approximation:(b)After we get the polynomial function, we calculate function value on a set ofsample points and get their mean as the average temperature.(c)plot X_new and Y_new in part(b) is equivalent to plot P N(x). And error bound canonly be obtain by analyzing since f(x) is unknown.【实验结果】(a) We can get the result:Which means that:(b)By taking the mean, the average temperature is 64.5℉:(c)The graph for polynomial and the sample points is shown below. Error bound forthe average temperature is 240.06.实验题目五Curve Fitting--Least mean square error姓名:xxxxxx学号:xxxxxxxxxxxx时间:2015年5月28日【题目】1.The temperature cycle in a suburb of Los Angeles on November 8 is given in theaccompanying table. There are 24 data points.(a)Follow the procedure outlined in Example5.5 (use the fmins command) to find theleast-squares curve of the form f (x) = Acos(Bx)+C sin(Dx)+ E for the given set ofdata.(b)Determine E2( f ).(c)Plot the data and the least-squares curvefrom part (a) on the same coordinatesystem.【实验分析】(a)In this problem. The hour 1~midnight p.m. can be map to 1~12, 1~noon a.m. canbe map to 13~24. So hour and temperature can be used to fit the curve. To get the curve with minimum error, which is define as:The best A, B, C, D, E can be found when:(b)is defined as:In our case,【实验过程和程序】(a) The function of the error is:Using command, A B C D E can be solved. (b) By the function solved by part(a), we can calculate for each sample pointand then calculate :(c) Code to plot the line:【实验结果】(a)The result for parameter A B C D E is:So the least-squares curve is :(b)Error for f(x) is:(c)Graph for least-squares curve and sample points is:Notice that the result of function fminesarch is sensitive about the input seeds.Firstly the seed is [1 1 1 1 1], and get the result [0.1055 -2.1100 0.9490 -4.869861.0712]. In second step, seed is [0.1 2 1 -5 61] and result is [0.0932 2.11141.175 -5.127 61.0424]. By 4 iterations, I find a good fit of the data and get the seed[1 0.1 1 1 60].Numerical Integration--Automatically select the integration step of the trapezoidal method姓名:xxxxxx学号:xxxxxxxxxxxx时间:2015年5月28日【题目】(i) Approximate each integral using the compositetrapezoidal rule with M = 10.(ii) Approximate each integral using the compositeSimpson rule with M = 5.【实验分析】The formula of each function is given in this problem, so we just need to use composite trapezoidal rule and composite Simpson rule to solve them.【实验过程和程序】Function of the six equations:Code for calculation:【实验结果】Solution of Differential Equations--Euler’s Method and Heun’s Method姓名:xxxxxx学号:xxxxxxxxxxxx时间:2015年5月28日【题目】(Supplement) A skydiver jumps from a plane, and up to the moment he opens theparachute the air resistance is proportional to (v represents velocity). Assume that the time interval is [0,6] and hat the differential equation for the downward direction isover [0,6] with v(0)=0Use Euler’s method with h=0.05 and estimate v(6).【实验分析】The function of , where left end point is 0 and right is 6, with initial point v(0)=0. So we can apply Euler’s method directory to solve the problem.【实验过程和程序】Function of :Code of applying Euler’s method:【实验结果】….,so v(6)=10.0726.。