线性方程组的迭代法(Jacobi 迭代法和Gauss-Seidel 迭代法) C++代码

2023-12-01 09:01

本文主要是介绍线性方程组的迭代法(Jacobi 迭代法和Gauss-Seidel 迭代法) C++代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Jacobi 迭代法

#include <iostream>
#include <cmath>
#include <vector>using namespace std;// 定义方程组的系数矩阵和常数向量
vector<vector<double>> A = {{20, 2, 3},{1, 8, 1},{2, -3, 15}};
vector<double> b = {24, 12, 30};// 定义迭代次数和精度阈值
int maxIterations = 100;
double epsilon = 5e-5;
int iterations = 0;
// 雅可比迭代函数
vector<double> jacobiIteration(const vector<vector<double>>& A, const vector<double>& b, const vector<double>& x0) {int n = A.size();vector<double> x(x0);  // 初始解的近似值for (int k = 0; k < maxIterations; k++) {iterations++;vector<double> x_new(n, 0);for (int i = 0; i < n; i++) {double sum = 0;for (int j = 0; j < n; j++) {if (j != i) {sum += A[i][j] * x[j];}}x_new[i] = (b[i] - sum) / A[i][i];}// 判断是否满足终止条件double diff = 0;for (int i = 0; i < n; i++) {diff += abs(x_new[i] - x[i]);}if (diff < epsilon) {break;}// 更新解的近似值x = x_new;}return x;
}int main() {int n = A.size();vector<double> x0(n, 0);  // 初始解的近似值vector<double> x = jacobiIteration(A, b, x0);cout << "Solution: \n";for (int i = 0; i < n; i++) {cout << "x" << i+1 << " = " << x[i] << "\n";}cout << endl;cout << "iterations = " << iterations << '\n';return 0;
}

Gauss-Seidel 迭代法

#include <iostream>
#include <cmath>#define N 3 // 线性方程组的未知数个数
#define MAX_ITERATIONS 100 // 最大迭代次数
#define EPSILON 0.00005 // 迭代停止的精度void gaussSeidel(double coef[N][N], double b[N], double x[N]) {double x_new[N];// 初始化迭代结果for (int i = 0; i < N; i++) {x[i] = 0;}int iterations = 0;double error = EPSILON + 1;while (error > EPSILON && iterations < MAX_ITERATIONS) {for (int i = 0; i < N; i++) {double sum1 = 0;for (int j = 0; j < i; j++) {sum1 += coef[i][j] * x_new[j];}double sum2 = 0;for (int j = i + 1; j < N; j++) {sum2 += coef[i][j] * x[j];}x_new[i] = (b[i] - sum1 - sum2) / coef[i][i];}error = 0;for (int i = 0; i < N; i++) {error += std::abs(x_new[i] - x[i]);x[i] = x_new[i];}iterations++;}if (iterations < MAX_ITERATIONS) {std::cout << "Converged in " << iterations << " iterations." << std::endl;} else {std::cout << "Did not converge within the maximum number of iterations." << std::endl;}
}int main() {double coef[N][N] = {{20, 2, 3}, {1, 8, 1}, {2, -3, 15}};double b[N] = {24, 12, 30};double x[N];gaussSeidel(coef, b, x);std::cout << "Solution:" << std::endl;for (int i = 0; i < N; i++) {std::cout << "x" << i << " = " << x[i] << std::endl;}return 0;
}

Jacobi 迭代法与Gauss-Seidel 迭代法的比较

#include <iostream>
#include <vector>
#include <cmath>#define N 3 // 线性方程组的未知数个数
#define MAX_ITERATIONS 100 // 最大迭代次数
#define EPSILON 0.00005 // 迭代停止的精度// 高斯-赛德尔迭代法
int gaussSeidel(const std::vector<std::vector<double>>& A, const std::vector<double>& b, std::vector<double>& x) {std::vector<double> x_new(N);// 初始化迭代结果for (int i = 0; i < N; i++) {x[i] = 0;}int iterations = 0;double error = EPSILON + 1;while (error > EPSILON && iterations < MAX_ITERATIONS) {for (int i = 0; i < N; i++) {double sum1 = 0;for (int j = 0; j < i; j++) {sum1 += A[i][j] * x_new[j];}double sum2 = 0;for (int j = i + 1; j < N; j++) {sum2 += A[i][j] * x[j];}x_new[i] = (b[i] - sum1 - sum2) / A[i][i];}error = 0;for (int i = 0; i < N; i++) {error += std::abs(x_new[i] - x[i]);x[i] = x_new[i];}iterations++;}return iterations;
}// 雅可比迭代法
int jacobi(const std::vector<std::vector<double>>& A, const std::vector<double>& b, std::vector<double>& x) {std::vector<double> x_new(N);// 初始化迭代结果for (int i = 0; i < N; i++) {x[i] = 0;}int iterations = 0;double error = EPSILON + 1;while (error > EPSILON && iterations < MAX_ITERATIONS) {for (int i = 0; i < N; i++) {double sum = 0;for (int j = 0; j < N; j++) {if (j != i) {sum += A[i][j] * x[j];}}x_new[i] = (b[i] - sum) / A[i][i];}error = 0;for (int i = 0; i < N; i++) {error += std::abs(x_new[i] - x[i]);x[i] = x_new[i];}iterations++;}return iterations;
}int main() {std::vector<std::vector<double>> A = {{20, 2, 3}, {1, 8, 1}, {2, -3, 15}};std::vector<double> b = {24, 12, 30};std::vector<double> x(N);int gaussSeidelIterations = gaussSeidel(A, b, x);int jacobiIterations = jacobi(A, b, x);std::cout << "Gauss-Seidel iterations: " << gaussSeidelIterations << std::endl;std::cout << "Jacobi iterations: " << jacobiIterations << std::endl;return 0;
}

这篇关于线性方程组的迭代法(Jacobi 迭代法和Gauss-Seidel 迭代法) C++代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/440537

相关文章

Django开发时如何避免频繁发送短信验证码(python图文代码)

《Django开发时如何避免频繁发送短信验证码(python图文代码)》Django开发时,为防止频繁发送验证码,后端需用Redis限制请求频率,结合管道技术提升效率,通过生产者消费者模式解耦业务逻辑... 目录避免频繁发送 验证码1. www.chinasem.cn避免频繁发送 验证码逻辑分析2. 避免频繁

精选20个好玩又实用的的Python实战项目(有图文代码)

《精选20个好玩又实用的的Python实战项目(有图文代码)》文章介绍了20个实用Python项目,涵盖游戏开发、工具应用、图像处理、机器学习等,使用Tkinter、PIL、OpenCV、Kivy等库... 目录① 猜字游戏② 闹钟③ 骰子模拟器④ 二维码⑤ 语言检测⑥ 加密和解密⑦ URL缩短⑧ 音乐播放

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

C++11范围for初始化列表auto decltype详解

《C++11范围for初始化列表autodecltype详解》C++11引入auto类型推导、decltype类型推断、统一列表初始化、范围for循环及智能指针,提升代码简洁性、类型安全与资源管理效... 目录C++11新特性1. 自动类型推导auto1.1 基本语法2. decltype3. 列表初始化3

C++11右值引用与Lambda表达式的使用

《C++11右值引用与Lambda表达式的使用》C++11引入右值引用,实现移动语义提升性能,支持资源转移与完美转发;同时引入Lambda表达式,简化匿名函数定义,通过捕获列表和参数列表灵活处理变量... 目录C++11新特性右值引用和移动语义左值 / 右值常见的左值和右值移动语义移动构造函数移动复制运算符

C++中detach的作用、使用场景及注意事项

《C++中detach的作用、使用场景及注意事项》关于C++中的detach,它主要涉及多线程编程中的线程管理,理解detach的作用、使用场景以及注意事项,对于写出高效、安全的多线程程序至关重要,下... 目录一、什么是join()?它的作用是什么?类比一下:二、join()的作用总结三、join()怎么

Python实现MQTT通信的示例代码

《Python实现MQTT通信的示例代码》本文主要介绍了Python实现MQTT通信的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 安装paho-mqtt库‌2. 搭建MQTT代理服务器(Broker)‌‌3. pytho

MySQL进行数据库审计的详细步骤和示例代码

《MySQL进行数据库审计的详细步骤和示例代码》数据库审计通过触发器、内置功能及第三方工具记录和监控数据库活动,确保安全、完整与合规,Java代码实现自动化日志记录,整合分析系统提升监控效率,本文给大... 目录一、数据库审计的基本概念二、使用触发器进行数据库审计1. 创建审计表2. 创建触发器三、Java

C++中全局变量和局部变量的区别

《C++中全局变量和局部变量的区别》本文主要介绍了C++中全局变量和局部变量的区别,全局变量和局部变量在作用域和生命周期上有显著的区别,下面就来介绍一下,感兴趣的可以了解一下... 目录一、全局变量定义生命周期存储位置代码示例输出二、局部变量定义生命周期存储位置代码示例输出三、全局变量和局部变量的区别作用域

C++中assign函数的使用

《C++中assign函数的使用》在C++标准模板库中,std::list等容器都提供了assign成员函数,它比操作符更灵活,支持多种初始化方式,下面就来介绍一下assign的用法,具有一定的参考价... 目录​1.assign的基本功能​​语法​2. 具体用法示例​​​(1) 填充n个相同值​​(2)