Advertisement
Guest User

Non-linear ODE FiniteDIfference algebraic iterative solution

a guest
Aug 11th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     !Discretization of du/dx + u^2 = 0 for 0<=x<=1, discretized to N-1 parts(so that x and u range from x_1 to x_N)
  2.     !=> (u_i - u_(i-1))/delX + u_i^2 = 0
  3.     !let u_i = u_gi + delu_i where, u_gi = guess for u_i and delu_i = error in guess
  4.     !so, u_i^2 = u_gi^2 + 2*delu_i*u_gi + delu_i^2. Neglecting delu_i^2 for being very small,
  5.     !u_i^2 = 2*u_gi*delu_i + u_gi^2 = 2*u_gi*(u_i - u_gi) + u_gi^2 = 2*u_i*u_gi - u_gi^2
  6.     !So, linearized(in u_i) finite difference equation:
  7.     !(2*delX*u_gi + 1)*u_i - u_(i-1) - delX*u_gi^2 = 0 ; i=2,3,4,..,N
  8.     !and u_1 = 1 (Boundary Condition)
  9.     !guess u_gi;i=1,2,...N and find u_i;i=1,2,...N as following:
  10.     !u_i = (u_(i-1) + delX*u_gi^2)/(1 + 2*delX*u_gi) ; i=2,3,...N
  11.     !update u_gi with the recently computed u_i and continue iterating till Sum(|u_i - u_gi|);i=1,2,...N is less than tolerance
  12.    
  13.     implicit none
  14.  
  15.     integer N,i,j
  16.     real delX
  17.     real U(50),U_guess(50),error(50)
  18.     real sumofErrors,tolerance
  19.    
  20.     tolerance=0.00001
  21.     N=4
  22.     delX=1/real(N-1)
  23.    
  24.     write(*,*) "N = ",N
  25.    
  26.     U_guess=1 !populate initial guess
  27.    
  28. 10  U(1)=1 !boundary condition: u_1=1, never changes
  29.     do i=2,N
  30.         U(i)=( U(i-1) + delX*U_guess(i)**2 ) / ( 1  +  2*delX*U_guess(i) )
  31.     end do
  32.    
  33.     error=abs(U-U_guess) !populate error vector
  34.    
  35.     sumofErrors=sum(error)
  36.    
  37.     write(*,*) "U guess = ",(U_guess(i),i=1,N)
  38.     write(*,*) "U       = ",(U(i),i=1,N)
  39.     write(*,*)
  40.    
  41.     U_guess=U !update guess vector with current output
  42.    
  43.     if(sumofErrors.GT.tolerance) then
  44.         goto 10
  45.     endif
  46.    
  47.     open(5,file="output.txt")
  48.     do i=1,N
  49.         write(5,*) U(i)
  50.     end do
  51.     close(5)
  52.    
  53. end program
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement