C++ is a great programming language to calculate engineering problems, it is one of the best for those operations. Delphi is also a super-fast compiled programming language that you can use in engineering problems. In this post we will explain how to write a C++ app to solve millions of unknowns in equations by using the Successive Over Relaxation Iteration Method. Haven’t you always wanted to hear that it’s possible to do some hard and difficult work merely by over-relaxing? ðŸ˜‰

In mathematics there are Implicit Equations and we Engineers are mostly faced with solving numerical equations about real life problems. Thermodynamics, fluid mechanics, mechanism theories, structure Analysis, and many other engineering areas requires us to solve many unknown equations. Sometimes calculations like these should be in real time or should be simulating time fractions very precisely. Generally, these implicit equations are exposed on a 1D, 2D or 3D grid examples (i.e compressed on uncompressed fluid flow, thermal distributions) and these grids has nodes with parameters (i.e., Velocity, Pressure, Temperature etc.)

In mathematics, anÂ **implicit equation**Â is a relation of the formÂ *R*(*x*_{1},â€¦,Â *x _{n}*) = 0, whereÂ

*R*Â is aÂ functionÂ of several variables. In example, the implicit equation of the unit circle is isÂ

*x*

^{2}Â +Â

*y*

^{2}Â âˆ’ 1 = 0. More complex equations can be given as equations about to solve first and second law of thermodynamics, Momentum Equations, Naiver-Stokes Equations etc.

AnÂ **Implicit Function**Â is a function that is defined by an implicit equation, that relates one of the variables, considered as the valueof the function, with the others considered as the arguments. These Implicit Equations can be solved by using iteration methods.

In this post we will learn to solve a 7-diagonal matrix by using successive over relaxation (SOR) iteration method In C++.

## Solving 7 Diagonal Matrix

One of the most popular method is SOR Successive Over Relaxation Iteration Method (SOR). This method can be used to solve problems on 1D, 2D and 3D problems. In Numerical Linear Algebra, the **Successive Over-RelaxationÂ (SOR) Method** is a variant of the Â Gaussâ€“Seidel methodÂ for solving a linear system equations, resulting in faster convergence. A similar method can be used for any slowly converging iterative processes.

If the problem is on a 3D grid (for example heat transfer on in a cube is 3D problem) this will produce 7 diagonal matrix forms. Because each 3D grid nodes on this cube connected to with left, right, front, back, up and down nodes (or they are called East, West, North, South, Up and Down) except the faces, corners and the edges. That means there will be maximum 7 variables in each lines of the 2D matrix.

Simply equations in a matrix form can be written as below.

In this equation;

M is 2D matrix (2 bars shows that it is 2D matrix),

U is 1D matrix form of unknown parameters from the each node,

q is 1D matrix form of right side of the equation.

These A matrixes can be shown as below here,

Let’s starting coding with defining our matrix forms. This matrix can be written as below,

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <iomanip> #define max(__a,__b) (((__a) > (__b)) ? (__a) : (__b)) #define min(__a,__b) (((__a) < (__b)) ? (__a) : (__b)) #define WIDTH 4 #define HEIGHT 4 #define DEPTH 4 #define NN 64 // Number of Nodes struct matrix_line { float A,B,D,E,F,H,I; // values in a line of 7 diagonal matrix form to solve 1D, 2D and 3D problems short int a,b,d,f,h,i; // X positions (index) of A,B,D and F,H,I avlues, E is same as line number so we dont need e }; struct matrix_line mx[NN+1]; double m[NN+1][NN+1], u[NN+1], x[NN+1], sol[NN+1]; |

We can print this matrix with formatting as below,

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
void display_7diag_matrix() { for(int i=0; i<NN; i++) { std::cout << std::fixed << std::setw(8) << std::setfill(' ') << std::setprecision(2) <<"|" <<mx[i].A << ", " << mx[i].B << ", " << mx[i].D << ", " << mx[i].E << ", " << mx[i].F << ", " << mx[i].H << ", " << mx[i].I << "| |" << u[i] << "| |" << x[i] << "| |" << sol[i] << "|\n"; } } |

Linear equations can be solved by The Successive Over Relaxation (SOR) Method. Here we developed a SOR Revised Method (SORR) in a C++ app as below,

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
int calc_matrix_SORR7(int max_iteration) { unsigned int iteration = 0; const double omega = 0.5; const double min_error = 0.0001; double err; double psol; do { err=0; for(int i=0; i<NN; i++) { psol=sol[i]; sol[i]=(1.00-omega)*sol[i] + omega*(x[i] -mx[i].A*sol[i+mx[i].a]-mx[i].B*sol[i+mx[i].b]-mx[i].D*sol[i+mx[i].d] -mx[i].F*sol[i+mx[i].f]-mx[i].H*sol[i+mx[i].h]-mx[i].I*sol[i+mx[i].i] )/mx[i].E; err=max(err, std::fabs( (sol[i]-psol) )); } iteration++; }while(err>min_error && iteration<=max_iteration); return iteration; } |

For example, we can calculate unknowns with maximum 1000 iterations by using this function as below,

1 |
void calc_matrix_SORR7(1000); |

C++ is a great programming language to solve these kinds of equations.

**This is no time to relax! Download a free trial of RAD Studio today and try out SOR in your own C++ App**.