Fortran Numerical Assignment 11
Fortran Numerical Assignment 11
1). Use (i) Bijection method (ii) Fixed point iteration method and (iii) Newton-Raphson
method to find the root of the equation f(x)=0
(c) 𝒇(𝒙) = 𝒙𝟑 − 𝒙 − 𝟏
(d) 𝒇(𝒙) = 𝒙𝟒 − 𝒙 − 𝟏𝟎 = 𝟎
(i)Bisection Method
Program Code :
program bisect
real a,b,c
Method"
10 read*,a,b
m1=f(a)
m2=f(b)
print*,a,b,m1,m2
if (m1*m2.GT.0) go to 10
print*, ” n c a b"
n=1
q=.1E-4
20 c=(a+b)/2
g=f(c)
if(g.EQ.0)goto 40
if(m1*g.LT.0) then
b=c
ASSIGNMENT-I
else
a=c
end if
if (abs(b-a).LT.q) goto 35
print*,n,c,a,b
n=n+1
goto 20
35 c=(a+b)/2
40 print*,c
end program
function f(x)
f=cos(x)-x
return
end
Intput
Output
1.00000000 2.00000000 0 -2
n c a b
1.99999619
Program Code :
program bisect
real a,b,c
Method"
10 read*,a,b
m1=f(a)
m2=f(b)
print*,a,b,m1,m2
if (m1*m2.GT.0) go to 10
print*," n c a b"
n=1
q=.1E-4
20 c=(a+b)/2
g=f(c)
if(g.EQ.0)goto 40
ASSIGNMENT-I
if(m1*g.LT.0) then
b=c
else
a=c
end if
if (abs(b-a).LT.q) goto 35
print*,n,c,a,b
n=n+1
goto 20
35 c=(a+b)/2
40 print*,c
end program
function f(x)
f=2*cos(x)+exp(x)+2**-x-6
return
end
Intput
Output
1.00000000 2.00000000 -1 0
n c a b
(c) . 𝒇(𝒙) = 𝒙𝟑 − 𝒙 − 𝟏
Program Code :
program bisect
real a,b,c
Method"
10 read*,a,b
m1=f(a)
m2=f(b)
print*,a,b,m1,m2
if (m1*m2.GT.0) go to 10
print*," n c a b"
n=1
q=.1E-4
20 c=(a+b)/2
g=f(c)
if(g.EQ.0)goto 40
ASSIGNMENT-I
if(m1*g.LT.0) then
b=c
else
a=c
end if
if (abs(b-a).LT.q) goto 35
print*,n,c,a,b
n=n+1
goto 20
35 c=(a+b)/2
40 print*,c
end program
function f(x)
f=x*x*x-x-1
return
end
Intput
-1
Output
-1.00000000 2.00000000 -1 5
n c a b
1.32471561
(d). 𝒇(𝒙) = 𝒙𝟒 − 𝒙 − 𝟏𝟎 = 𝟎
Program Code:
program bisect
real a,b,c
Method"
10 read*,a,b
m1=f(a)
m2=f(b)
print*,a,b,m1,m2
if (m1*m2.GT.0) go to 10
print*," n c a b"
ASSIGNMENT-I
n=1
q=.1E-4
20 c=(a+b)/2
g=f(c)
if(g.EQ.0)goto 40
if(m1*g.LT.0) then
b=c
else
a=c
end if
if (abs(b-a).LT.q) goto 35
print*,n,c,a,b
n=n+1
goto 20
35 c=(a+b)/2
40 print*,c
end program
function f(x)
f=x*x*x*x-x-10
return
end
Intput
-2
Output
n c a b
ASSIGNMENT-I
(iii)Newton-Raphson Method
Program Code:
program nr
integer count
count=0
Method.'
ASSIGNMENT-I
write(*,50)
50 format('Method.')
read(*,*)x,e
print*,"Itteraional roots"
10 if(g(x).EQ.0)then
stop
endif
y=x- (f(x)/g(x))
if (abs(f(y)).LT.e)goto 20
count=count+1
print*,count,x
if(count.GT.500)then
endif
x=y
goto 10
20 write(*,30)x
stop
end
function f(x)
f=cos(x)-x
return
end
function g(x)
g=-sin(x)-1
return
ASSIGNMENT-I
end
Intput
0.0001
Output
Itteraional roots
1 1.00000000
Program Code:
program nr
integer count
count=0
Method.'
write(*,50)
50 format('Method.')
read(*,*)x,e
print*,"Itteraional roots"
10 if(g(x).EQ.0)then
stop
ASSIGNMENT-I
endif
y=x- (f(x)/g(x))
if (abs(f(y)).LT.e)goto 20
count=count+1
print*,count,x
if(count.GT.500)then
endif
x=y
goto 10
20 write(*,30)x
stop
end
function f(x)
f=2*cos(x)+exp(x)+2**(-x)-6
return
end
function g(x)
g=-2*sin(x)+exp(x)- (alog2)*2**(-x)
return
end
Intput
Method.
0.0001
ASSIGNMENT-I
Output
Itteraional roots
1 1.00000000
2 1.90201855
3 1.83499610
c). 𝒇(𝒙) = 𝒙𝟑 − 𝒙 − 𝟏
Program Code :
program nr
integer count
count=0
Method.'
write(*,50)
50 format('Method.')
read(*,*)x,e
print*,"Itteraional roots"
10 if(g(x).EQ.0)then
stop
endif
y=x- (f(x)/g(x))
if (abs(f(y)).LT.e)goto 20
ASSIGNMENT-I
count=count+1
print*,count,x
if(count.GT.500)then
endif
x=y
goto 10
20 write(*,30)x
stop
end
function f(x)
f=x*x*x-x-1
return
end
function g(x)
g=3*x**2-1
return
end
Intput
Method.
0.0001
Output
Itteraional roots
1 1.00000000
ASSIGNMENT-I
2 1.50000000
3 1.34782612
(d) 𝒇(𝒙) = 𝒙𝟒 − 𝒙 − 𝟏𝟎 = 𝟎
Program Code :
program nr
integer count
count=0
Method.'
write(*,50)
50 format('Method.')
read(*,*)x,e
print*,"Itteraional roots"
10 if(g(x).EQ.0)then
stop
endif
y=x- (f(x)/g(x))
if (abs(f(y)).LT.e)goto 20
count=count+1
print*,count,x
if(count.GT.500)then
endif
x=y
ASSIGNMENT-I
goto 10
20 write(*,30)x
stop
end
function f(x)
f=x*x*x*x-x-10
return
end
function g(x)
g=4*x**3-1
return
end
Intput
Method.
0.0001
Output
Itteraional roots
1 1.00000000
2 4.33333349
3 3.29083443
4 2.55620646
5 2.09823632
ASSIGNMENT-I
6 1.89560878
Program Code :
program NIF
dimension x(20),y(20,20)
write(*,100)
a \')
write(*,110)
method')
write(*,*)
read(*,*)n
do 10 i=1,n
read(*,*)x(i),y(i,1)
10 continue
to be found :'
read (*,*)a
k=0
ASSIGNMENT-I
do 20 j=2,n
k=k+1
do 30 i=1,n-k
y(i,j)=(y(i+1,j-1)-y(i,j-1))/(x(i+k)-x(i))
30 continue
20 continue
write(*,*)
do 70 i=1,n
write(*,90)x(i)
do 80 j=1,n-i+1
write(*,90)y(i,j)
90 format(1x,F10.3)
80 continue
write(*,*)
write(*,*)
70 continue
s=y(1,1)
do 40 j=2,n
p=1
do 50 i=1,j-1
p=p*(a-x(i))
50 continue
s=s+p*y(1,j)
40 continue
write(*,60)s
stop
end
ASSIGNMENT-I
Input
0.0
0.00
.2
.0347
.4
.1173
.6
.2160
.8
.2987
1.0
.3333
.567
Output
0.000
0.000
0.173
0.599
-0.662
ASSIGNMENT-I
-0.008
0.008
0.200
0.035
0.413
0.201
-0.669
-0.000
0.400
0.117
0.493
-0.200
-0.669
0.600
0.216
0.414
-0.601
0.800
0.299
0.173
1.000
0.333
.0
.00
.2
.0347
.4
.1171
.6
.2160
.8
.2987
1.0
.3333
.798
Output
0.000
0.000
0.173
0.596
-0.650
-0.039
0.060
0.200
0.035
0.412
0.206
-0.681
ASSIGNMENT-I
0.021
0.400
0.117
0.494
-0.202
-0.665
0.600
0.216
0.414
-0.601
0.800
0.299
0.173
1.000
0.333
0.0
0.00
0.2
0.0347
0.4
0.1173
0.6
ASSIGNMENT-I
0.2160
0.8
0.2987
1.0
0.3333
0.95
Output
0.000
0.000
0.173
0.599
-0.662
-0.008
0.008
0.200
0.035
0.413
0.201
-0.669
-0.000
0.400
0.117
0.493
-0.200
-0.669
0.600
ASSIGNMENT-I
0.216
0.414
-0.601
0.800
0.299
0.173
1.000
0.333
Program Code :
program li
dimension x(20),y(20)
write(*,*)
read(*,*)n
values '
do 10 i=1,n
read(*,*)x(i),y(i)
10 continue
write (*,*)'Enter the value of x for which function values is to be found :'
ASSIGNMENT-I
read (*,*)a
s=0
do 20 i=1,n
p=1
do 30 j=1,n
if(i.NE.j) p=p*(a-x(j))/(x(i)-x(j))
30 continue
s=s+p*y(i)
20 continue
write(*,*)
write(*,40)a,s
stop
end
Intput
To find the function value for a particular value of x using Lagrange interpola
tion formula
1.00
0.2420
1.20
0.1942
1.40
0.1497
1.60
ASSIGNMENT-I
0.1109
1.80
0.0790
2.00
0.0540
1.50
Output
The function value at x= 1.50 is : 0.129
4). Use (i) Gaussian Elimination Method and (ii) LU Factorization Method to solve the
following
−𝟑𝒙𝟏 + 𝟓𝒙𝟐 + 𝒙𝟑 = 𝟓
𝟔𝒙𝟏 − 𝟒𝒙𝟐 = 𝟐
Program Code :
PROGRAM gelmn
dimension a(30,30),x(30)
write(*,10)
write(*,20)
read(*,*)n
ASSIGNMENT-I
read(*,*)((a(i,j),j=1,n+1),i=1,n)
DO 30 k=1,n-1
call pivot(a,k,n)
DO 90 i=k+1,n
u=a(i,k)/a(k,k)
DO 100 j=k,n+1
a(i,j)=a(i,j)-u*a(k,j)
100 continue
90 continue
30 continue
IF(abs(a(n,n)).LE.(.00001))THEN
STOP
ENDIF
x(n)=a(n,n+1)/a(n,n)
DO 60 i=n-1,1,-1
sum=0
DO 70 j=i+1,n
sum=sum+a(i,j)*x(j)
70 continue
x(i)=(a(i,n+1)-sum)/a(i,i)
60 continue
DO 80 i=1,n
write(*,110)x(i)
110 format(1x,F10.3)
80 continue
STOP
ASSIGNMENT-I
END
subroutine pivot(a,k,n)
dimension a(30,30)
real mx
integer p,q
mx=abs(a(k,k))
p=k
DO 40 m=k+1,n
IF(abs(a(m,k)).GT.mx)THEN
mx=abs(a(m,k))
p=m
ENDIF
40 continue
IF(mx.LE.(.00001))THEN
write(*,*)'Ill-conditioned equations.'
STOP
ENDIF
DO 50 q=k,n+1
temp=a(k,q)
a(k,q)=a(p,q)
a(p,q)=temp
50 continue
return
END
Intput
3
ASSIGNMENT-I
-7
-2
-7
-3
-4
Output
3.000
4.000
-6.000
Program Code :
real l(20,20)
dimension a(20,20),b(20),u(20,20),x(20),y(20)
write(*,130)
write(*,140)
read(*,*)n
read(*,*)((a(i,j),j=1,n),b(i),i=1,n)
do 10 i=1,n
do 20 j=i+1,n
l(i,j)=0
20 continue
do 30 j=1,i-1
u(i,j)=0
30 continue
u(1,i)=a(1,i)
l(i,i)=1
if (i.GT.1)l(i,1)=a(i,1)/u(1,1)
10 continue
do 40 i=2,n
do 50 j=2,i-1
s=0
do 60 k=1,j-1
s=s+l(i,k)*u(k,j)
60 continue
l(i,j)=(a(i,j)-s)/u(j,j)
50 continue
do 70 j=i,n
s=0
do 80 k=1,i-1
s=s+l(i,k)*u(k,j)
80 continue
u(i,j)=a(i,j)-s
ASSIGNMENT-I
70 continue
40 continue
write(*,*)((l(i,j),i=1,n),j=1,n)
write(*,*)((u(i,j),i=1,n),j=1,n)
y(1)=b(1)
do 90 i=2,n
sum=0
do 100 j=1,n-1
sum=sum+l(i,j)*y(j)
100 continue
y(i)=b(i)-sum
90 continue
write(*,*)(y(i),i=1,n)
x(n)=y(n)/u(n,n)
do 110 i=n-1,1,-1
sum=0
do 120 j=i+1,n
sum=sum+u(i,j)*x(j)
120 continue
x(i)=(y(i)-sum)/u(i,i)
110 continue
do 150 i=1,n
write(*,160)x(i)
160 format(1x,F6.3)
150 continue
stop
end
Input
ASSIGNMENT-I
LU factorisation
-7
-2
-7
-3
-4
Output
3.000
4.000
-6.000