《研究生数值分析课后题上机编程》由会员分享,可在线阅读,更多相关《研究生数值分析课后题上机编程(14页珍藏版)》请在金锄头文库上搜索。
1、数值分析上机 电工2009020954 马凯 2009级研究生数值分析上机作业院系 电气工程学院专业 控制理论与控制工程姓名 马凯学号 2009020954指导教师 代新敏2009年12月29日第一题(二问):超松弛法求方程组根1.解题理论依据或方法应用条件:超松弛算法是在GS方法已求出x(m),x(m-1)的基础上,经过重新组合得到新序列。如能恰当选择松弛因子,收敛速度会比较快。当1时,称为超松弛法,可以用来加速收敛。其具体算法为: 2.计算程序(使用软件:VC):#include#define w 1.4main()float a1010= 0,0,0,0,0,0,0,0,0,0,0,12
2、.38412,2.115237,-1.061074,1.112336,-0.113584,0.718719,1.742382,3.067813,-2.031743,0,2.115237,19.141823,-3.125432,-1.012345,2.189736,1.563849,-0.784165,1.112348,3.123124,0,-1.061074,-3.125432,15.567914,3.123848,2.031454,1.836742,-1.056781,0.336993,-1.010103,0,1.112336,-1.012345,3.123848,27.108437,4.1
3、01011,-3.741856,2.101023,-0.71828,-0.037585,0,-0.113584,2.189736,2.031454,4.101011,19.897918,0.431637,-3.111223,2.121314,1.784317,0,0.718719,1.563849,1.836742,-3.741856,0.431637,9.789365,-0.103458,-1.103456,0.238417,0,1.742382,-0.784165,-1.056781,2.101023,-3.111223,-0.103458,14.7138465,3.123789,-2.2
4、13474,0,3.067813,1.112348,0.336993,-0.71828,2.121314,-1.103456,3.123789,30.719334,4.446782,0,-2.031743,3.123124,-1.010103,-0.037585,1.784317,0.238417,-2.213474,4.446782,40.00001; float b101=0,2.1874369,33.992318,-25.173417,0.84671695,1.784317,-86.612343,1.1101230,4.719345,-5.6784392; float x1010=0,0
5、,0,0,0,0,0,0,0,0; /*由x(0)=0得到其第一列全为零*/ float sum1=0,sum2=0; int i,m,j; for(m=1;m=9;m+) for(i=1;i=9;i+) sum1=0; for(j=1;j=(i-1);j+)sum1+=(-aij/aii)*xjm; /*计算第一个累加和*/ sum2=0; for(j=(i+1);j=9;j+)sum2+=(-aij/aii)*xjm-1; /*计算第二个累加和*/ xim=(1-w)*xim-1+w*(sum1+sum2+bi0/aii); /*用SOR方法计算*/ printf(x1为:%lfn,x19
6、); printf(x2为:%lfn,x29); printf(x3为:%lfn,x39); printf(x4为:%lfn,x49); printf(x5为:%lfn,x59); printf(x6为:%lfn,x69); printf(x7为:%lfn,x79); printf(x8为:%lfn,x89); printf(x9为:%lfn,x99);3.计算结果4.问题讨论(误差分析、上机出现情况等)这道题目是所有题目中编写最顺利的,一次即顺利得出结果,当然这道题目还是有应该注意到地方,一是注意两个求和的清零,二是注意下标,不要弄混行标和列标。第一题(三问):列主元素法求方程组根1.解题理
7、论依据或方法应用条件: 所谓列主元消去法是,对矩阵作恰当的调整,选取绝对值最大的元素作为主元素。然后把矩阵化为上三角阵,再进行回代,求出方程的解。算法为: , , , , q10=0 , u10=0 , x9=u192.计算程序(使用软件:VC):#include#includemain()double a1011= 0,0,0,0,0,0,0,0,0,0,0,0,12.38412,2.115237,-1.061074,1.112336,-0.113584,0.718719,1.742382,3.067813,-2.031743,2.1874369,0,2.115237,19.141823,-
8、3.125432,-1.012345,2.189736,1.563849,-0.784165,1.112348,3.123124,33.992318,0,-1.061074,-3.125432,15.567914,3.123848,2.031454,1.836742,-1.056781,0.336993,-1.010103,-25.173417,0,1.112336,-1.012345,3.123848,27.108437,4.101011,-3.741856,2.101023,-0.71828,-0.037585,0.84671695,0,-0.113584,2.189736,2.03145
9、4,4.101011,19.897918,0.431637,-3.111223,2.121314,1.784317,1.784317,0,0.718719,1.563849,1.836742,-3.741856,0.431637,9.789365,-0.103458,-1.103456,0.238417,-86.612343,0,1.742382,-0.784165,-1.056781,2.101023,-3.111223,-0.103458,14.7138465,3.123789,-2.213474,1.1101230,0,3.067813,1.112348,0.336993,-0.7182
10、8,2.121314,-1.103456,3.123789,30.719334,4.446782,4.719345,0,-2.031743,3.123124,-1.010103,-0.037585,1.784317,0.238417,-2.213474,4.446782,40.00001,-5.6784392; double x10,y11; double sum=0,max; int i,j,j1,m,n,k,g,h; for(j=1;j=8;j+)max=ajj; for(k=j+1;kfabs(max)max=akj; for(k=j;k=9;k+) if(akj/max=1)g=k;
11、/*确定本列绝对值最大元素所在行:g行*/ for(h=j;h=10;h+) yh=ajh; for(h=j;h=10;h+) ajh=agh; for(h=j;h=10;h+) agh=yh; /*将g行与J行所有元素进行交换*/ for(i=j+1;i=9;i+) for(j1=j+1;j1=1;m-) sum=0; for(n=m+1;n=9;n+) sum+=xn*amn; printf(x%d为:%lfn,m,xm=(am10-sum)/amm); /*从x9到x1逐个解出结果*/3.计算结果4.问题讨论(误差分析、上机出现情况等)(1)这道题目出现的最大问题在 for(j1=j+1
12、;j1=10;j1+)aij1-=(aij/ajj)*ajj1; ,刚开始我错写为:for(j1=j;j1=10;j1+)aij1-=(aij/ajj)*ajj1; ,也就是j1应该从j+1开始取值,如果从j开始取,会导致aij1-=(aij/ajj)*ajj1; 一句中aij是零,这样后面的aij1全部都将是原来的数值,不会变化,导致错误。(2)对于列主元素消元法,个人感觉将a,b两个矩阵,放在一个矩阵中进行编程,较方便。第三题:三次样条插值求近似值1.解题理论依据或方法应用条件:依据三弯矩插值法列出相应的方程,然后将x的值带入(在求解三弯矩方程的参数时,应用追赶法求解方程组求出相应参数值)。 ,由,得出:2.计算程序(使用软件:VC):#include#includemain() int i; double s,z; /*定义函数近似值,导数近似值*/ double b11=