Skip to main content

Posts

Iterative method for a nonlinear equation

Introduction: Program: ! Iterative method for a nonlinear equation real::x1,x2,check x1=-1.5 check=x1 20 check=check+1.0 x1=check 19 x2=f(x1) x1=x2 !write(*,*)check if(abs(f(x2)-x2)<=0.001)then write(*,*)"Initial choice",check+1.0 write(*,*)"The solution is" write(*,*)x1 else goto 19 endif if(x1<10.0)goto 20 end function f(q) real::q f=(q**2+6.0)/5.0 return end function Output: Summary:

gauss seidel

!PROGRAM gauss seidel integer :: i,j,k,n,no_of_ite real :: a(5,5),b(5),x(5),y(5),s,ep open(15,file="input.dat") open(16,file="output10") read(15,*)n read(15,*)no_of_ite read(15,*)ep do i=1,n read(15,*)(a(i,j),j=1,n) enddo read(15,*)(b(j),j=1,n) write(16,*)"a=" do i=1,n write(16,*)(a(i,j),j=1,n) enddo write(16,*)"b=" write(16,*)(b(j),j=1,n) do i=1,n x(i)=0.0 y(i)=0.0 enddo do k=1,no_of_ite do i=1,n s=0.0 do j=1,n if(i/=j)then s=s+a(i,j)*x(j) endif enddo x(i)=(b(i)-s)/a(i,i) enddo if((abs(x(i))-abs(y(i)))<ep)exit do i=1,n y(i)=x(i) enddo write(16,*)"no of ite=",k," x= ",(x(j),j=1,n) write(16,*)" " enddo write(16,*)"x=" write(16,*)(x(j),j=1,n) end

gauss Jocobi

!PROGRAM gauss Jocobi integer :: i,j,k,n,no_of_ite real :: a(5,5),b(5),x(5),y(5),s,ep open(15,file="input.dat") open(16,file="output10") read(15,*)n read(15,*)no_of_ite read(15,*)ep do i=1,n read(15,*)(a(i,j),j=1,n) enddo read(15,*)(b(j),j=1,n) write(16,*)"a=" do i=1,n write(16,*)(a(i,j),j=1,n) enddo write(16,*)"b=" write(16,*)(b(j),j=1,n) do i=1,n x(i)=0.0 y(i)=0.0 enddo do k=1,no_of_ite do i=1,n s=0.0 do j=1,n if(i/=j)then s=s+a(i,j)*y(j) endif enddo x(i)=(b(i)-s)/a(i,i) enddo if((abs(x(i))-abs(y(i)))<ep)exit do i=1,n y(i)=x(i) enddo write(16,*)"no of ite=",k," x= ",(x(j),j=1,n) write(16,*)" " enddo write(16,*)"x=" write(16,*)(x(j),j=1,n) end

Tridiagonal

PROGRAM Tridiagonal !The number of variable or number of equations is n. !The coefficient matrix=a(ij), i, j=1,2,3,...,n. !The lower triangular matrix=l(ij), i, j=1,2,3,...,n. !The upper matrix=u(ij), i, j=1,2,3,...,n. !b(i), i=1,2,3,...,n, the constants of the of the equations. !z(i), i=1,2,3,...,n. !x(i), i=1,2,3,...,n. integer::n,i,j,k,m real:: a(5),b(5),c(5),p(5),q(5),r(5),x(5),z(5),d(5) open(9,file="input.dat") open(10,file="output10") read(9,*)n read(9,*)(a(j),j=1,n) read(9,*)(b(j),j=1,n-1) read(9,*)(c(j),j=2,n) read(9,*)(d(j),j=1,n) write(10,*)"a="," ",(a(j),j=1,n) write(10,*)" " write(10,*)"b="," ",(b(j),j=1,n-1) write(10,*)" " write(10,*)"c="," ",(c(j),j=2,n) write(10,*)" " write(10,*)"d="," ",(d(j),j=1,n) r(1)=a(1) do i=2,n q(i)=c(i) p(i-1)=b(i-1)/r(i-1) r(i)=a(i)-p(i-1)*q(i) write(10,*)"i=",i,"p,q,r=",p(i),q(i),r(i) enddo writ...

LU-Decomposition

! PROGRAM LU-Decomposition !The number of variable or number of equations is n. !The coefficient matrix=a(ij), i, j=1,2,3,...,n. !The lower triangular matrix=l(ij), i, j=1,2,3,...,n. !The upper matrix=u(ij), i, j=1,2,3,...,n. !b(i), i=1,2,3,...,n, the constants of the of the equations. !z(i), i=1,2,3,...,n. !x(i), i=1,2,3,...,n. integer::n,i,j,k,m real:: a(5,5),l(5,5),u(5,5),b(5),x(5),z(5),ratio,s open(3,file="input.dat") open(4,file="output2") read(3,*)n do i=1,n read(3,*)(a(i,j),j=1,n) enddo write(4,*) "A=" do i=1,n write(4,*)(a(i,j),j=1,n) enddo write(4,*)" " read(3,*)(b(j),j=1,n) write(4,*)"B=",(b(j),j=1,n) write(4,*)" " do i=1,n l(i,1)=a(i,1) u(i,i)=1.0 enddo do j=2,n u(1,j)=a(1,j)/l(1,1) enddo do k=2,n !To determine columns of l do i=k,n s=0 do j=1,i-1 s=s+l(i,j)*u(j,k) enddo l(i,k)=a(i,k)-s enddo !!To determine rows of u do i=k+1,n if(i/=k)then s=0 do j=1,i-1 s=s+l(k,j)*u(j,i) enddo endif u(k,i)=(a(k,i)-s)/l(k,k) enddo...

Gauss Jordan

PROGRAM Gauss_Jordan ! Gauss Jordan method to find the solution of the system of linear equations ! The number of equations or the number of variables of the system ! of linear equations is n ! The coefficient matrix =a(ij) : i, j=1,2,3,...,n ! The constant vector =b(i) : i=1,2,3,...,n ! The vector of the variables =x(i) : i=1,2,3,...,n integer::n,i,j,k,m real:: a(5,5),b(5),x(5),r open(3,file="input.dat") open(4,file="output2") read(3,*)n do i=1,n read(3,*)(a(i,j),j=1,n) enddo read(3,*)(b(j),j=1,n) write(4,*) "a=" do i=1,n write(4,*)(a(i,j),j=1,n) enddo write(4,*)" " write(4,*)"b=",(b(j),j=1,n) do k=1,n do i=1,n if(i/=k)then r=a(i,k)/a(k,k) do j=k,n a(i,j+1)=a(i,j+1)-a(k,j+1)*r enddo b(i)=b(i)-b(k)*r endif enddo enddo do i=1,n x(i)=b(i)/a(i,i) enddo write(4,*)" " write(4,*)"The solution set is ","",(x(j),j=1,n),"" end PROGRAM

Gauss Elimination

PROGRAM Gauss_Elimination ! n is number of variables or number of equations ! a[ij], i, j=1,2,3,...,n is coefficient matrix, b[i], i=1,2,3,...,n is constant ! vector, x[i], i=1,2,3...,n is the vector of variables integer::n,i,j,k real:: a(5,5),b(5),x(5),ratio,s open(1,file="input.dat") open(2,file="output.dat") read(1,*)n write(2,*)"The number of variables or number of equations, n=",n do i=1,n read(1,*)(a(i,j),j=1,n) enddo write(2,*)"The cefficient matrix, A=" do i=1,n write(2,*)(a(i,j),j=1,n) enddo do j=1,n read(1,*)b(j) enddo write(2,*)"The constant vector, B=" write(2,*)(b(j),j=1,n) do k=1,n-1 do i=k+1,n ratio=a(i,k)/a(k,k) do j=k,n a(i,j)=a(i,j)-ratio*a(k,j) enddo b(i)=b(i)-ratio*b(k) enddo enddo write(2,*) do i=1,n write(2,*)(a(i,j),j=1,n) enddo x(n)=b(n)/a(n,n) do i=n-1,1,-1 s=0.0 do j=n,i+1,-1 s=s+a(i,j)*x(j) enddo x(i)=(b(i)-s)/a(i,i) enddo write(2,*) write(2,*)(x(j),j=1,n) end PROGRAM