当前位置:网站首页>[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;
}
边栏推荐
- What is eplato cast by Plato farm on elephant swap? Why is there a high premium?
- 除了「加机器」,其实你的微服务还能这样优化
- 递归/回溯刷题(上)
- 软件测试面试题:什么是回归测试?
- Oppo core making plan officially announced: the first chip or oppo M1
- 声扬科技正式上线闻声远程声纹健康回访服务系统!
- 对象在内存中存在形式&内存分配机制
- Simple use of enum
- The US Department of justice added 16 new charges against Huawei, including stealing trade secrets
- 一口气学完 Redis 集群方案
猜你喜欢

Station B collapsed. If we were the developer responsible for the repair that night

Microsoft store can't download apps, vs2019 can't download plug-ins solution

Search, insert and delete of hash table

How long will it take to learn the four redis cluster solutions? I'll finish it for you in one breath

零钱通项目(两个版本)含思路详解

Log4j 漏洞仍普遍存在?

Talk about MySQL transaction two-phase commit

Exception -exception

Finish learning redis cluster solution at one go

Station B collapsed. What did the developer responsible for the repair do that night?
随机推荐
Openai issued a document to introduce the latest application of Dall · E 2: fully enter the field of artistic creation and design
LinkedList underlying source code
Internal class (detailed explanation of four internal classes)
Up to 7.5gbps! The world's first 5nm 5g baseband snapdragon X60 release: support the aggregation of all major bands!
Qmodbus library is used, and it is written as ROS node publishing topic and program cmakelist
Software testing interview question: what aspects should be considered when designing test cases, that is, which aspects should different test cases be tested for?
Mobilevit learning notes
Station B collapsed. If we were the developer responsible for the repair that night
Mask automatic update description file (mask description file)
深入理解递归的方法调用(含实例迷宫问题、汉诺塔、猴子吃桃、斐波拉契、阶乘))
软件测试面试题:单元测试、集成测试、系统测试的侧重点是什么?
Search, insert and delete of hash table
DAY_ 4. Operation -- judge whether there is a certain data in the array -- realize array mapping (enlarge by 10 times) -- insert the array in sequence (modify bugs) -- realize array de duplication
How to realize a good knowledge management system?
疫情之下,手机供应链及线下渠道受阻!销量骤降库存严重!
@Can component be used on the same class as @bean?
美司法部增加针对华为的指控,包括窃取商业秘密等16项新罪名
软件测试面试题:通过画因果图来写测试用例的步骤为___、___、___、___及把因果图转换为状态图共五个步骤。 利用因果图生成测试用例的基本步骤是?
软件测试面试题:设计测试用例时应该考虑哪些方面,即不同的测试用例针对那些方面进行测试?
Software test interview question: does software acceptance test include formal acceptance test, alpha test and beta test?