0% found this document useful (0 votes)
3 views8 pages

Assignment_2_Merged_With_Questions

The document contains solutions to five programming assignments related to numerical methods, including bisection, fixed-point iteration, Newton's method, secant method, and false position method. Each section includes the Fortran code, the output generated from the code, and the format of the output files. The assignments demonstrate various approaches to finding roots of functions with specified tolerances.

Uploaded by

ashikalianik2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views8 pages

Assignment_2_Merged_With_Questions

The document contains solutions to five programming assignments related to numerical methods, including bisection, fixed-point iteration, Newton's method, secant method, and false position method. Each section includes the Fortran code, the output generated from the code, and the format of the output files. The assignments demonstrate various approaches to finding roots of functions with specified tolerances.

Uploaded by

ashikalianik2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Assignment-2

Solved by Md Ashik Ali (AE-123-082)

Answer to the question no. 1


Code: A2Q1.f90

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.

9 format(3x,"Iteration No",13x,"a",13x,"b",14x,"x",14x,"f(x)",7x,"Relative Error")


write(21,9)
10 format(120('_'))
write(21,10)
11 format(i9,t17,5F15.6)
do while(.true.)
write(21,11)i,a,b,p,f(p),abs(p-p0)/p

if(abs(p-p0)<tol .or. abs(f(p))<tol)then


exit
end if

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

Answer to the question no. 2


Code: A2Q2.f90

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')

8 format(3x,"Iteration No",6x,"Pn-1",8x,"Pn",10x,"f(Pn)",10x,"Abs error")


9 format(i9,6f16.6)

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

Answer to the question no. 3


Code: A2Q3.f90

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

Iteration No Pn-1 f'(Pn-1) Pn f(Pn)


Relative Error
________________________________________________________________________________________
____________
1 5.00000000 -248.77044678 4.30621672 -54.67616653
0.16111203
2 4.30621672 -106.27825165 3.79175425 -16.43313980
0.13567927
3 3.79175425 -47.94177628 3.44898129 -4.09515142
0.09938383
4 3.44898129 -26.02430916 3.29162264 -0.53661358
0.04780580
5 3.29162264 -19.51435089 3.26412416 -0.01327465
0.00842446
6 3.26412416 -18.55679321 3.26340890 -0.00001160
0.00021917
7 3.26340890 -18.53252411 3.26340818 0.00000363
0.00000022

Answer to the question no. 4


Code: A2Q4.f90

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')

write(678,'(3x,"iteration no",12x,"xn_1",13x,"xn",14x,"xn+1",14x,"abs error")')


write(678,'(90("_"))')
3 format(t6,i3,t20,f15.7,3(2x,f15.7))

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

iteration no xn_1 xn xn+1 abs error


________________________________________________________________________________________
__
1 0.0000000 2.0000000 0.1250000 1.8750000
2 2.0000000 0.1250000 0.2417582 0.1167582
3 0.1250000 0.2417582 4.9005117 4.6587534
4 0.2417582 4.9005117 0.2609944 4.6395173
5 4.9005117 0.2609944 0.2800078 0.0190134
6 0.2609944 0.2800078 2.4568796 2.1768718
7 0.2800078 2.4568796 0.3502818 2.1065979
8 2.4568796 0.3502818 0.4153887 0.0651069
9 0.3502818 0.4153887 1.3872030 0.9718143
10 0.4153887 1.3872030 0.5756238 0.8115792
11 1.3872030 0.5756238 0.6768853 0.1012616
12 0.5756238 0.6768853 0.8379075 0.1610222
13 0.6768853 0.8379075 0.7867994 0.0511081
14 0.8379075 0.7867994 0.7933280 0.0065286
15 0.7867994 0.7933280 0.7937038 0.0003758
16 0.7933280 0.7937038 0.7937005 0.0000033
17 0.7937038 0.7937005 0.7937005 0.0000000

Answer to the question no. 5


Code: A2Q5.f90

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')

write(789,'(3x,"iteration no",15x,"a",15x,"b",15x,"c",15x,"f(c)",12x,"abs error")')


write(789,'(100("_"))')
4 format(t6,i3,t20,f15.6,4(2x,f15.6))

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy