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

Assignment 6 Code Input Output

The document contains multiple Fortran programs that implement various numerical methods for solving linear systems, including Jacobi and SOR iterative methods, Gram-Schmidt orthogonalization, and linear system solving. Each section provides the source code, input data, and output results for the respective methods. The programs are designed to read matrices and vectors from input files, perform calculations, and output the results to specified files.

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)
4 views8 pages

Assignment 6 Code Input Output

The document contains multiple Fortran programs that implement various numerical methods for solving linear systems, including Jacobi and SOR iterative methods, Gram-Schmidt orthogonalization, and linear system solving. Each section provides the source code, input data, and output results for the respective methods. The programs are designed to read matrices and vectors from input files, perform calculations, and output the results to specified files.

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-6

Answer to the question no. 1


Code: A6Q1.f90
Program Jacobi
implicit none
integer,Parameter::n=6,max_iter=1000
real::A(n,n),x1(n),b(n),x2(n),tol=1.0e-3,er,s
integer::i,j,iter,k

open(unit=2,file='Jacobiinput.txt')
open(unit=4,file='JacobiOut.txt')
do i=1,n
read(2,*)(A(i,j),j=1,n)
end do

read(2,*)(b(i),i=1,n)
read(2,*)(x1(i),i=1,n)
close(2)

write(4,'("Iteration",10x,"x1",10x,"x2"10x,"x3",10x,"x4",10x,"x5",10x,"x6",10x,"Error")'
)
write(4,'(120("-"))')
do k=1,max_iter
do i=1,n
s=0.0
do j=1,n
if(i/=j) s=s+A(i,j)*x1(j)
end do
x2(i)=(b(i)-s)/A(i,i)

end do
er=maxval(abs(x2-x1))

write(4,'(i4,10x,6f11.5,6x,f12.5)')k,x2,er

if(er < tol)exit


x1=x2

end do
end program

Input: Jacobiinput.txt
4. -1. 0. -1. 0. 0.
-1. 4. -1. 0. -1. 0.
0. -1. 4. 0. 0. -1.
-1. 0. 0. 4. -1. 0.
0. -1. 0. -1. 4. -1.
0. 0. -1. 0. -1. 4.

0. 5. 0. 6. -2. 6.

0. 0. 0. 0. 0. 0.

Output: JacobiOut.txt
Iteration x1 x2 x3 x4 x5 x6
Error
----------------------------------------------------------------------------------------
--------------------------------
1 0.00000 1.25000 0.00000 1.50000 -0.50000 1.50000
1.50000
2 0.68750 1.12500 0.68750 1.37500 0.56250 1.37500
1.06250
3 0.62500 1.73438 0.62500 1.81250 0.46875 1.81250
0.60938
4 0.88672 1.67969 0.88672 1.77344 0.83984 1.77344
0.37109
5 0.86328 1.90332 0.86328 1.93164 0.80664 1.93164
0.22363
6 0.95874 1.88330 0.95874 1.91748 0.94165 1.91748
0.13501
7 0.95020 1.96478 0.95020 1.97510 0.92957 1.97510
0.08148
8 0.98497 1.95749 0.98497 1.96994 0.97874 1.96994
0.04918
9 0.98186 1.98717 0.98186 1.99093 0.97434 1.99093
0.02968
10 0.99452 1.98451 0.99452 1.98905 0.99226 1.98905
0.01791
11 0.99339 1.99533 0.99339 1.99670 0.99065 1.99670
0.01081
12 0.99801 1.99436 0.99801 1.99601 0.99718 1.99601
0.00653
13 0.99759 1.99830 0.99759 1.99880 0.99660 1.99880
0.00394
14 0.99927 1.99795 0.99927 1.99855 0.99897 1.99855
0.00238
15 0.99912 1.99938 0.99912 1.99956 0.99876 1.99956
0.00143
16 0.99974 1.99925 0.99974 1.99947 0.99963 1.99947
0.00087

Answer to the question no. 2


Code: A6Q2.f90
Program Jacobi_2
implicit none
integer,parameter::n=3,max_iter=25
real::b(n),x1(n),x2(n),er,A(n,n),exact(n),s
integer::i,j,iter
open(unit=3,file='jacobinputttttttttttttttttt.txt')
open(unit=4,file='out2jacobi.txt')

do i=1,n
read(3,*)(A(i,j),j=1,n)
end do

read(3,*)(b(i),i=1,n)
read(3,*)(x1(i),i=1,n)
read(3,*)(exact(i),i=1,n)
close(3)

write(4,'("Iteration",7x,"x1",12x,"x2",15x,"x3",10x,"Error")')
write(4,'(70("-"))')

do iter=1,max_iter
do i=1,n
s=0.0
do j=1,n
if(i/=j)s=s+A(i,j)*x1(j)
end do
x2(i)=(b(i)-s)/A(i,i)
end do
er=maxval(abs(x2-exact))
write(4,'(i2,3f17.5,f10.5)')iter,x2,er
x1=x2
end do

end program

Input: jacobinputttttttttttttttttt.txt
2 -1 1
2 2 2
-1 -1 2

-1 4 -5

0 0 0

1 2 -1

Output: out2jacobi.txt
Iteration x1 x2 x3 Error
----------------------------------------------------------------------
1 -0.50000 2.00000 -2.50000 1.50000
2 1.75000 5.00000 -1.75000 3.00000
3 2.87500 2.00000 0.87500 1.87500
4 0.06250 -1.75000 -0.06250 3.75000
5 -1.34375 2.00000 -3.34375 2.34375
6 2.17188 6.68750 -2.17188 4.68750
7 3.92969 2.00000 1.92969 2.92969
8 -0.46484 -3.85938 0.46484 5.85938
9 -2.66211 2.00000 -4.66211 3.66211
10 2.83105 9.32422 -2.83105 7.32422
11 5.57764 2.00000 3.57764 4.57764
12 -1.28882 -7.15527 1.28882 9.15527
13 -4.72205 2.00000 -6.72205 5.72205
14 3.86102 13.44409 -3.86102 11.44409
15 8.15256 2.00000 6.15256 7.15256
16 -2.57628 -12.30511 2.57628 14.30511
17 -7.94070 2.00000 -9.94070 8.94070
18 5.47035 19.88139 -5.47035 17.88139
19 12.17587 2.00000 10.17587 11.17587
20 -4.58794 -20.35174 4.58794 22.35174
21 -12.96984 2.00000 -14.96984 13.96984
22 7.98492 29.93968 -7.98492 27.93968
23 18.46230 2.00000 16.46230 17.46230
24 -7.73115 -32.92460 7.73115 34.92460
25 -20.82787 2.00000 -22.82787 21.82787

Answer to the question no. 3


Code: A6Q3.f90
program sor_iterative
implicit none
integer,parameter::n=6,max_i=1000
integer::i,j,k
real::a(n,n),b(n),x(n),x0(n),e(n),w=1.1,t=1e-5,ssum
open(unit=24,file='3i.txt')
open(unit=23,file='3o.txt')

do i = 1, n
read(24, *) (a(i,j), j = 1, n)
end do
read(24, *) (b(i), i = 1, n)
read(24, *) (x(i), i = 1, n)

write(23,'("itteration",6x,"x1",15x,"x2",15x,"x3"15x,"x4"15x,"x5"15x,"x6",/,104("_"))')
do k=1,max_i
x0=x
do i=1,n
ssum=0.0
do j=1,n
if(i<j) ssum=ssum+a(i,j)*x0(j)
if(i>j) ssum=ssum+a(i,j)*x(j)
end do
x(i)=(1-w)*x0(i)+(w)*(b(i)-ssum)/a(i,i)
end do
e=abs(x-x0)
write(23,'(5x,i0,6(6x,f10.6))')k,x
if(maxval(e)<t) exit
end do
write(23,'(//,"converged solution : ",6f10.6)')x
close(23)
close(24)
end program

Input: 3i.txt
4. -1. 0. 0. 0. 0.
-1. 4. -1. 0. 0. 0.
0. -1. 4. 0. 0. 0.
0. 0. 0. 4. -1. 0.
0. 0. 0. -1. 4. -1.
0. 0. 0. 0. -1. 4.
3. 2. 3. 3. 2. 3.
0. 0. 0. 0. 0. 0.

Output: 3o.txt
itteration x1 x2 x3 x4 x5
x6
________________________________________________________________________________________
________________
1 0.825000 0.776875 1.038641 0.825000 0.776875
1.038641
2 0.956141 1.020877 1.001877 0.956141 1.020877
1.001877
3 1.010127 1.001213 1.000146 1.010127 1.001213
1.000146
4 0.999321 0.999732 0.999912 0.999321 0.999732
0.999912
5 0.999994 1.000001 1.000009 0.999994 1.000001
1.000009
6 1.000001 1.000003 1.000000 1.000001 1.000003
1.000000

converged solution : 1.000001 1.000003 1.000000 1.000001 1.000003 1.000000

Answer to the question no. 4


Code: A6Q4.f90
program gram_schmidt
implicit none
integer::i,j
real:: u(3,3),v(3,3)
open(unit=43,file='4i.txt')
open(unit=32,file='4o.txt')

do j = 1, 3
read(43,*) u(:, j)
end do
write(32,'("the orthonormal vectors are :")')
do i=1,3
v(:,i)=u(:,i)
do j=1,i-1
v(:,i)=v(:,i)-(dot_product(u(:,i),v(:,j))/dot_product(v(:,j),v(:,j)))*v(:,j)
end do
v(:,i)= v(:,i)/sqrt(dot_product( v(:,i),v(:,i)))
write(32,'("w",i0,"=")')i
write(32,*), v(:,i)
end do
close(43)
close(32)
end program

Input: 4i.txt
1. 1. 1.
1. 1. 0.
1. 0. 0.

Output: 4o.txt
the orthonormal vectors are :
w1=
0.577350259 0.577350259 0.577350259
w2=
0.408248305 0.408248305 -0.816496611
w3=
0.707106769 -0.707106769 -1.40489496E-08

Answer to the question no. 5


Code: A6Q5.f90
program solvelinearsystem
implicit none
integer,parameter::n=3
real::a(n,n),b(n),l(n,n),u(n,n)
real::y(n),x(n)
real::a_orig(n,n),b_orig(n)
integer::i,j,k
open(unit=20,file='5o.txt')
open(unit=10,file='5i.txt')
! read a and b from input file
do i=1,n
read(10,*) a(i,:)
end do
read(10,*) b
close(10)

! store original a and b


a_orig=a
b_orig=b

! gaussian elimination
do k=1,n-1
do i=k+1,n
b(i)=b(i)-a(i,k)/a(k,k)*b(k)
a(i,:)=a(i,:)-a(i,k)/a(k,k)*a(k,:)
end do
end do

! back-substitution
x(n)=b(n)/a(n,n)
do i=n-1,1,-1
x(i)=(b(i)-sum(a(i,i+1:n)*x(i+1:n)))/a(i,i)
end do

! open output file

write(20,*) 'solution using gaussian elimination:'


write(20,'(3f10.5)') x

! reset a and b for lu decomposition


a=a_orig
b=b_orig

! initialize l and u
l=0.0
u=0.0
do i=1,n
l(i,i)=1.0
do j=i,n
u(i,j)=a(i,j)-sum(l(i,1:i-1)*u(1:i-1,j))
end do
do j=i+1,n
l(j,i)=(a(j,i)-sum(l(j,1:i-1)*u(1:i-1,i)))/u(i,i)
end do
end do

! solve ly=b
y(1)=b(1)
do i=2,n
y(i)=b(i)-sum(l(i,1:i-1)*y(1:i-1))
end do

! solve ux=y
x(n)=y(n)/u(n,n)
do i=n-1,1,-1
x(i)=(y(i)-sum(u(i,i+1:n)*x(i+1:n)))/u(i,i)
end do

write(20,*) 'solution using lu decomposition:'


write(20,'(3f10.5)') x
close(20)

end program solvelinearsystem

Input: 5i.txt
2.0 -1.0 1.0
2.0 2.0 2.0
-1.0 -1.0 2.0
-1.0 4.0 -5.0
Output: 5o.txt
solution using gaussian elimination:
1.00000 2.00000 -1.00000
solution using lu decomposition:
1.00000 2.00000 -1.00000

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