0% found this document useful (0 votes)
6 views15 pages

Chapter 6

The document contains a Fortran program for calculating the inverse of a 3x3 matrix using Gaussian elimination, along with an augmented matrix. It includes detailed steps for matrix manipulation, including row operations and normalization, to achieve the final inverse. Additionally, it discusses the least squares fit method for finding the best-fit line for experimental data points fitting a quadratic equation.

Uploaded by

Vs Vs
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)
6 views15 pages

Chapter 6

The document contains a Fortran program for calculating the inverse of a 3x3 matrix using Gaussian elimination, along with an augmented matrix. It includes detailed steps for matrix manipulation, including row operations and normalization, to achieve the final inverse. Additionally, it discusses the least squares fit method for finding the best-fit line for experimental data points fitting a quadratic equation.

Uploaded by

Vs Vs
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/ 15

! **Name: GJNEW.F95 *******Name of this File in MS word: GJNEW.

DOCX************************
DIMENSION P (3, 3), A (3, 6), PI (3, 3)
DOUBLE PRECISION A, P, ALPHA
INTEGER R, C
! IN GENERAL R IS ROW NUMBER & C IS COLOUMN NUMBER IN THE MATRIX
! P => PRIMARY MARTIX; A => AUGMENTED MATRIX
! PI => P INVERSE or INVERSE OF MATRIX P

DATA P/ 2.0, -3.0, 1.0, -1.0, 4.0, 3.0, 3.0, -5.0, -6.0 /
DO 10 R = 1, 3
DO 15 C = 1, 6 This is matrix ‘A’ and it is a 3 X 6 matrix

IF (C < = 3) THEN
A(R, C) = P(R, C)
ELSE 𝑪𝟏 𝑪𝟐 𝑪𝟑 𝑪𝟒 𝑪𝟓 𝑪𝟔
A(R, C) = 0.0
IF (C = = R+3) A(R, C) = 1.0 A = 𝑹𝟏 𝟐 −𝟏 𝟑 𝟏 𝟎 𝟎
END IF 𝑹𝟐 −𝟑 𝟒 −𝟓 𝟎 𝟏 𝟎
(𝑹 𝟑 𝟏 𝟑 −𝟔 𝟎 𝟎 𝟏)
15 CONTINUE
10 CONTINUE
! WRITE(*,100) (( A(R,C) , C=1, 6), R=1, 3)

DO R = 1, 3
WRITE (*, 100) (A(R, C), C=1, 6)
WRITE (*,*)
END DO
WRITE (*,*) '---------------------------------------------------'

Page 1
! ALPHA IS Regulating Coefficient - which regulates the Coefficient depending on K and I
DO 20 K = 1, 3 Note: RI  RI -  RK [Here  = AIK / AKK ]
DO 25 I = 1, 3 further we also note that I  K or If I = K then we
must have to make  = 0.0
! ALPHA = A(I, K) / A(K, K)
IF (I = = K) GO TO 25 R1 = R1
ALPHA = A(I, K) / A(K, K) R2  R2 -  R1 [Here  = A21 / A11 ]
! IF (I = = K) ALPHA=0.0 R3  R3 -  R1 [Here  = A31 / A11 ]
DO 30 C = 1, 6 R1  R1 -  R2 [Here  = A12 / A22 ]
A(I, C) = A(I, C) – ALPHA * A(K, C) R2 = R2
30 CONTINUE R3  R3 -  R2 [Here  = A32 / A22 ]
R1  R1 -  R3 [Here  = A13 / A33 ]
25 CONTINUE
R2  R2 -  R3 [Here  = A23 / A33 ]
20 CONTINUE
R3 = R3
DO R = 1, 3
WRITE (*,100) (A(R, C), C=1, 6) 2.0000 0.0000 0.0000 0.5294 -0.1765 0.4118
WRITE (*,*) 0.0000 2.5000 0.0000 1.6912 1.1029 - 0.0735
0.0000 0.0000 -6.8000 -2.6000 -1.4000 1.0000
END DO
WRITE (*,*) '------------------------------------'
! WRITE(*,100) (( A(R,C) , C=1, 6), R=1, 3)
DO R = 1, 3 𝑪𝟏 𝑪𝟐 𝑪𝟑 𝑪𝟒 𝑪𝟓 𝑪𝟔
DIN = A (R, R) A = 𝑹𝟏 𝑨𝟏𝟏 𝑨𝟏𝟐 𝑨𝟏𝟑 𝑨𝟏𝟒 𝑨𝟏𝟓 𝑨𝟏𝟔
DO C =1, 6 𝑹𝟐 𝑨𝟐𝟏 𝑨𝟐𝟐 𝑨𝟐𝟑 𝑨𝟐𝟒 𝑨𝟐𝟓 𝑨𝟐𝟔
A(R, C) = A(R, C) / DIN (𝑹 𝟑 𝑨𝟑𝟏 𝑨𝟑𝟐 𝑨𝟑𝟑 𝑨𝟑𝟒 𝑨𝟑𝟓 𝑨𝟑𝟔 )
END DO
END DO

Page 2
! WRITE(*,100) (( A(R,C) , C=1, 6), R=1, 3)
1.0000 0.0000 0.0000 0.2647 -0.0882 0.2059
DO R = 1, 3
0.0000 1.0000 0.0000 0.6765 0.4412 -0.0294
WRITE (*,100) (A(R, C), C = 1, 6)
WRITE (*,*) 0.0000 0.0000 1.0000 0.3824 0.2059 -0.1471
END DO

WRITE (*,*) '------------------------------------------------------------'

! MATRIX PI => THIS IS INVERSE OF MATRIX P


DO R = 1, 3
DO C = 4, 6
! Here we are making the inverse of matrix i.e.PI and storing
! in memory if we want to use PI otherwise there is no need
! of this matrix if we want to simply print inverse of P i.e. PI
I=C-3
PI(R, I) = A(R, C) Hence the inverse of the matrix P is given below
END DO
! WRITE (*,100) PI(R, 1), PI(R, 2), PI(R, 3)
WRITE (*,100) (A(R, IC), IC = 4, 6) 𝟎. 𝟐𝟔𝟒𝟕 − 𝟎. 𝟎𝟖𝟖𝟐 𝟎. 𝟐𝟎𝟓𝟗
WRITE (*,*)
ENDDO 𝟎. 𝟔𝟕𝟔𝟓 𝟎. 𝟒𝟒𝟏𝟐 − 𝟎. 𝟎𝟐𝟗𝟒

100 FORMAT (6 (5X, F7.4)) ( 𝟎. 𝟑𝟖𝟐𝟒 𝟎. 𝟐𝟎𝟓𝟗 − 𝟎. 𝟏𝟒𝟕𝟏)


STOP
END

Page 3
2.0000 -1.0000 3.0000 1.0000 0.0000 0.0000

-3.0000 4.0000 -5.0000 0.0000 1.0000 0.0000

1.0000 3.0000 -6.0000 0.0000 0.0000 1.0000

---------------------------------------------------
2.0000 0.0000 0.0000 0.5294 -0.1765 0.4118

0.0000 2.5000 0.0000 1.6912 1.1029 -0.0735

0.0000 0.0000 -6.8000 -2.6000 -1.4000 1.0000

---------------------------------------------------
1.0000 0.0000 0.0000 0.2647 -0.0882 0.2059

0.0000 1.0000 0.0000 0.6765 0.4412 -0.0294

-0.0000 -0.0000 1.0000 0.3824 0.2059 -0.1471

------------------------------------------------------------
0.2647 -0.0882 0.2059

0.6765 0.4412 -0.0294

0.3824 0.2059 -0.1471

Page 4
Q. Find the inverse of the following matrices

(a) 1 2 3 (b) 1 2 3

4 5 6 4 5 6

7 8 9 7 8 9

DATA FED and printout along with AUGMENTED MATRIX


3.0000 2.0000 1.0000 1.0000 0.0000 0.0000
1.0000 1.0000 1.0000 0.0000 1.0000 0.0000
5.0000 1.0000 -1.0000 0.0000 0.0000 1.0000
---------------------------------------------------
Inverse of Matrix is given below:

-1.0000 1.5000 0.5000


3.0000 -4.0000 -1.0000
-2.0000 3.5000 0.5000

! ************************Name:GJNEW.F95*******************
DIMENSION P(3,3),A(3,6),PI(3,3)
DOUBLE PRECISION A,P,ALPHA
INTEGER R,C
! P => PRIMARY MARTIX; A => AUGMENTED MATRIX
! PI => P INVERSE or INVERSE OF MATRIX P

DATA P/2.0,-3.0,1.0,-1.0,4.0,3.0,3.0,-5.0,-6.0/

DO 10 R=1,3
DO 15 C=1,6

IF (C .LE.3) THEN
A(R,C)=P(R,C)
ELSE
A(R,C)=0.0
IF (C .EQ. R+3) A(R,C)=1.0
END IF
15 CONTINUE
10 CONTINUE

DO R=1,3
WRITE(*,100) (A(R,C), C=1,6)
WRITE(*,*)
END DO
WRITE(*,*) '---------------------------------------------------'

DO 20 K=1,3
DO 25 I=1,3

Page 5
IF (I==K) GO TO 25
! ALPHA IS Regulating Coefficient - which regulates the Coefficient
!depending on K and I
ALPHA=A(I,K)/A(K,K)

DO 30 C=1,6
A(I,C)=A(I,C)-ALPHA*A(K,C)
30 CONTINUE

25 CONTINUE
20 CONTINUE

DO R=1,3
WRITE(*,100) (A(R,C), C=1,6)
WRITE(*,*)
END DO

WRITE(*,*) '--------------------------------------------------'

DO R=1,3
DIN=A(R,R)
DO C=1,6

A(R,C)=A(R,C)/DIN

END DO
END DO

DO R=1,3
WRITE(*,100) (A(R,C), C=1,6)
WRITE(*,*)
ENDDO

WRITE(*,*) '---------------------------------------------------'

! MATRIX PI => THIS IS INVERSE OF MATRIX P


DO R=1,3
DO C=4,6
! Here we are making the inverse of matrix i.e.PI and storing
! in memory if we want to use PI otherwise there is no need
! of this matrix if we want to simply print inverse of P i.e. PI
I=C-3
PI(R,I)=A(R,C)
END DO
! WRITE(*,100) PI(R,1),PI(R,2),PI(R,3)
WRITE(*,100) (A(R,IC), IC=4,6)
WRITE(*,*)
ENDDO

100 FORMAT(6(5X,F7.4))

STOP
END

Page 6
Least Square Fit Method:

Page 7
Page 8
Page 9
Page 10
Page 11
An experiment was done in which ‘n’ observations were made at different values of x. Say at x1
the observation was y1, at x2 the observation was y2, at x3 the observation was y3, - - - and finally
at at xn the observation was yn. It is expected that all this data fit to an equation y = a + bx + cx2.
Using least square fit method we have to find the best line for all the observed points. Hence we
must find the best values of a, b and c using these experimental values and draw the best graph.
Thus the experimental points are given as follows:

𝑥 𝑥1 𝑥2 𝑥3 𝑥4 - - 𝑥𝑛
𝑦 = 𝑓(𝑥) 𝑦1 𝑦2 𝑦3 𝑦4 - - 𝑦𝑛

We know that these experimental points fit to the equation y = a + bx + cx2. The best values of a,
b and c are obtained from the above experimental values as follows:

Now using x1, x2, . . . . . . . . xn we have the following equations which are called residual
equations using y = a + bx + cx2 - - - - - - - (1)
2
𝑦1 = 𝑎 + 𝑏𝑥1 + 𝑐𝑥1 These are equations obtained from
2
𝑦2 = 𝑎 + 𝑏𝑥2 + 𝑐𝑥2 equation (1) at different values of
2 x.
𝑦3 = 𝑎 + 𝑏𝑥3 + 𝑐𝑥3
-----------------
-----------------
𝑦𝑛 = 𝑎 + 𝑏𝑥𝑛 + 𝑐𝑥𝑛2

Adding all the equations (which


are called residual equations) we
get the equation (2) called normal
equation
𝑛 n n
This is equation (2).
∑ 𝑦𝑖 = 𝑛𝑎 + 𝑏 ∑ 𝑥𝑖 + 𝑐 ∑ 𝑥𝑖2 The first normal equation formed
𝑖=1 i= 1 i= 1 from x1, x2,- - - - xn using equation
(1)

Now let us multiply both sides of equation (1) with x, so that we have
yx = ax + bx2 + cx3 - - - - - - (3)
Now using x1, x2,- - - - xn and equation (3) we formulate the residual equations and normal
equation which are given below

Page 12
𝑦1 𝑥1 = 𝑎𝑥1 + 𝑏𝑥12 + 𝑐𝑥13 These are equations
𝑦2 𝑥2 = 𝑎𝑥2 + 𝑏𝑥22 + 𝑐𝑥23 obtained from equation (3)
𝑦3 𝑥3 = 𝑎𝑥3 + 𝑏𝑥32 + 𝑐𝑥33 at different values of x.
-----------------
----------------
𝑦𝑛 𝑥𝑛 = 𝑎𝑥𝑛 + 𝑏𝑥𝑛2 + 𝑐𝑥𝑛3

Adding all the equations


(which are called residual
equations) we get the
equation (4) called normal
equation
𝑛 n n n
This is equation (4).
∑ 𝑦𝑖 𝑥𝑖 = 𝑎 ∑ 𝑥𝑖 + 𝑏 ∑ 𝑥𝑖2 + 𝑐 ∑ 𝑥𝑖3 The second normal
𝑖=1 i= 1 i= 1 i= 1 equation formed from x1,
x2,- - - - xn using equation
(3)

Now multiplying both sides of equation (1) on both sides with x2 we get
yx2 = ax2 + bx3 + cx4 - - - - - - (5)
Now using x1, x2,- - - - xn and equation (5) we formulate the normal equations and residual
equation which are given below

𝑦1 𝑥12 = 𝑎𝑥12 + 𝑏𝑥13 + 𝑐𝑥14 These are equations


𝑦2 𝑥22 = 𝑎𝑥22 + 𝑏𝑥23 + 𝑐𝑥24 obtained from equation
𝑦3 𝑥32 = 𝑎𝑥32 + 𝑏𝑥33 + 𝑐𝑥34 (5) at different values of
----------------- x.
----------------
𝑦𝑛 𝑥𝑛2 = 𝑎𝑥𝑛2 + 𝑏𝑥𝑛3 + 𝑐𝑥𝑛4
Adding all the equations
(which are called residual
equations) we get the
equation (6) called normal
equation
𝑛 n n n
This is equation (6).
∑ 𝑦𝑖 𝑥𝑖2 = 𝑎 ∑ 𝑥𝑖2 + 𝑏 ∑ 𝑥𝑖3 + 𝑐 ∑ 𝑥𝑖4 The normal equation
𝑖=1 i= 1 i= 1 i= 1 formed from x1, x2,- - - -
xn using equation (5)

Thus we have the following three normal equations i.e.

Page 13
n n 𝑛

𝑛𝑎 + 𝑏 ∑ 𝑥𝑖 + 𝑐 ∑ 𝑥𝑖2 = ∑ 𝑦𝑖 Eqn. (2)


i= 1 i= 1 𝑖=1
n n n 𝑛

𝑎 ∑ 𝑥𝑖 + 𝑏 ∑ 𝑥𝑖2 + 𝑐 ∑ 𝑥𝑖3 = ∑ 𝑦𝑖 𝑥𝑖 Eqn. (4)


i= 1 i= 1 i= 1 𝑖=1
n n n 𝑛

𝑎 ∑ 𝑥𝑖2 + 𝑏 ∑ 𝑥𝑖3 + 𝑐 ∑ 𝑥𝑖4 = ∑ 𝑦𝑖 𝑥𝑖2 Eqn.(6)


i= 1 i= 1 i= 1 𝑖=1

Hence from the above three equations we formulate the matrix equation as follows:
n n 𝑛

𝑛 ∑ 𝑥𝑖 ∑ 𝑥𝑖2 ∑ 𝑦𝑖
i= 1 i= 1 𝑖=1
n n n 𝑎 𝑛

∑ 𝑥𝑖 ∑ 𝑥𝑖2 ∑ 𝑥𝑖3 (𝑏 ) = ∑ 𝑦𝑖 𝑥𝑖
i= 1 i= 1 i= 1 𝑐 𝑖=1
n n n 𝑛

∑ 𝑥𝑖2 ∑ 𝑥𝑖3 ∑ 𝑥𝑖4 ∑ 𝑦𝑖 𝑥𝑖2


(i= 1 i= 1 i= 1 ) ( 𝑖=1 )

The elements in first matrix on the left hand side are the coefficients of a, b and c and is called
coefficient matrix ‘C’ (This is capital C not small c)
The second matrix on the left hand side with elements a, b and c is the unknown matrix U
The third matrix is known matrix named as K
Hence the above matrix equation can be written as
CU=K
Now left multiplying on both sides with C-1 (i.e. C inverse) we have
C-1 C U = C-1 K
or U = C-1 K [since C-1 C = 1]
Hence finding inverse of the matrix C and the multiplying with matrix K we get the unknown
matrix U and hence the best values of the elements a, b and c.

Q. Given a set of ‘n’ experimental points [say y1 is the value at x1, y2 is the value at x2, - - - - - -
- - and yn is the value at xn] . These experimental points fit to the equation
y = bx + a

Formulate the matrix equation for finding the best values of a and b and also obtain the expression
in matrix form for finally arriving at the values of a and b i.e U = C-1 K

Page 14
Refer the book by C. Xavier for evaluating inverse of matrix [see page 350 for the complete
FORTRAN program].
[Use Gauss-Jordan method to obtain inverse of a matrix]

Page 15

You might also like