Fortran Program
Fortran Program
==============================================================
! Solution for a quadratic equation ax^2 + bx + c = 0
! Fortran 90 Demo program for students
! method: analytic solutions
! written by: Alex Godunov (February 2007)
!----------------------------------------------------------------
!input:
! a, b, c - coefficiants of equation (supplied by the user)
!output:
! x1, x2 - roots (can be complex)
!================================================================*/
program quad
implicit none
write (*,100)
read (*,*) a, b, c
f(0) = 0
f(1) = 1
do i=2,40
f(i) = f(i-1) + f(i-2)
! check for prime numbers
prime = 'prime'
do j=2,f(i)-1
if (f(i) == (f(i)/j)*j) then
prime = ' '
exit
end if
end do
write (*,102) i, f(i), prime
end do
stop
end
program Legendre
!=======================================
! Fortran 90 demo program for students
! calculates Legendre polynomials Pn(x)
! using the recurrence relation
! written by: Alex Godunov (2008)
!=======================================
implicit none
double precision x, xmin, xmax, dx
double precision pl, plg
integer n
n = 10 ! order of Pn(x)
x = xmin
!open (unit=7,file="Legendre.dat")
write(*,100) n
do while (x < xmax+0.01)
plg = pl(x,n)
write(*,101) x, plg
x = x + dx
end do
stop
end
function pl(x,n)
!======================================
! calculates Legendre polynomials Pn(x)
! using the recurrence relation
! if n > 100 the function retuns 0.0
!======================================
double precision pl
double precision x
double precision pln(0:n)
integer n, k
pln(0) = 1.0
pln(1) = x
if (n <= 1) then
pl = pln(n)
else
do k=1,n-1
pln(k+1) = ((2.0*k+1.0)*x*pln(k) - float(k)*pln(k-1))/(float(k+1))
end do
pl = pln(n)
end if
return
end
program main
!====================================================================
! A single root of an equation f(x)=0 in [x1,x2] interval
! Method: Bisectional
!====================================================================
implicit none
double precision f,x1,x2,root,eps
integer flag
external f
x1 = -10.0
x2 = 10.0
eps = 1.0e-6
write (*,100)
call bisection(f,x1,x2,eps,root,flag)
! print solutions
if(flag == 0) then
write(*,104)
stop
end if
! test solution
write(*,105) f(Root)
Subroutine bisection(f,x1,x2,eps,Root,flag)
!============================================================
! Solutions of equation f(x)=0 on [x1,x2] interval
! Method: Bisectional (closed domain) (a single root)
! Alex G. January 2010
!------------------------------------------------------------
! input ...
! f - function - evaluates f(x) for any x in [x1,x2]
! x1 - left endpoint of initial interval
! x2 - right endpoint of initial interval
! eps - desired uncertainity of the root as |b-a|<eps
! output ...
! Root - root of the equation f(x)=0
! flag - indicator of success
! >0 - a single root found, flag=number of iterations
! 0 - no solutions for the bisectional method
! <0 - not a root but singularity, flag=number of iterations
!
! Comments: Function f(x) has to change sign between x1 and x2
! Max number of iterations - 200 (accuracy (b-a)/2**200)
!====================================================================
implicit none
double precision f, x1, x2, eps, Root
double precision a, b, c
integer i, flag
integer, parameter:: iter=200
!* initialize calculations
a=x1
b=x2
!-----------------------------------------------------
Function f(x)
implicit none
double precision f, x
f = x + cos(x)
end function f
program main
!====================================================================
! A single root of an equation f(x)=0 in [x1,x2] interval
! Method: Closed Domain (Bisectional or False position)
!====================================================================
implicit none
double precision f,x1,x2,root,eps
integer key, flag
external f
key = 2
x1 = -10.0
x2 = 10.0
eps = 1.0e-6
call CDomain(f,x1,x2,eps,root,key,flag)
! print solutions
if(flag == 0) then
write(*,*)' no roots for closed domain methods'
stop
end if
! test solution
write(*,104) f(Root)
Subroutine CDomain(f,x1,x2,eps,Root,key,flag)
!============================================================
! Solutions of equation f(x)=0 on [x1,x2] interval
! Close Domain Methods: bisectional or false position
! Alex G. January 2010
!------------------------------------------------------------
! input ...
! f - function - evaluates f(x) for any x in [x1,x2]
! x1 - left endpoint of initial interval
! x2 - right endpoint of initial interval
! eps - desired uncertainity of the root as |b-a|<eps
! key - select a method
! 1 - bisectional method
! 2 - false position method
! output ...
! Root - root of the equation f(x)=0
! flag - indicator of success
! >0 - a single root found, flag=number of iterations
! 0 - no solutions for the bisectional method
! <0 - not a root but singularity, flag=number of iterations
!
! Comments: Function f(x) has to change sign between x1 and x2
! Max number of iterations - 200 (accuracy (b-a)/2**200)
!====================================================================
implicit none
double precision f, x1, x2, eps, Root
double precision a, b, c
integer i, key, flag
integer, parameter:: iter=200
!* initialize calculations
a=x1
b=x2
!-----------------------------------------------------
Function f(x)
implicit none
double precision f, x
f = x + cos(x)
end function f
program main
!====================================================================
! A single root of an equation f(x)=0 in around x1
! Method: Open Domain (Newton)
!====================================================================
implicit none
double precision f,x1,root,eps
integer flag
external f
x1 = -1.0
eps = 1.0e-7
call Newton1(f,x1,eps,Root,flag)
! print solutions
if(flag == 0) then
write(*,*)' no root found'
stop
end if
! test solution
write(*,104) f(Root,0)
Subroutine Newton1(f,x1,eps,Root,flag)
!============================================================
! Solutions of equation f(x)=0 around point x
! Methods: Newton
! Alex G. January 2010
!------------------------------------------------------------
! input ...
! f - function - evaluates f(x)
! fp - finction - evaluates f'(x)
! x1 - a guess point
! eps - desired uncertainity of the root as f(Root)<eps
! output ...
! Root - root of the equation f(Root)=0
! flag - indicator of success
! >0 - a single root found, flag=number of iterations
! 0 - no solutions for the method of secants
!
! Comments: a user should suply both f(x) and f'(x)
!====================================================================
implicit none
double precision f, x1, eps, Root
double precision x2
integer i, flag
integer, parameter:: iter=200
Function f(x,k)
!-----------------------------------------------------
! evaluates f(x) for k=0
! evaluates f'(x) for k=1
!-----------------------------------------------------
implicit none
double precision f, x
integer k
if(k==0) f = x + cos(x)
if(k==1) f = 1 - sin(x)
end function f