Commit 09cf7b9d authored by EdwinMindcraft's avatar EdwinMindcraft
Browse files

Initial tests for the gauss seidel algorithm



The implementation works in both float and double-precision
Signed-off-by: default avatarEdwinMindcraft <edwin.mindcraft@gmail.com>
parents
.idea/**
cmake-build-debug/**
\ No newline at end of file
cmake_minimum_required(VERSION 3.14)
project(GaussSeidel Fortran)
enable_language(Fortran)
add_executable(GaussSeidel main.f gauss_seidel.f)
\ No newline at end of file
Gauss-Seidel
------------
This is a Fortran implementation of the Gauss-Seidel algorithm to solve linear systems.
\ No newline at end of file
module gauss_seidel
implicit none
private
public :: solve_system
interface solve_system
procedure solve_system_double, solve_system_real
endinterface
contains
subroutine solve_system_double(A, b, x)
implicit none
integer :: An(2), n(1), i, j
double precision :: A(:,:), b(:), x(:), tmp
n = shape(b)
do i=1,n(1)
tmp = 0
do j=1,n(1)
if (j /= i) then
tmp = tmp + A(i,j) * x(j)
endif
enddo
x(i) = (b(i) - tmp) / A(i, i)
enddo
end subroutine solve_system_double
subroutine solve_system_real(A, b, x)
implicit none
integer :: n(1), i, j
real :: A(:,:), b(:), x(:), tmp
n = shape(b)
do i=1,n(1)
tmp = 0
do j=1,n(1)
if (j /= i) then
tmp = tmp + A(i,j) * x(j)
endif
enddo
x(i) = (b(i) - tmp) / A(i, i)
enddo
end subroutine solve_system_real
end module
\ No newline at end of file
program main
use gauss_seidel
implicit none
integer i
real :: A(2,2), b(2), x(2)
A = reshape((/1, 0, 1, 2/), shape(A))
b = (/13, 11/)
x = (/1, 2/)
do i=1,3000000
call solve_system(A, b, x)
if (norm2(matmul(A, x) - b) < 0.0001) then
exit
endif
enddo
write(*,*) x, matmul(A, x)
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment