当前位置:网站首页>[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;
}
边栏推荐
- Log4j 漏洞仍普遍存在,并持续造成影响
- Shengyang technology officially launched the remote voiceprint health return visit service system!
- B站崩了,如果我们是那晚负责修复的开发人员
- 华为成立全球生态发展部:全力推进HMS全球生态建设
- Commercial delay of self-developed 5g chip? Apple iPhone will adopt Qualcomm 5g chip in the next four years
- Nano semiconductor 65W gallium nitride (GAN) scheme was adopted by Xiaomi 10 Pro charger
- Small change project (two versions) with detailed ideas
- 异常-Exception
- 屏蔽自动更新描述文件(屏蔽描述文件)
- LinkedList underlying source code
猜你喜欢

Talk about MySQL transaction two-phase commit

Encapsulate an array into a class

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

腾讯云[HiFlow】| 自动化 -------HiFlow:还在复制粘贴?

Openai issued a document to introduce the latest application of Dall · E 2: fully enter the field of artistic creation and design

How to realize a good knowledge management system?

@Detailed introduction of requestparam annotation

day 1 - day 4

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

Why do server programs need to listen first
随机推荐
2019q4 memory manufacturers' revenue ranking: Samsung fell 5%, only SK Hynix and micron maintained growth
Understanding of L1 regularization and L2 regularization [easy to understand]
Shengyang technology officially launched the remote voiceprint health return visit service system!
Form of objects in memory & memory allocation mechanism
Exception -exception
Software test interview question: does software acceptance test include formal acceptance test, alpha test and beta test?
For 3nm and below processes, ASML new generation EUV lithography machine exposure
Technology Management - we must focus on the big and let go of the small
软件测试面试题:设计系统测试计划需要参考的项目文档?
Internal class (detailed explanation of four internal classes)
首发展锐5G芯片!纯国产5G手机海信F50曝光:搭载虎贲T710+春藤510
枚举Enum的简单使用
2021-11-05 understanding of class variables and class methods
Finish learning redis cluster solution at one go
Mysql 数据恢复流程 基于binlog redolog undolog
Pythia: Facebook's latest open source visual and language multitasking learning framework
In addition to "adding machines", in fact, your micro service can be optimized like this
8000字讲透OBSA原理与应用实践
Software test interview question: when saving a text file under windows, a save dialog box will pop up. If a test case is established for the file name, how should equivalent classes be divided?
2021-11-05 understand main method syntax, code block and final keyword