Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
13 views13 pages

Fortran Code

The document outlines four practical exercises focused on statistical calculations and root-finding methods for equations. It includes computations for mean, variance, skewness, and kurtosis, as well as methods like Bisection, Regula Falsi, and Newton-Raphson to find roots of a quadratic equation. Each section provides code examples, outputs, and conclusions regarding the efficiency and results of the methods used.
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)
13 views13 pages

Fortran Code

The document outlines four practical exercises focused on statistical calculations and root-finding methods for equations. It includes computations for mean, variance, skewness, and kurtosis, as well as methods like Bisection, Regula Falsi, and Newton-Raphson to find roots of a quadratic equation. Each section provides code examples, outputs, and conclusions regarding the efficiency and results of the methods used.
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/ 13

Date: 16/01/2023 page:

Practical: 1

Objective: Compute mean, variance, moments, skewness and kurtosis from the data.
Generate a table about goodness of values under consideration.

Formula used: Let Xi be the ith data point and n be the number of data points.
Then we have used the following formulas:
n
1
Mean = X =¿ ∑X
n i=1 i
n
1
Variance = ∑ ( X i−X )
2

n i=1
n
1 r
Rth order central moment = μr =
n ∑ (X i −X )
i=1

μ3
Skewness = 3
2
μ 2

μ4
Kurtosis = 2
μ2

One sigma limit = (mean – sd , mean + sd)

Code:
PROGRAM STAT

!AVERAGE OF THE NUMBERS:-----------------------

INTEGER :: N

REAL :: SUM, AVRG, X(100), VAR, VAR1, TEMP, MED, SK, KR

PRINT*, 'ENTER THE NUMBER OF DATA POINTS'

READ*, N

PRINT*, 'ENTER THE VALUE OF X'

READ*, (X(i),i=1,N)

SUM=0

DO i = 1,N

SUM=SUM+X(i)
END DO

AVRG = SUM/N

Page:

!-----VARIANCE---------------------------

VAR = 0

DO i = 1,N

VAR = VAR + (X(i)-AVRG)*(X(i)-AVRG)

END DO

VAR1 = VAR/N

!-------SKEWNESS-----------------------------------

V=0

DO i = 1,N

V = V + (X(i)-AVRG)**3

END DO

M = SQRT(VAR1**3)

SK = (V/(N*M))

MU3 = V/N

!---------KURTOSIS---------------------------------

V1 = 0

DO i = 1,N

V1 = V1+ (X(i)-AVRG)**4

END DO

KR = (V1/((N)*((VAR1)**2)))

MU4 = V1/N

SD = ABS( SQRT(VAR1))

A1 = AVRG-SD

A2 = AVRG + SD

PRINT*, 'THE TOTAL SUM OF THE NUMBERS ARE', SUM

PRINT*, 'THE MEAN OF THE NUMBERS ARE' , AVRG

PRINT*, 'THE VARIANCE IS', VAR1


PRINT*, ‘THE STANDARD DEVIATION IS’ , SD

PRINT*, 'THE SECOND ORDER CENTRAL MOMENT IS', VAR

Page:

PRINT*, 'THE THIRD ORDER CENTRAL MOMENT IS', MU3

PRINT*, 'THE FOURTH ORDER CENTRAL MOMENT IS', MU4

PRINT*, 'THE SKEWNESS IS', SK

PRINT*, 'THE KURTOSIS IS', KR

PRINT*, 'THE TABLE OF GOODNESS OF VALUES'

DO i =1,N

IF (X(i).GT.A2) THEN

PRINT*, X(i) , 'IS BAD'

ELSE IF (X(i).LT.A1) THEN

PRINT*, X(i) , 'IS BAD'

ELSE

PRINT*, X(i) , 'IS GOOD'

END IF

END DO

END PROGRAM STAT

Output:
ENTER THE NUMBER OF DATA POINTS

ENTER THE VALUE OF X

2.3 1.4 3.6 2.3 0.4 6.3 1.7

THE TOTAL SUM OF THE NUMBERS ARE 18.0000000

THE MEAN OF THE NUMBERS ARE 2.57142854

THE VARIANCE IS 3.13632655

THE STANDARD DEVIATION IS 1.77096772

THE SECOND ORDER CENTRAL MOMENT IS 3.13632655

THE THIRD ORDER CENTRAL MOMENT IS 5

THE FOURTH ORDER CENTRAL MOMENT IS 31


THE SKEWNESS IS 1.15359914

THE KURTOSIS IS 3.18193221

Page:

THE TABLE OF GOODNESS OF VALUES

2.29999995 IS GOOD

1.39999998 IS GOOD

3.59999990 IS GOOD

2.29999995 IS GOOD

0.400000006 IS BAD

6.30000019 IS BAD

1.70000005 IS GOOD

Results: we have got the following results:


Mean = 2.57142854, Variance = 3.13632655, μ2 = 3.13632655, μ3 = 5, μ4 = 31, sk = 1.15359914,
kurtosis = 3.18193221.

From the table, we get that 5 data points are good which means it belongs to the one sigma limits
and 2 are bad which lie outside the one sigma limit.

Conclusion: hence, we have taken 7 real numbers using user input and compute mean,
variance, standard deviation, moments, skewness and kurtosis. Also we have obtained the
one sigma limits of the data points and got a table about Goodness of values under
consideration such that the data points are good or bad.
Date: 06/02/2023 Page:

Practical: 2

Objective: Find the root of the equation 2x2+ 4x - 4 = 0 using Bisection method.
Formula used: To obtain the root of the polynomial equation we have used Bisection
method.
Method:
For any continuous function f(x),

 Find two points, say a and b such that a < b and f(a)* f(b) < 0
 Find the midpoint of a and b, say “t”
 t is the root of the given function if f(t) = 0; else follow the next step
 Divide the interval [a, b] – If f(t)*f(a) <0, there exist a root between t and a
– else if f(t) *f (b) < 0, there exist a root between t and b
 Repeat above three steps until f(t) = 0.
The bisection method is an approximation method to find the roots of the given equation by
repeatedly dividing the interval. This method will divide the interval until the resulting
interval is found, which is extremely small.

Code:
PROGRAM BISECTION_METHOD
IMPLICIT NONE
REAL, PARAMETER :: ERROR = 0.0001
REAL :: A,B,F,C
INTEGER I
WRITE(*,*)'ENTER THE VALUE OF A AND B:'
10 READ(*,*) A,B
15 IF (F(A)*F(B).LT.0)THEN
I=I+1
C = (A+B)/2
ELSE
WRITE(*,*)' TRY WITH ANOTHER VALUES OF A AND B '
GO TO 10
Page:
END IF
IF (F(A)*F(C).LT.0)THEN
B=C
ELSE
A=C
ENDIF
IF (ABS(B-A).GT.ERROR) GO TO 15
WRITE(*,*) 'THE ROOT IS =', C
WRITE(*,*) 'THE NO.OF ITERATIONS REQUIRED IS =',I
END
REAL FUNCTION F(X)
REAL :: X
F= 2*X**2 +4*X-4
END

Output:
ENTER THE VALUE OF A AND B:
01
THE ROOT IS = 0.731994629
THE NO.OF ITERATIONS REQUIRED IS = 14

Conclusion: Here we have got the root of the equation as 0.731994629 and to get the
root the number of iterations required is 14.
Date: 13/02/2023 page:

Practical: 3

Objective: To find the root of the given equation using Regula Falsi Method.
Formula used: Consider an equation f(x) = 0, which contains only one variable, i.e. x.
To find the real root of the equation f(x) = 0, we consider a sufficiently small interval (a, b)
where a < b such that f(a) and f(b) will have opposite signs. According to the intermediate
value theorem, this implies a root lies between a and b.
Also, the curve y = f(x) will meet the x-axis at a certain point between A[a, f(a)] and B[b,
f(b)].
Now, the equation of the chord joining A[a, f(a)] and B[b, f(b)] is given by:

f (b)−f (a)
y−f (a)= .( x−a)
(b−a)

Let y = 0 be the point of intersection of the chord equation (given above) with the x-axis.
Then,
f (b)−f (a)
−f (a)= .(x−a)
(b−a)

This can be simplified as:


−f ( a )( b−a )
=x−a
f ( b )−f ( a )
af ( a ) −bf ( a )
+ a= x
f ( b )−f ( a )
af ( a )−bf ( a )+ af ( b )−af ( a )
⇒ x=
f ( b ) −f ( a )
af ( b )−bf ( a )
⇒ x=
f ( b )−f ( a )

Thus, the first approximation is x1 = [af(b) – bf(a)]/ [f(b) – f(a)]


Also, x1 is the root of f(x) if f(x1) = 0.
If f(x1) ≠ 0 and if f(x1) and f(a) have opposite signs, then we can write the second
approximation as:
x2 = [af(x1) – x1f(a)]/ [f(x1) – f(a)]
Similarly, we can estimate x3, x4, x5, and so on.

Page:

Code:
PROGRAM REGULAFALSI
IMPLICIT NONE
REAL:: X0,X1,X2,F,I
WRITE(*,*)'ENTER THE VALUE OF X0,X1'
READ(*,*)X0,X1
10 X2=(X1*F(X0)-X0*F(X1))/(F(X0)-F(X1))
IF(ABS(X2-X1).GE.0.0001)THEN
X1=X2
I=I+1
GOTO 10
ENDIF
WRITE(*,*)'THE VALUE OF THE ROOT IS:'
WRITE(*,*)X2
WRITE(*,*)'THE NUMBER OF ITERATIONS REQUIRED:'
WRITE(*,*)I
STOP
END
REAL FUNCTION F(X)
IMPLICIT NONE
REAL::X
F=2*X**2+4*X-4
END

Output:
ENTER THE VALUE OF X0,X1
0,1
THE VALUE OF THE ROOT IS:
Page:
0.732057452
THE NUMBER OF ITERATIONS REQUIRED:
7

Conclusion: The root of the equation using Regula Falsi is 0.732057452. The number of
iterations required is 7. It can be said that using Regula Falsi method the value converges
more rapidly compared to bisection method.
Date: 20/02/2023 page:

Practical: 4

Objective: To find the root of the given equation using Newton-Raphson method.
Formula Used: Let x0 be the approximate root of f(x) = 0 and let x1 = x0 + h be the correct
root. Then f(x1) = 0
⇒ f(x0 + h) = 0….(1)
By expanding the above equation using Taylor’s theorem, we get:
f(x0) + hf1(x0) + … = 0
⇒ h = -f(x0) /f’(x0)
Therefore, x1 = x0 – f(x0)/ f’(x0)
Now, x1 is the better approximation than x0.
Similarly, the successive approximations x2, x3, …., xn+1 are given by
f (x n )
x n+1=x n −
f ' (x n)

This is called Newton-Raphson formula.

Code:

PROGRAM NRMETHOD

IMPLICIT NONE

REAL:: X0,X1,F,FD

INTEGER I

I=0

WRITE(*,*)'ENTER THE VALUE OF X0'

READ(*,*)X0

10 X1= X0-(F(X0)/FD(X0))
IF(ABS(X1-X0).GE.0.0001)THEN

X0=X1

Page:

I=I+1

GOTO 10

ENDIF

WRITE(*,*)'THE VALUE OF THE ROOT IS:'

WRITE(*,*)X1

WRITE(*,*)'THE NUMBER OF ITERATIONS REQUIRED:'

WRITE(*,*)I

STOP

END

REAL FUNCTION F(X)

IMPLICIT NONE

REAL::X

F=2*X**2+4*X-4

END

REAL FUNCTION FD(X)

IMPLICIT NONE

REAL X

FD=4*X+4
END

Output:

ENTER THE VALUE OF X0


0

THE VALUE OF THE ROOT IS:

Page:

0.732050776

THE NUMBER OF ITERATIONS REQUIRED:


3

Conclusion:
The root of the equation using Newton-Raphson method is:0.732050776. The number of
iterations required is 3. It provides a solution using fewer number of iterations as compared
to Bisection and Regula falsi method.

You might also like