Assignment_2_Merged_With_Questions
Assignment_2_Merged_With_Questions
program bisection
implicit none
integer:: i=1
real::a=1,b=2,p0,p,f
real,parameter::tol=1e-8
open(unit=21,file='bisectout.txt')
p0=a
p=(a+b)/2.
if (f(a)*f(p)>0)then
a=p
end if
if (f(a)*f(p)<0)then
b=p
end if
p0=p
p=a+b/2.
i=i+1
end do
end
function f(x)
implicit none
real::x,f
f=x**3-x-1
end function
Output: bisectout.txt
Iteration No a b x f(x)
Relative Error
________________________________________________________________________________________
________________________________
1 1.000000 2.000000 1.500000 0.875000
0.333333
2 1.000000 1.500000 1.750000 2.609375
0.142857
3 1.000000 1.750000 1.875000 3.716797
0.066667
4 1.000000 1.875000 1.937500 4.335693
0.032258
5 1.000000 1.937500 1.968750 4.662079
0.015873
6 1.000000 1.968750 1.984375 4.829586
0.007874
7 1.000000 1.984375 1.992188 4.914428
0.003922
8 1.000000 1.992188 1.996094 4.957123
0.001957
9 1.000000 1.996094 1.998047 4.978539
0.000978
10 1.000000 1.998047 1.999023 4.989264
0.000489
11 1.000000 1.999023 1.999512 4.994630
0.000244
12 1.000000 1.999512 1.999756 4.997315
0.000122
13 1.000000 1.999756 1.999878 4.998657
0.000061
14 1.000000 1.999878 1.999939 4.999329
0.000031
15 1.000000 1.999939 1.999969 4.999664
0.000015
16 1.000000 1.999969 1.999985 4.999832
0.000008
17 1.000000 1.999985 1.999992 4.999916
0.000004
18 1.000000 1.999992 1.999996 4.999958
0.000002
19 1.000000 1.999996 1.999998 4.999979
0.000001
20 1.000000 1.999998 1.999999 4.999990
0.000000
21 1.000000 1.999999 2.000000 4.999995
0.000000
22 1.000000 2.000000 2.000000 4.999998
0.000000
23 1.000000 2.000000 2.000000 4.999999
0.000000
24 1.000000 2.000000 2.000000 5.000000
0.000000
25 1.000000 2.000000 2.000000 5.000000
0.000000
program fixed_point
implicit none
integer::i=1
real:: x,p(3)=[0.5,1.0,1.5],x1,f,g1,g1d,j,g2d,g2
real,parameter::tol=1e-6
open(unit=2,file='fixed_point_output.dat')
do j=1,3
x=p(j)
if(abs(g1d(x))<tol)then
write(2,8)
write(2,'(100("_"))')
do while(.true.)
x1=g1(x)
write(2,9)i,x,x1,g1(x1),abs(x1-x)
if(abs(x1-x)<tol)then
exit
end if
x=x1
i=i+1
end do
else
write(2,*)"Error"
end if
end do
write(2,'(///)')
do j=1,3
x=p(j)
if(abs(g2d(x))<tol)then
write(2,8)
write(2,'(100("_"))')
do while(.true.)
x1=g2(x)
write(2,9)i,x,x1,g1(x1),abs(x1-x)
if(abs(x1-x)<tol)then
exit
end if
x=x1
i=i+1
end do
else
write(2,*)"Error"
end if
end do
end
function g1(x)
implicit none
real::x,g1
g1=sqrt(x+10)/x
end function
function g1d(x)
implicit none
real::x,g1d
g1d=1/(2*x*sqrt(x+10))-sqrt(x+10)/x**2
end function
function g2(x)
implicit none
real::x,g2
g2=(x+10)**(1/4)
end function
function g2d(x)
implicit none
real::x,g2d
g2d=(1/4)*(x+10)**(-3/4)
end function
Output: fixed_point_output.dat
Error
Error
Error
Iteration No Pn-1 Pn f(Pn) Abs error
________________________________________________________________________________________
____________
1 0.500000 1.000000 3.316625 0.500000
2 1.000000 1.000000 3.316625 0.000000
Iteration No Pn-1 Pn f(Pn) Abs error
________________________________________________________________________________________
____________
2 1.000000 1.000000 3.316625 0.000000
Iteration No Pn-1 Pn f(Pn) Abs error
________________________________________________________________________________________
____________
2 1.500000 1.000000 3.316625 0.500000
3 1.000000 1.000000 3.316625 0.000000
program newton
implicit none
integer::i=1
real::x0=5,x,f,fd
real,parameter::tol=1e-5
open(unit=23,file='newtonoutput.txt')
7 format(3x, "Iteration
No",7x,"Pn-1",10x,"f'(Pn-1)",12x,"Pn",12x,"f(Pn)",12x,"Relative Error")
write(23,7)
write(23,'(100("_"))')
8 format(i9,t17,5f16.8)
do while(.true.)
x=x0-f(x0)/fd(x0)
write(23,8)i,x0,fd(x0),x,f(x),abs(x-x0)/x
if(abs(x-x0)<tol)then
exit
end if
x0=x
i=i+1
end do
end program
function f(y)
real::y,f
f=5*(y**2)+ cos(3*y)-2*exp(y)-exp(-y)
end function
function fd(y)
real::x,fd
fd=10*y-3*sin(3*y)-2*exp(y)+exp(-y)
end function
Output: newtonoutput.txt
program secant
implicit none
integer::i=1
real::x=0,x1=2.,x2,f
real,parameter::t=1e-6
open(unit=678,file='secant_out.txt')
do while(.true.)
x2=x1-((f(x1)*(x-x1))/(f(x)-f(x1)))
write(678,3)i,x,x1,x2,abs(x2-x1)
if(abs(x2-x1)<t )then
exit
end if
x=x1
x1=x2
i=i+1
end do
close(678)
end program
function f(y)
real::y
f=4.*y**3-1.-exp((x**2)/2.)
end function
Output: secant_out.txt
program false_position
implicit none
integer::i=1
real::a=1.,b=2.,c,f
real,parameter::t=1e-5
open(unit=789,file='falsi_out.txt')
do while(.true.)
c=b-((b-a)*f(b)/(f(b)-f(a)))
write(789,4)i,a,b,c,f(c),abs(c-b)
if(abs(c-b)<t .or. abs(f(c))<t)then
exit
end if
if(f(a)*f(c)<0)then
b=c
end if
if(f(a)*f(c)>0)then
a=c
end if
i=i+1
end do
close(789)
end program
function f(y)
real::y
f=exp(y)+2.**(-y)+(2.*cos(y))-6.
end function
Output: falsi_out.txt
iteration no a b c f(c)
abs error
________________________________________________________________________________________
____________
1 1.000000 2.000000 1.678308 -0.545674
0.321692
2 1.678308 2.000000 1.808103 -0.085739
0.191897
3 1.808103 2.000000 1.826538 -0.011645
0.173462
4 1.826538 2.000000 1.829006 -0.001549
0.170994
5 1.829006 2.000000 1.829334 -0.000205
0.170666
6 1.829334 2.000000 1.829377 -0.000027
0.170623
7 1.829377 2.000000 1.829383 -0.000003
0.170617