当前位置:网站首页>[numerical analysis exercise] Jacobi iteration method of third-order matrix
[numerical analysis exercise] Jacobi iteration method of third-order matrix
2022-07-27 21:56:00 【Dream and wake up, happy and carefree】
Actually, it's pure c Language , Just encapsulate a few more functions .
//n*n Matrix and n*1 Matrix jacobi Iterative method
#include<cstdio>
#include<cmath>
#define ROW 3
#define COL 3
void input(double A[][COL + 1], double B[][2], double G[][COL + 1], double F[][2])
{
// Read A,B
for (int i = 1; i <= ROW; i++)
for (int j = 1; j <= COL; j++)
scanf("%lf", &A[i][j]);
for (int i = 1; i <= ROW; i++)
scanf("%lf", &B[i][1]);
// Line by line G,F assignment
double a_ii;
for (int i = 1; i <= ROW; i++)
{
//G
a_ii = A[i][i];
if (a_ii == 0) // Wrong situation
{
puts("a_ii==0");
return;
}
for (int j = 1; j <= COL; j++)
{
if (j == i) // Diagonals 0
G[i][j] = 0;
else // Others are normal
G[i][j] = -A[i][j] / a_ii;
}
//F
F[i][1] = B[i][1] / a_ii;
}
}
void printMatrix(double M[][COL + 1])
{
for (int i = 1; i <= ROW; i++)
{
for (int j = 1; j <= COL; j++)
printf("%f\t", M[i][j]);
putchar('\n');
}
}
void printVector(double V[][2])
{
for (int i = 1; i <= ROW; i++)
printf("%f\n", V[i][1]);
}
double getVectorNorm(double V[][2])
{
double max = 0, temp;
for (int i = 1; i <= ROW; i++)
{
temp = fabs(V[i][1]);
if (temp > max)
max = temp;
}
return max;
}
void vectorMinus(double R[][2], double Va[][2], double Vb[][2])
{
for (int i = 1; i <= ROW; i++)
R[i][1] = Va[i][1] - Vb[i][1];
}
void matrixMult(double R[][2], double G[][4], double X[][2])
{
for (int i = 1; i <= ROW; i++)
{
for (int j = 1; j <= 1; j++)
{
double count = 0;
for (int k = 1; k <= COL; k++)
count += G[i][k] * X[k][j];
R[i][j] = count;
}
}
}
void vectorAdd(double R[][2], double GX[][2], double F[][2])
{
for (int i = 1; i <= ROW; i++)
R[i][1] = GX[i][1] + F[i][1];
}
void vectorCopy(double A[][2], double C[][2])
{
for (int i = 1; i <= ROW; i++)
C[i][1] = A[i][1];
}
int main(void)
{
freopen("input.txt", "r", stdin);
double A[ROW + 1][COL + 1], B[ROW + 1][2];
double G[ROW + 1][COL + 1], F[ROW + 1][2];
double pre_X[ROW + 1][2], X[ROW + 1][2],R[ROW + 1][2];// Take from 1 Start remembering
double GX[ROW + 1][2], AX[ROW + 1][2];
input(A, B, G, F);
// iterative process
vectorCopy(F, X);
vectorCopy(F, pre_X);
do {
vectorCopy(X, pre_X);
matrixMult(GX, G, X);
vectorAdd(X, GX, F);
vectorMinus(R, X, pre_X);// Error of error analysis
printVector(X);
putchar('\n');
} while (getVectorNorm(R) > 0.00001);// Norm accuracy
puts("last result X:");
printVector(X);
puts("matching result AX:");
matrixMult(AX, A, X);
printVector(AX);
return 0;
}
边栏推荐
- 一文读懂Plato Farm的ePLATO,以及其高溢价缘由
- Excalidraw: an easy-to-use online, free "hand drawn" virtual whiteboard tool
- 为什么要使用MQ消息中间件?这几个问题必须拿下
- University of Tilburg, Federal University of the Netherlands | neural data to text generation based on small datasets: comparing the added value of two semi supervised learning approvals on top of a l
- Software testing interview question: what project documents need to be referred to in designing the system test plan?
- B站崩了,那晚负责修复的开发人员做了什么?
- QT take out the input box string, lineedit
- Software testing interview question: what is regression testing?
- 技术管理 - 一定要抓大放小
- Custom recycleview delete & move animation
猜你喜欢

Monitor the running of server jar and restart script

B站崩了,如果我们是那晚负责修复的开发人员

为什么服务端程序都需要先 listen 一下

Is log4j vulnerability still widespread?

Read Plato farm's eplato and the reason for its high premium

Log4j vulnerability is still widespread and continues to cause impact

为什么要使用MQ消息中间件?这几个问题必须拿下

Lvs+kept highly available cluster

An article takes you into the world of pycharm - stop asking me about pycharm installation and environment configuration!!!

怎么还有人问 MySQL 是如何归档数据的呢?
随机推荐
Microsoft store can't download apps, vs2019 can't download plug-ins solution
Regular expression exercise
LVS+Keepalived高可用群集
Technology Management - we must focus on the big and let go of the small
最高7.5Gbps!全球首款5nm 5G基带骁龙X60发布:支持聚合全部主要频段!
MySQL data recovery process is based on binlog redolog undo
Monitor the running of server jar and restart script
面向3nm及以下工艺,ASML新一代EUV光刻机曝光
Mysql 数据恢复流程 基于binlog redolog undolog
Software testing interview question: what project documents need to be referred to in designing the system test plan?
高并发遇到死锁了,如何搞?
In crsctl, the function of displayed home
Enumeration and annotation
单核CPU, 1G内存,也能做JVM调优吗?
University of Tilburg, Federal University of the Netherlands | neural data to text generation based on small datasets: comparing the added value of two semi supervised learning approvals on top of a l
V2.x synchronization is abnormal. There are a lot of posts that cannot be synchronized in the cloud, and the synchronization is blocked and slow
LInkedList底层源码
三星最先进EUV产线已投产:今年7nm产能将是去年3倍
8000字讲透OBSA原理与应用实践
How long will it take to learn the four redis cluster solutions? I'll finish it for you in one breath