首页 > 其他分享 >(十)计算机数值方法之Gauss_Seidel迭代法

(十)计算机数值方法之Gauss_Seidel迭代法

时间:2024-05-27 12:01:14浏览次数:15  
标签:xk int double Gauss ++ 迭代法 x0 Seidel size

数学问题:

用Gauss_Seidel迭代法求解方程组:

初始迭代向量均设为零向量,二范数误差小于1e-4。

解决代码:

#include<iostream>  
#include<math.h>  
#include<iomanip>
using namespace std;
#define size 10
void Gauss_Seidel(double A[size][size], double B[size], int n, double x0[size]);

int main()
{
    double A[size][size]; double B[size]; double x0[size];
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> A[i][j];
        }
    }
    for (int i = 0; i < n; i++) {
        cin >> B[i];
    }
    for (int i = 0; i < n; i++) {
        x0[i] = 0;
    }
    Gauss_Seidel(A, B, n, x0);
}

void  Gauss_Seidel(double A[size][size], double B[size], int n, double x0[size])
{
    double xk[size];
    for (int i = 0; i < n; i++) {
        xk[i] = 0;
    }
    double eps = 1e-7;
    while (true)
    {
        for (int i = 0; i < n; i++)
        {
            double leijia = 0.0;
            for (int j = 0; j < n; j++)
            {
                if (j < i)
                {
                    leijia = leijia + A[i][j] * xk[j];
                }
                if (j > i)
                {
                    leijia = leijia + A[i][j] * x0[j];
                }

            }
            xk[i] = (1.0 / A[i][i]) * (B[i] - leijia);

        }
        double Fanshu_max = 0.0;
        for (int i = 0; i < n; i++)
        {
            if (fabs(xk[i] - x0[i]) > Fanshu_max)
            {
                Fanshu_max = fabs(xk[i] - x0[i]);
            }
        }
        if (Fanshu_max < eps)
        {
            break;
        }


        for (int i = 0; i < n; i++)
        {
            x0[i] = xk[i];
        }

    }

    

    for (int i = 0; i < n; i++)   
    {
        cout << fixed << setprecision(2) << xk[i] << " ";

    }
}

使用方法:

第一行输入方程组阶度:m

第二行至第m+1行输入二维数组A,即线性方程组的系数矩阵(非奇异)

第m+2行输入矩阵B,即常数项列向量

测试输入:

3

8 -3 2

4 11 -1

2 1 4

20 33 12

预期输出:

3.00 2.00 1.00

问题解决:

计算结果为:

标签:xk,int,double,Gauss,++,迭代法,x0,Seidel,size
From: https://blog.csdn.net/szt12345__/article/details/139204615

相关文章

  • 意外发现openGauss兼容Oracle的几个条件表达式
    意外发现openGauss兼容Oracle的几个条件表达式最近工作中发现openGauss在兼容oracle模式下,可以兼容常用的两个表达式,因此就随手测试了一下。查看数据库版本[omm@openGauss~]$gsql-rgsql((openGauss6.0.0-RC1builded7f8e37)compiledat2024-03-3111:59:31comm......
  • 在Docker中安装GaussDB的ODBC驱动并添加Python测试代码
    #使用官方的Ubuntu20.04镜像作为基础镜像FROMubuntu:20.04#设置环境变量以避免交互式安装提示ENVDEBIAN_FRONTEND=noninteractive#更新包列表并安装必要的软件包RUNapt-getupdate&&\apt-getinstall-y\wget\curl\gnupg2\apt-transport-https......
  • 二叉树 | 迭代法 102.二叉树的层序遍历 429. N 叉树的层序遍历 226.翻转二叉树
    leetcode102.二叉树的层序遍历题目102.二叉树的层序遍历给你二叉树的根节点root,返回其节点值的层序遍历。(即逐层地,从左到右访问所有节点)。解题思路实现代码classTreeNode:def__init__(self,val=0,left=None,right=None):self.val=valse......
  • openGauss lo_truncate
    lo_truncate功能描述将一个大对象截断成一个给定长度。原型intlo_truncate(PGconn*conn,intfd,size_tlen);参数表1lo_truncate参数关键字参数说明conn一个数据库连接fd文件描述符len要截断的长度返回值int:成功时返回0,失败时返回值为-1......
  • openGauss lo_write
    lo_write功能描述向一个大对象写入数据。原型intlo_write(PGconn*conn,intfd,constchar*buf,size_tlen);参数表1lo_write参数关键字参数说明conn一个数据库连接fd文件描述符buf要写入的数据,长度必须等于lenlen要写入的长度返回......
  • openGauss lo_unlink
    lo_unlink功能描述从数据库中移除一个大对象。原型intlo_unlink(PGconn*conn,OidlobjId);参数表1lo_unlink参数关键字参数说明conn一个数据库连接lobjId要移除的大对象的OID返回值int:成功时返回1,失败时返回-1。示例请参见示例章节。详情查......
  • openGauss ODBC接口参考
    ODBC接口参考ODBC接口是一套提供给用户的API函数,本节将对部分常用接口做具体描述,若涉及其他接口可参考msdn中ODBCProgrammer'sReference项的相关内容。SQLAllocEnvSQLAllocConnectSQLAllocHandleSQLAllocStmtSQLBindColSQLBindParameterSQLColAttribute......
  • openGauss ODBC包及依赖的库和头文件
    ODBC包及依赖的库和头文件Linux下的ODBC包从发布包中获取,包名为openGauss-x.x.x-ODBC.tar.gz。Linux环境下,开发应用程序要用到unixODBC提供的头文件(sql.h、sqlext.h等)和库libodbc.so。这些头文件和库可从unixODBC-2.3.0的安装包中获得。详情查看:https://opengauss.org详情查看......
  • openGauss JDBC常用参数参考
    JDBC常用参数参考targetServerType原理:值为master时会依次尝试连接串中配置的ip,直到能够连接到集群中的主机,值为slave时会依次尝试连接串中配置的ip,直到能够连接到集群中的备机(查询语句为:selectlocal_role,db_statefrompg_stat_get_stream_replications();)。建议:有写操......
  • openGauss JDBC包-驱动类和环境类
    JDBC包、驱动类和环境类JDBC包openGauss提供两种JDBCjar包:postgresql.jar和openGauss-jdbc-x.x.x.jar,两种jar包功能一致,仅仅是为了解决和PostgreSQL之间的JDBC驱动包名冲突。在Linux服务器端源代码目录下执行build.sh,获得驱动jar包postgresql.jar和opengauss-jdbc-x.x.x.jar,包......