资源描述:
《数值分析-迭 代法》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、华北科技学院上机报告系(部)专业、班级姓名学号课程名称数值分析上机题目 实验六,实验七 任课教师指导教师成绩(优、良、中、及格、不及格)华北科技学院基础部实验六解线性方程组的迭代法1.目的与要求:1)熟悉求解线性方程组的有关理论哈方法。2)会编制雅可比迭代和高斯—塞得尔迭代法。3)通过实际计算,进一步了解各算法的优缺点,选择合适的数值方法。2.雅可比迭代法算法设方程组AX=b的系数矩阵的对角元素M为迭代次数容许的最大值,为容许误差.①取初始向量令k=0;②对计算③如果则输出,结束;否则执行④,④如果则不收敛,终止程序;否则转②.1.分别用雅可比迭代法与高斯-塞德尔迭
2、代法解下列方程组:1.用雅可比迭代法计算:#include"stdafx.h"#include"iostream.h"#include"stdio.h"#include"math.h"#include"conio.h"#include"malloc.h"#include#defineEPS1e-8#defineMAX100float*Jacobi(floata[9][10],intn){float*x,*y,s;doubleepsilon;inti,j,k=1;x=(float*)malloc(n*sizeof(float));y=(float*)
3、malloc(n*sizeof(float));for(i=0;iEPS);if(k>=MAX){returny;}for(i=0;i4、ta[9][10]={{31,-13,0,0,0,-10,0,0,0,-15},{-13,35,-9,0,-11,0,0,0,0,27},{0,-9,31,-10,0,0,0,0,0,-23},{0,0,-10,79,-30,0,0,0,-9,0},{0,0,0,-30,57,-7,0,-5,0,-20},{0,0,0,0,7,47,-30,0,0,12},{0,0,0,0,0,-30,41,0,0,-7},{0,0,0,0,-5,0,0,27,-2,7},{0,0,0,0,0,0,0,-2,29,-10}};float*x;x=(float*)malloc(9*si
5、zeof(float));printf("结果为:");x=Jacobi(a,9);for(i=0;i<9;i++)printf("x[%d]=%f",i,x[i]);}程序运行结果如下:结果为:x[0]=-0.200550x[1]=0.368393x[2]=-0.731859x[3]=-0.300318x[4]=-0.446577x[5]=0.399384x[6]=0.121501x[7]=0.151792x[8]=-0.334359Pressanykeytocontinue2.用高斯-塞德尔迭代法:#include"stdafx.h"#include"st
6、dio.h"#include"math.h"#include"conio.h"#include"iostream"#include"malloc.h"#defineN100voidmain(){inti;float*x;floatc[90]={31,-13,0,0,0,-10,0,0,0,-15,-13,35,-9,0,-11,0,0,0,0,27,0,-9,31,-10,0,0,0,0,0,-23,0,0,-10,79,-30,0,0,0,-9,0,0,0,0,-30,57,-7,0,-5,0,-20,0,0,0,0,7,47,-30,0,0,12,0,0,0,0,
7、0,-30,41,0,0,-7,0,0,0,0,-5,0,0,27,-2,7,0,0,0,0,0,0,0,-2,29,-10};float*GauseSeide(float*,int);x=GauseSeide(c,9);for(i=0;i<9;i++)printf("x[%d]=%f",i,x[i]);}float*GauseSeide(float*a,intn){inti,j,nu=0;float*x,dx,d,wucha;x=(float*)malloc(n*sizeof(float));for(i=0;i<=n-1;i++)x[i]=0.