0% found this document useful (0 votes)
136 views32 pages

Fortran Numerical Assignment 11

The document provides code to find the root of equations using the bisection, fixed point iteration, and Newton-Raphson methods. It tests each method on the equations (a) f(x) = Cosx - x, (b) f(x) = 2cosx + ex + 2^-x - 6, (c) f(x) = x^3 - x - 1, and (d) f(x) = x^4 - x - 10. For each equation and method, it gives the input values, output table of iterations, and calculated root to 5 decimal places.

Uploaded by

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

Fortran Numerical Assignment 11

The document provides code to find the root of equations using the bisection, fixed point iteration, and Newton-Raphson methods. It tests each method on the equations (a) f(x) = Cosx - x, (b) f(x) = 2cosx + ex + 2^-x - 6, (c) f(x) = x^3 - x - 1, and (d) f(x) = x^4 - x - 10. For each equation and method, it gives the input values, output table of iterations, and calculated root to 5 decimal places.

Uploaded by

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

ASSIGNMENT-I

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

Correct upto five decimal places,where

(a) 𝒇(𝒙) = 𝑪𝒐𝒔𝒙 − 𝒙

(b) 𝒇(𝒙) = 𝟐𝒄𝒐𝒔𝒙 + 𝒆𝒙 + 𝟐−𝒙 − 𝟔

(c) 𝒇(𝒙) = 𝒙𝟑 − 𝒙 − 𝟏

(d) 𝒇(𝒙) = 𝒙𝟒 − 𝒙 − 𝟏𝟎 = 𝟎

(i)Bisection Method

a). 𝒇(𝒙) = 𝑪𝒐𝒔𝒙 − 𝒙

Program Code :

program bisect

real a,b,c

print*,"To find a real root of an equation using Bisection

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

To find a real root of an equation using Bisection Method

Output

1.00000000 2.00000000 0 -2

n c a b

1 1.50000000 1.50000000 2.00000000

2 1.75000000 1.75000000 2.00000000

3 1.87500000 1.87500000 2.00000000

4 1.93750000 1.93750000 2.00000000

5 1.96875000 1.96875000 2.00000000

6 1.98437500 1.98437500 2.00000000

7 1.99218750 1.99218750 2.00000000

8 1.99609375 1.99609375 2.00000000


ASSIGNMENT-I

9 1.99804688 1.99804688 2.00000000

10 1.99902344 1.99902344 2.00000000

11 1.99951172 1.99951172 2.00000000

12 1.99975586 1.99975586 2.00000000

13 1.99987793 1.99987793 2.00000000

14 1.99993896 1.99993896 2.00000000

15 1.99996948 1.99996948 2.00000000

16 1.99998474 1.99998474 2.00000000

1.99999619

(b). 𝒇(𝒙) = 𝟐𝒄𝒐𝒔𝒙 + 𝒆𝒙 + 𝟐−𝒙 − 𝟔

Program Code :

program bisect

real a,b,c

print*,"To find a real root of an equation using Bisection

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

To find a real root of an equation using Bisection Method

Output

1.00000000 2.00000000 -1 0

n c a b

1 1.50000000 1.50000000 2.00000000

2 1.75000000 1.75000000 2.00000000

3 1.87500000 1.75000000 1.87500000

4 1.81250000 1.81250000 1.87500000

5 1.84375000 1.81250000 1.84375000

6 1.82812500 1.82812500 1.84375000


ASSIGNMENT-I

7 1.83593750 1.82812500 1.83593750

8 1.83203125 1.82812500 1.83203125

9 1.83007813 1.82812500 1.83007813

10 1.82910156 1.82910156 1.83007813

11 1.82958984 1.82910156 1.82958984

12 1.82934570 1.82934570 1.82958984

13 1.82946777 1.82934570 1.82946777

14 1.82940674 1.82934570 1.82940674


15 1.82937622 1.82937622 1.82940674
16 1.82939148 1.82937622 1.82939148
1.82938004

(c) . 𝒇(𝒙) = 𝒙𝟑 − 𝒙 − 𝟏

Program Code :

program bisect

real a,b,c

print*,"To find a real root of an equation using Bisection

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

To find a real root of an equation using Bisection Method

-1

Output

-1.00000000 2.00000000 -1 5

n c a b

1 0.500000000 0.500000000 2.00000000

2 1.25000000 1.25000000 2.00000000

3 1.62500000 1.25000000 1.62500000

4 1.43750000 1.25000000 1.43750000

5 1.34375000 1.25000000 1.34375000


ASSIGNMENT-I

6 1.29687500 1.29687500 1.34375000

7 1.32031250 1.32031250 1.34375000

8 1.33203125 1.32031250 1.33203125

9 1.32617188 1.32031250 1.32617188

10 1.32324219 1.32324219 1.32617188

11 1.32470703 1.32470703 1.32617188

12 1.32543945 1.32470703 1.32543945

13 1.32507324 1.32470703 1.32507324

14 1.32489014 1.32470703 1.32489014

15 1.32479858 1.32470703 1.32479858

16 1.32475281 1.32470703 1.32475281

17 1.32472992 1.32470703 1.32472992

18 1.32471848 1.32470703 1.32471848

1.32471561

(d). 𝒇(𝒙) = 𝒙𝟒 − 𝒙 − 𝟏𝟎 = 𝟎

Program Code:

program bisect

real a,b,c

print*,"To find a real root of an equation using Bisection

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

To find a real root of an equation using Bisection Method

-2

Output

1.00000000 -2.00000000 -10 8

n c a b
ASSIGNMENT-I

1 -0.500000000 -0.500000000 -2.00000000

2 -1.25000000 -1.25000000 -2.00000000

3 -1.62500000 -1.62500000 -2.00000000

4 -1.81250000 -1.62500000 -1.81250000

5 -1.71875000 -1.62500000 -1.71875000

6 -1.67187500 -1.67187500 -1.71875000

7 -1.69531250 -1.69531250 -1.71875000

8 -1.70703125 -1.69531250 -1.70703125

9 -1.70117188 -1.69531250 -1.70117188

10 -1.69824219 -1.69531250 -1.69824219

11 -1.69677734 -1.69677734 -1.69824219

12 -1.69750977 -1.69677734 -1.69750977

13 -1.69714355 -1.69714355 -1.69750977

14 -1.69732666 -1.69732666 -1.69750977

15 -1.69741821 -1.69741821 -1.69750977

16 -1.69746399 -1.69746399 -1.69750977

17 -1.69748688 -1.69746399 -1.69748688

18 -1.69747543 -1.69746399 -1.69747543


-1.69747257

(iii)Newton-Raphson Method

(a) 𝒇(𝒙) = 𝑪𝒐𝒔𝒙 − 𝒙

Program Code:

program nr

integer count

count=0

print*,'To find a root of an equation using Newton Raphsan

Method.'
ASSIGNMENT-I

write(*,50)

50 format('Method.')

write(*,*)'Enter initial root and error value : '

read(*,*)x,e

print*,"Itteraional roots"

10 if(g(x).EQ.0)then

write(*,*)'Incorrect initial root.'

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

write(*,*)'An error has occured.'

endif

x=y

goto 10

20 write(*,30)x

30 format(1x,'Approximate root = ',F10.3)

stop

end

function f(x)

f=cos(x)-x

return

end

function g(x)

g=-sin(x)-1

return
ASSIGNMENT-I

end

Intput

To find a root of an equation using Newton Raphsan Method.

Enter initial root and error value :

0.0001

Output

Itteraional roots

1 1.00000000

Approximate root = 0.750

(b). 𝒇(𝒙) = 𝟐𝒄𝒐𝒔𝒙 + 𝒆𝒙 + 𝟐−𝒙 − 𝟔

Program Code:

program nr

integer count

count=0

print*,'To find a root of an equation using Newton Raphsan

Method.'

write(*,50)

50 format('Method.')

write(*,*)'Enter initial root and error value : '

read(*,*)x,e

print*,"Itteraional roots"

10 if(g(x).EQ.0)then

write(*,*)'Incorrect initial root.'

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

write(*,*)'An error has occured.'

endif

x=y

goto 10

20 write(*,30)x

30 format(1x,'Approximate root = ',F10.3)

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

To find a root of an equation using Newton Raphsan Method.

Method.

Enter initial root and error value :

0.0001
ASSIGNMENT-I

Output

Itteraional roots
1 1.00000000

2 1.90201855

3 1.83499610

Approximate root = 1.830

c). 𝒇(𝒙) = 𝒙𝟑 − 𝒙 − 𝟏

Program Code :

program nr

integer count

count=0

print*,'To find a root of an equation using Newton Raphsan

Method.'

write(*,50)

50 format('Method.')

write(*,*)'Enter initial root and error value : '

read(*,*)x,e

print*,"Itteraional roots"

10 if(g(x).EQ.0)then

write(*,*)'Incorrect initial root.'

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

write(*,*)'An error has occured.'

endif

x=y

goto 10

20 write(*,30)x

30 format (1x,'Approximate root = ',F10.3)

stop

end

function f(x)

f=x*x*x-x-1

return

end

function g(x)

g=3*x**2-1

return

end

Intput

To find a root of an equation using Newton Raphsan Method.

Method.

Enter initial root and error value :

0.0001

Output

Itteraional roots

1 1.00000000
ASSIGNMENT-I

2 1.50000000

3 1.34782612

Approximate root = 1.325

(d) 𝒇(𝒙) = 𝒙𝟒 − 𝒙 − 𝟏𝟎 = 𝟎

Program Code :

program nr

integer count

count=0

print*,'To find a root of an equation using Newton Raphsan

Method.'

write(*,50)

50 format('Method.')

write(*,*)'Enter initial root and error value : '

read(*,*)x,e

print*,"Itteraional roots"

10 if(g(x).EQ.0)then

write(*,*)'Incorrect initial root.'

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

write(*,*)'An error has occured.'

endif

x=y
ASSIGNMENT-I

goto 10

20 write(*,30)x

30 format(1x,'Approximate root = ',F10.3)

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

To find a root of an equation using Newton Raphsan Method.

Method.

Enter initial root and error value :

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

Approximate root = 1.857

2). Using Newton’s Interpolation Formula to evaluate F (0.567),F(.798),F(0.95)

For the given following set of tabulated values:

X 0.0 0.2 0.4 0.6 0.8 1.0


Y=F(X) 0.000 0.0347 0.1173 0.2160 0.2987 0.3333

Program Code :

program NIF

dimension x(20),y(20,20)

write(*,100)

100 format(1x,'To find the value of a function corresponding to

a \')

write(*,110)

110 format ('value of x using newotons divided difference

method')

write(*,*)

write(*,*)'Enter the number of known function values : '

read(*,*)n

write(*,*)'Enter the values of x and the corresponding function values "'

do 10 i=1,n

read(*,*)x(i),y(i,1)

10 continue

write(*,*)'Enter the value of x for which function values is

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(*,*)'the divided difference table is : '

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

60 format(1x,'The corresponding function value is : ',F10.3)

stop

end
ASSIGNMENT-I

Input

For the point F(0.567)

To find the value of a function corresponding to a \

value of x using newotons divided difference method

Enter the number of known function values :

Enter the values of x and the corresponding function values "

0.0

0.00

.2

.0347

.4

.1173

.6

.2160

.8

.2987

1.0

.3333

Enter the value of x for which function values is to be found :

.567

Output

the divided difference table is :

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

The corresponding function value is : 0.200

For the point F(0.798)

To find the value of a function corresponding to a

value of x using newotons divided difference method

Enter the number of known function values :

Enter the values of x and the corresponding function values "


ASSIGNMENT-I

.0

.00

.2

.0347

.4

.1171

.6

.2160

.8

.2987

1.0

.3333

Enter the value of x for which function values is to be found :

.798

Output

the divided difference table is :

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

The corresponding function value is : 0.298

For the point F(0.96)

To find the value of a function corresponding to a

value of x using newton’s divided difference method

Enter the number of known function values :

Enter the values of x and the corresponding function values "

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

Enter the value of x for which function values is to be found :

0.95

Output

the divided difference table is :

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

The corresponding function value is : 0.331

3). Using Lagrange’s Interpolation Formula ,compute the value at X= 1.50


For the following table :

X 1.00 1.20 1.40 1.60 1.80 2.00


Y=F(X) .2420 .1942 .1497 .1109 .0790 0.0540

Program Code :

program li

dimension x(20),y(20)

print*,"To find the function value for a particular value of x

using Lagrange interpolation formula"

write(*,*)

write(*,*)'Enter the number of known function values : '

read(*,*)n

write(*,*)'Enter the values of x and the corresponding function

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

40 format(1x,'The function value at x= ',F7.2,'is : ',F9.3)

stop

end

Intput

To find the function value for a particular value of x using Lagrange interpola

tion formula

Enter the number of known function values :

Enter the values of x and the corresponding function values

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

Enter the value of x for which function values is to be found :

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 of linear equations.

𝟑𝒙𝟏 − 𝟕𝒙𝟐 − 𝟐𝒙𝟑 = −𝟕

−𝟑𝒙𝟏 + 𝟓𝒙𝟐 + 𝒙𝟑 = 𝟓

𝟔𝒙𝟏 − 𝟒𝒙𝟐 = 𝟐

(i)Gauss Elimination Method

Program Code :

PROGRAM gelmn

dimension a(30,30),x(30)

write(*,10)

10 format(1x,'To solve a linear system of equations using Gauss ')

write(*,20)

20 format('Elimination method with pivoting(using subroutine).')

write(*,*)'Enter the number of variables:'

read(*,*)n
ASSIGNMENT-I

write(*,*)'Enter the coefficients in the equations:'

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

write(*,*)'Ill conditioned equations.'

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

write(*,*)'Values of the variables are as follows:'

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

To solve a linear system of equations using Gauss

Elimination method with pivoting(using subroutine).

Enter the number of variables:

3
ASSIGNMENT-I

Enter the coefficients in the equations:

-7

-2

-7

-3

-4

Output

Values of the variables are as follows:

3.000

4.000

-6.000

(ii)LU Factorization Method

Program Code :

real l(20,20)

dimension a(20,20),b(20),u(20,20),x(20),y(20)

write(*,130)

130 format(1x,'To solve a system of equations using method of ')

write(*,140)

140 format(1x'LU factorisation')


ASSIGNMENT-I

write(*,*)'Enter the number of equations:'

read(*,*)n

write(*,*)'Enter the coeffficients of each equations:'

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

write(*,*)'the solution of system is :'

do 150 i=1,n

write(*,160)x(i)

160 format(1x,F6.3)

150 continue

stop

end

Input
ASSIGNMENT-I

To solve a system of equations using method of

LU factorisation

Enter the number of equations:

Enter the coeffficients of each equations:

-7

-2

-7

-3

-4

Output

1.00000000 -1.00000000 2.00000000 0.00000000 1.0000000

0 -5.00000000 0.00000000 0.00000000 1.00000000

3.00000000 0.00000000 0.00000000 -7.00000000 -2.0000000

0 0.00000000 -2.00000000 -1.00000000 -1.00000000

-7.00000000 -2.00000000 6.00000000

The solution of system is :

3.000

4.000

-6.000

You might also like