CC++Learn C++

Solve A 3 Diagonal Matrix Using Successive Over Relaxation

Solve A 3 Diagonal Matrix Using Successive Over Relaxation

C++ is a great programming language to calculate engineering problems, it is one of the best for those operations. Delphi is also faster 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 Successive Over Relaxation Iteration Method.

A little about the mathematics behind this C++ app

In mathematics there are Implicit Equations and we Engineers mostly faced on solving numerical equations about the real-life problems. Thermodynamics, Fluid Mechanics, Mechanism Theories, Structure Analysis, and many other engineering areas requires to solve many unknown equations. Sometimes calculations these should be in real time, or it 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(x1,…, xn) = 0, where R is a function of several variables. In example, the implicit equation of the unit circle is is x2 + y2 − 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.

How to solve a 3-diagonal matrix in C++

One of the most popular methods 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 equation, resulting in faster convergence. A similar method can be used for any slowly converging iterative processes.

Implicit Equations on 1D Grids produces Triangular Matrix forms. If the problem is on a 1D grid (for example heat transfer on a wire is 1D problem) this will produce 3 diagonal matrix forms. Because each 1D grid nodes on this wire connected to with left node and right except the first and the last one. That means there will be 3 variables (Left, Mid, Right) in each lines of the 2D matrix affecting to 3 Unknowns that we want to solve.

Simply equations in 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 M, U and q matrixes can be shown as below here,

Here is C++ app example of how to solve a 3-diagonal matrix

If you don’t have any equation or example matrix, we can generate random 3 diagonal equations to test as given below,

Solving a linear equation in a C++ app

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

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

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

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?