【数值分析】Jacobi、Seidel和Sor迭代法求解线性方程组(附matlab代码)

简介: 【数值分析】Jacobi、Seidel和Sor迭代法求解线性方程组(附matlab代码)

线性方程组迭代解法公式类似非线性方程求根的简单迭代法公式, 有Jacobi迭代法、 Seidel迭代法及Sor法等。

题目1-Jacobi迭代法和Seidel迭代法

来源:《数值分析》第5版,清华大学出版社,作者:李庆扬等,第6章习题1

代码1-Jocabi

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 简介:用Jocabi迭代法求解线性方程组,要求误差小于10^(-4)
% 作者:不雨_亦潇潇
% 文件:Jocabi.m
% 日期:20221102
% 博客:https://blog.csdn.net/weixin_43470383/article/details/127661516
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc; clear all;
x_10 = 0;
x_20 = 0;
x_30 = 0;
n = 0;
err = 1;
while err>10^(-4)
    x_11 = -0.4*x_20-0.2*x_30-2.4;
    x_21 = 0.25*x_10 -0.5*x_30+5;
    x_31 = -0.2*x_10+0.3*x_20+0.3;
    n = n+1;
    err1 = abs(x_11-x_10);
    err2 = abs(x_21-x_20);
    err3 = abs(x_31-x_30);
    err = max(err1, max(err2, err3));
    fprintf("n = %d,  ", n)
    fprintf("x_1 = %f,  x_2 = %f,  x_3 = %f,  ", x_11, x_21, x_31)
    fprintf("err = %f\n", err)
    x_10 = x_11;
    x_20 = x_21;
    x_30 = x_31;
end

运行结果1-Jacobi

n = 1, x_1 = -2.400000, x_2 = 5.000000, x_3 = 0.300000, err = 5.000000

n = 2, x_1 = -4.460000, x_2 = 4.250000, x_3 = 2.280000, err = 2.060000

n = 3, x_1 = -4.556000, x_2 = 2.745000, x_3 = 2.467000, err = 1.505000

n = 4, x_1 = -3.991400, x_2 = 2.627500, x_3 = 2.034700, err = 0.564600

n = 5, x_1 = -3.857940, x_2 = 2.984800, x_3 = 1.886530, err = 0.357300

n = 6, x_1 = -3.971226, x_2 = 3.092250, x_3 = 1.967028, err = 0.113286

n = 7, x_1 = -4.030306, x_2 = 3.023680, x_3 = 2.021920, err = 0.068570

n = 8, x_1 = -4.013856, x_2 = 2.981464, x_3 = 2.013165, err = 0.042216

n = 9, x_1 = -3.995218, x_2 = 2.989954, x_3 = 1.997210, err = 0.018637

n = 10, x_1 = -3.995423, x_2 = 3.002590, x_3 = 1.996030, err = 0.012637

n = 11, x_1 = -4.000242, x_2 = 3.003129, x_3 = 1.999862, err = 0.004819

n = 12, x_1 = -4.001224, x_2 = 3.000009, x_3 = 2.000987, err = 0.003121

n = 13, x_1 = -4.000201, x_2 = 2.999200, x_3 = 2.000247, err = 0.001023

n = 14, x_1 = -3.999730, x_2 = 2.999826, x_3 = 1.999800, err = 0.000626

n = 15, x_1 = -3.999890, x_2 = 3.000167, x_3 = 1.999894, err = 0.000341

n = 16, x_1 = -4.000046, x_2 = 3.000081, x_3 = 2.000028, err = 0.000155

n = 17, x_1 = -4.000038, x_2 = 2.999974, x_3 = 2.000033, err = 0.000106

n = 18, x_1 = -3.999996, x_2 = 2.999974, x_3 = 2.000000, err = 0.000041

经过18次迭代,误差满足条件

最终得到近似解:(-3.999996, 2.999974, 2.000000)

代码2-Seidel

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 简介:用Seidel迭代法求解线性方程组,要求误差小于10^(-4)
% 作者:不雨_亦潇潇
% 文件:Seidel.m
% 日期:20221102
% 博客:https://blog.csdn.net/weixin_43470383/article/details/127661516
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc; clear all;
x_10 = 0;
x_20 = 0;
x_30 = 0;
n = 0;
err = 1;
while err>10^(-4)
    x_11 = -0.4*x_20-0.2*x_30-2.4;
    x_21 = 0.25*x_11 -0.5*x_30+5;
    x_31 = -0.2*x_11+0.3*x_21+0.3;
    n = n+1;
    err1 = abs(x_11-x_10);
    err2 = abs(x_21-x_20);
    err3 = abs(x_31-x_30);
    err = max(err1, max(err2, err3));
    fprintf("n = %d,  ", n)
    fprintf("x_1 = %f,  x_2 = %f,  x_3 = %f,  ", x_11, x_21, x_31)
    fprintf("err = %f\n", err)
    x_10 = x_11;
    x_20 = x_21;
    x_30 = x_31;
end

运行结果2-Seidel

n = 1, x_1 = -2.400000, x_2 = 4.400000, x_3 = 2.100000, err = 4.400000

n = 2, x_1 = -4.580000, x_2 = 2.805000, x_3 = 2.057500, err = 2.180000

n = 3, x_1 = -3.933500, x_2 = 2.987875, x_3 = 1.983063, err = 0.646500

n = 4, x_1 = -3.991763, x_2 = 3.010528, x_3 = 2.001511, err = 0.058263

n = 5, x_1 = -4.004513, x_2 = 2.998116, x_3 = 2.000338, err = 0.012751

n = 6, x_1 = -3.999314, x_2 = 3.000003, x_3 = 1.999864, err = 0.005199

n = 7, x_1 = -3.999974, x_2 = 3.000075, x_3 = 2.000017, err = 0.000660

n = 8, x_1 = -4.000033, x_2 = 2.999983, x_3 = 2.000002, err = 0.000092

经过8次迭代,误差满足条件

最终得到近似解:(-4.000033, 2.999983, 2.000002)

题目2-Sor迭代法

来源:《数值分析》第5版,清华大学出版社,作者:李庆扬等,第6章习题8

代码3-Sor

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 简介:用Sor迭代法求解线性方程组,要求误差小于10^(-4)
% 作者:不雨_亦潇潇
% 文件:Sor.m
% 日期:20221102
% 博客:https://blog.csdn.net/weixin_43470383/article/details/127661516
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc; clear all;
x_10 = 0;
x_20 = 0;
x_30 = 0;
n = 0;
err = 1;
omega = 0.9;
while err>10^(-4)
    x_11 = (1-omega)*x_10+omega*(-2.4-0.4*x_20-0.2*x_30);
    x_21 = (1-omega)*x_20+omega*(5+0.25*x_11-0.5*x_30);
    x_31 = (1-omega)*x_30+omega*(0.3-0.2*x_11+0.3*x_21);
    n = n+1;
    err1 = abs(x_11-x_10);
    err2 = abs(x_21-x_20);
    err3 = abs(x_31-x_30);
    err = max(err1, max(err2, err3));
    fprintf("n = %d,  ", n)
    fprintf("x_1 = %f,  x_2 = %f,  x_3 = %f,  ", x_11, x_21, x_31)
    fprintf("err = %f\n", err)
    x_10 = x_11;
    x_20 = x_21;
    x_30 = x_31;
end

运行结果3-Sor

n = 1, x_1 = -2.160000, x_2 = 4.014000, x_3 = 1.742580, err = 4.014000

n = 2, x_1 = -4.134704, x_2 = 3.186931, x_3 = 2.048976, err = 1.974704

n = 3, x_1 = -4.089581, x_2 = 2.976498, x_3 = 2.014677, err = 0.210432

n = 4, x_1 = -4.003139, x_2 = 2.990339, x_3 = 1.999424, err = 0.086442

n = 5, x_1 = -3.996732, x_2 = 3.000028, x_3 = 1.999362, err = 0.009689

n = 6, x_1 = -3.999569, x_2 = 3.000387, x_3 = 1.999963, err = 0.002836

n = 7, x_1 = -4.000090, x_2 = 3.000035, x_3 = 2.000022, err = 0.000521

n = 8, x_1 = -4.000026, x_2 = 2.999988, x_3 = 2.000004, err = 0.000064

经过8次迭代,误差满足条件

最终得到近似解:(-4.000026, 2.999988, 2.000004)

相关文章
|
3月前
|
安全
【2023高教社杯】D题 圈养湖羊的空间利用率 问题分析、数学模型及MATLAB代码
本文介绍了2023年高教社杯数学建模竞赛D题的圈养湖羊空间利用率问题,包括问题分析、数学模型建立和MATLAB代码实现,旨在优化养殖场的生产计划和空间利用效率。
191 6
【2023高教社杯】D题 圈养湖羊的空间利用率 问题分析、数学模型及MATLAB代码
|
2月前
|
资源调度 算法
基于迭代扩展卡尔曼滤波算法的倒立摆控制系统matlab仿真
本课题研究基于迭代扩展卡尔曼滤波算法的倒立摆控制系统,并对比UKF、EKF、迭代UKF和迭代EKF的控制效果。倒立摆作为典型的非线性系统,适用于评估不同滤波方法的性能。UKF采用无迹变换逼近非线性函数,避免了EKF中的截断误差;EKF则通过泰勒级数展开近似非线性函数;迭代EKF和迭代UKF通过多次迭代提高状态估计精度。系统使用MATLAB 2022a进行仿真和分析,结果显示UKF和迭代UKF在非线性强的系统中表现更佳,但计算复杂度较高;EKF和迭代EKF则更适合维数较高或计算受限的场景。
|
3月前
|
存储 算法 搜索推荐
【2022年华为杯数学建模】B题 方形件组批优化问题 方案及MATLAB代码实现
本文提供了2022年华为杯数学建模竞赛B题的详细方案和MATLAB代码实现,包括方形件组批优化问题和排样优化问题,以及相关数学模型的建立和求解方法。
122 3
【2022年华为杯数学建模】B题 方形件组批优化问题 方案及MATLAB代码实现
|
3月前
|
数据采集 存储 移动开发
【2023五一杯数学建模】 B题 快递需求分析问题 建模方案及MATLAB实现代码
本文介绍了2023年五一杯数学建模竞赛B题的解题方法,详细阐述了如何通过数学建模和MATLAB编程来分析快递需求、预测运输数量、优化运输成本,并估计固定和非固定需求,提供了完整的建模方案和代码实现。
88 0
【2023五一杯数学建模】 B题 快递需求分析问题 建模方案及MATLAB实现代码
|
6月前
|
数据安全/隐私保护
地震波功率谱密度函数、功率谱密度曲线,反应谱转功率谱,matlab代码
地震波格式转换、时程转换、峰值调整、规范反应谱、计算反应谱、计算持时、生成人工波、时频域转换、数据滤波、基线校正、Arias截波、傅里叶变换、耐震时程曲线、脉冲波合成与提取、三联反应谱、地震动参数、延性反应谱、地震波缩尺、功率谱密度
|
6月前
|
数据安全/隐私保护
耐震时程曲线,matlab代码,自定义反应谱与地震波,优化源代码,地震波耐震时程曲线
地震波格式转换、时程转换、峰值调整、规范反应谱、计算反应谱、计算持时、生成人工波、时频域转换、数据滤波、基线校正、Arias截波、傅里叶变换、耐震时程曲线、脉冲波合成与提取、三联反应谱、地震动参数、延性反应谱、地震波缩尺、功率谱密度
基于混合整数规划的微网储能电池容量规划(matlab代码)
基于混合整数规划的微网储能电池容量规划(matlab代码)
|
6月前
|
算法 调度
含多微网租赁共享储能的配电网博弈优化调度(含matlab代码)
含多微网租赁共享储能的配电网博弈优化调度(含matlab代码)
|
6月前
|
Serverless
基于Logistic函数的负荷需求响应(matlab代码)
基于Logistic函数的负荷需求响应(matlab代码)
|
6月前
|
供应链 算法
基于分布式优化的多产消者非合作博弈能量共享(Matlab代码)
基于分布式优化的多产消者非合作博弈能量共享(Matlab代码)
下一篇
无影云桌面