Assignment-4
Solved by Md Ashik Ali (AE-123-082)
Answer to the question no. 1
Code: A4Q1.f90
Program Current
Implicit none
integer::i
real::t,h,m(0:100),n(0:100),I1,I2,I3,I4,f,fd,exact
t=5
Open(unit=41,file='CurrentOutput.txt')
exact=fd(t)
write(41,*)"The Exact Value of Current is:",exact
write(41,'(/)')
2 format(t37,"Approximation Value",6x,"Error")
write(41,2)
h=-.25
do i=0,2
m(i)=f(t+i*h)
n(i)=f(t-i*h)
end do
I1=(m(1)-n(1))/(2*h)
Write(41,*)"Using Three Point Midpoint Formula:",I1,abs(I1-exact)
h=.25
do i=0,2
m(i)=f(t+i*h)
n(i)=f(t-i*h)
end do
I2=(-3*m(0)+4*m(1)-m(2))/(2*h)
Write(41,*)"Using Three Point Endpoint Formula:",I2,abs(I2-exact)
h=.5
do i=1,2
m(i)=f(t+i*h)
n(i)=f(t-i*h)
end do
I3=(n(2)-8*n(1)+8*m(1)-m(2))/(12*h)
Write(41,*)"Using Five Point Midpoint Formula:",I3,abs(I3-exact)
h=.5
do i=0,4
m(i)=f(t+i*h)
n(i)=f(t-i*h)
end do
I4=(-25*m(0)+48*m(1)-36*m(2)+16*m(3)-3*m(4))/(12*h)
Write(41,*)"Using Five Point Endpoint Formula:",I4,abs(I4-exact)
end program
function f(t)
implicit none
real::f,t
f=60-60*exp(-t/5)
end function
function fd(t)
implicit none
real::fd,t
fd=12*exp(-t/5)
end function
Output: CurrentOutput.txt
The Exact Value of Current is: 4.41455364
Approximation Value Error
Using Three Point Midpoint Formula: 4.41639709 1.84345245E-03
Using Three Point Endpoint Formula: 4.41099548 3.55815887E-03
Using Five Point Midpoint Formula: 4.41454172 1.19209290E-05
Using Five Point Endpoint Formula: 4.41447449 7.91549683E-05
Answer to the question no. 2
Code: A4Q2.f90
program richardson
implicit none
integer::i,j
real::n(5),h=0.11,d(5,5),x,x0=2.,f,fd,exact
Open(unit=5,file='RichardOutPut.txt')
exact=fd(x0)
Write(5,*)"The Exact Value is: ",exact
do i=0,4
n(i+1)=h/2**i
end do
do i=1,5
d(i,1)=(f(x0+n(i))-f(x0-n(i)))/(2*n(i))
end do
do j=2,5
do i=j,5
d(i,j)=(4**(j-1)*d(i,j-1)-d(i-1,j-1))/(4**(j-1)-1)
end do
end do
write(5,'(/)')
Write(5,'(t5,"O(h**2)",10x,"O(h**4)",10x,"O(h**6)",10x,"O(h**8)",10x,"O(h**10)")')
10 format(90('_'))
write(5,10)
Do i=1,5
Write(5,*)(d(i,j),j=1,i)
End Do
end program
function f(x)
real::x
f=x*exp(x)
end function
function fd(x)
real:: fd,x
fd=x*exp(x)+exp(x)
end function
Output: RichardOutPut.txt
The Exact Value is: 22.1671677
O(h**2) O(h**4) O(h**6) O(h**8) O(h**10)
________________________________________________________________________________________
__
22.2417221
22.1858120 22.1671753
22.1718006 22.1671295 22.1671257
22.1683502 22.1672001 22.1672039 22.1672058
22.1675529 22.1672878 22.1672935 22.1672935 22.1672955
Answer to the question no. 3
Code: A4Q3.f90
program motion
implicit none
real::t(7),x(7),v1,v2,a1,a2
integer::i
open(10,file='3input.txt')
open(12,file='3output.txt')
do i=1,7
read(10,*) t(i),x(i)
end do
! when t=0.0 s
v1=(x(2)-x(1))/(t(2)-t(1))
a1=(x(3)-2*x(2)+x(1))/((t(3)-t(2))*(t(2)-t(1)))
! when t=0.6 s
v2=(x(7)-x(6))/(t(7)-t(6))
a2=(x(7)-2*x(6)+x(5))/((t(7)-t(6))*(t(6)-t(5)))
write(12,'(t17,"velocity",8x,"acceleration")')
write(12,'(50("_"))')
write(12,*) "when t=0.0s:",v1,a1
write(12,*) "when t=0.6s:",v2,a2
close(10)
close(12)
end program motion
Input: 3input.txt
0.0 30.13
0.1 31.62
0.2 32.87
0.3 33.64
0.4 33.95
0.5 33.81
0.6 33.24
Output: 3output.txt
velocity acceleration
__________________________________________________
when t=0.0s: 14.9000168 -24.0003586
when t=0.6s: -5.69999552 -43.0000229
Answer to the question no. 4
Code: A4Q4.f90
Program integral
implicit none
integer::i,n=30
real:: a=-0.4,b=0.6,f,fd,exact,h,I1,I2,I3,I4,s,m(0:100)
h=(b-a)/n
exact=fd(b)-fd(a)
open(unit=4,file='integralOutput.txt')
write(4,*)"The exact Value is: ",exact
Write(4,'(t40,"Aproximation Value",3x,"Abs Error",6x,"Relative Error")')
!Using Trapezoidal Rule
s=0
do i=1,n-1
s=s+f(a+i*h)
end do
I1=(h/2)*(f(a)+2*s+f(b))
write(4,*)"Using Trapezoidal Rule the value is:
",I1,Abs(exact-I1),Abs(exact-I1)/exact
!Using Simpson's 1/3 Rule
s=0
do i=1,n-1
if(mod(i,2)==0)then
s=s+2*f(a+i*h)
else
s=s+4*f(a+i*h)
end if
end do
I2=(h/3)*(f(a)+s+f(b))
write(4,*)"Using Simpson's 1/3 Rule the value is:
",I2,Abs(exact-I2),Abs(exact-I2)/exact
!Using Simpson's 3/8 Rule
s=0
do i=1,n-1
if(mod(i,3)==0)then
s=s+2*f(a+i*h)
else
s=s+3*f(a+i*h)
end if
end do
I3=(3*h/8)*(f(a)+s+f(b))
write(4,*)"Using Simpson's 3/8 Rule the value is:
",I3,Abs(exact-I3),Abs(exact-I3)/exact
!Using Weddles Rule
do i=1,n-1
m(i)=f(a+i*h)
end do
I4=(3*h/10)*(f(a)+5*m(1)+m(2)+6*m(3)+m(4)+5*m(5)+f(b))
write(4,'(a,3f14.10)')"Using Weddles Formula the value is:
",I4,Abs(exact-I4),Abs(exact-I4)/exact
end program
function f(x)
implicit none
real::f,x
f=-1/((x-1)*(2+sqrt(2.)-2*x))
end function
function fd(x)
implicit none
real::fd,x
fd=-(1/sqrt(2.))*log(abs((x-1)/(sqrt(2.)-2*(x-1))))
end function
Output: integralOutput.txt
The exact Value is: 0.430767536
Aproximation Value Abs Error Relative
Error
Using Trapezoidal Rule the value is: 0.431104422 3.36885452E-04
7.82058574E-04
Using Simpson's 1/3 Rule the value is: 0.430768639 1.10268593E-06
2.55981672E-06
Using Simpson's 3/8 Rule the value is: 0.430769980 2.44379044E-06
5.67310735E-06
Using Weddles Formula the value is: 0.0475881808 0.3831793666 0.8895270228
Answer to the question no. 5
Code: A4Q5.f90
program romberg_integration
implicit none
integer, parameter :: nmax = 10
integer :: i, j, k, n
real :: a, b, h, s, tol,f
real :: R(nmax, nmax)
open(unit=8,file='RombergOutput.txt')
a = 0.0
b = 1.0
tol = 1.0e-5
R(1,1) = 0.5 * (b -a)*(f(a)+f(b))
do i = 2,nmax
n = 2**(i-1)
h = (b - a) /n
s=0.0
do k = 1, n/2
s= s+f(a +(2*k-1)*h)
end do
R(i,1) = 0.5 * R(i-1,1) + h * s
do j = 2, i
R(i,j) = R(i,j-1) + (R(i,j-1) - R(i-1,j-1)) / (4.0**(j-1) - 1.0)
end do
write(8,*)( R(i,j),j = 1, i)
if (abs(R(i,i) - R(i-1,i-1)) < tol) then
write(8,*)"Romberg integration result:",R(i,i)
write(8,*)"Estimated error:",abs(R(i,i) - R(i-1,i-1))
stop
end if
end do
write(8,*)"Did not converge after",nmax, "iterations."
end program
real function f(x)
real:: x
f = 1.0 / (1.0 + x*x)
end function
Output: RombergOutPut.txt
0.774999976 0.783333302
0.782794118 0.785392165 0.785529435
0.784747124 0.785398126 0.785398543 0.785396457
0.785235405 0.785398185 0.785398185 0.785398185 0.785398185
Romberg integration result: 0.785398185
Estimated error: 1.72853470E-06