Skip to content

Instantly share code, notes, and snippets.

@md-abdul-halim-rafi
Created October 22, 2019 16:25
Show Gist options
  • Save md-abdul-halim-rafi/62daeac4ec69460f296efdb10b1aa851 to your computer and use it in GitHub Desktop.
Save md-abdul-halim-rafi/62daeac4ec69460f296efdb10b1aa851 to your computer and use it in GitHub Desktop.
Lagrange Interpolation using Fortran
1.0 181.0
2.0 155.0
3.0 161.5
4.0 183.0
6.0 214.1
10.0 319.0
program lagrange
implicit none
integer, parameter :: n = 6
integer :: i, j
real :: x(n), y(n), output, xp, p
open(unit = 2, file = 'input.txt')
do i = 1, n
read(2,*)x(i), y(i)
end do
do i = 1, n
write(*,*)x(i), y(i)
end do
output = 0; xp = 7!Approximation point
do i = 1, n
p = 1
do j = 1, n
if(i == j) then
p = p * 1
else
p = p * (xp-x(j)) / (x(i)-x(j))
end if
end do
output = output + p * y(i)
end do
write(*,'(2/, A24, F10.4, 2/)')"Approximate result : ", output
end program
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment