Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1. x'' = p(t)x'(t)+q(t)x(t)+r(t)
  2. x(b) = beta in [a,b]
  3.  
  4. program test_linsht
  5. !
  6. ! Linear Shooting method
  7. ! To approximate the solution of boundary value problem
  8. ! x'' = p(t)x'(t)+q(t)x(t)+r(t)
  9. !' x(b) = beta in [a,b]
  10. ! and using Runge-Kutta method of order N = 4
  11. !
  12. !
  13. !
  14. implicit none
  15. integer,parameter :: n = 2
  16. integer,parameter :: nstep = 20
  17. real(8) :: U(nstep) ! U contains the solutions at each step
  18. real(8) :: V(nstep),out(nstep) ! V just like U for another equation
  19. real(8) :: a,b,h,alph,beta,w
  20. real(8) :: x(0:n)
  21. integer :: i
  22. a = 0.d0
  23. b = 4.0
  24. alph = 125.d-2
  25. beta = -95.d-2
  26. h = (b-a)/nstep
  27. ! x = t0 x0 x'0
  28. x = (/0.d0, alph, 0.d0/)
  29.  
  30. call rks4U(n,h,x,nstep,U)
  31. a = 0.d0
  32. b = 4.0
  33. x = (/0.d0, 0.d0, 1.d0 /)
  34. h = (b-a)/nstep
  35.  
  36. call rks4V(n,h,x,nstep,V)
  37.  
  38. ! calculate the solution to the boundary value broblem
  39. w = (beta - U(nstep))/V(nstep)
  40. write (*,*) w,U(nstep),V(nstep)
  41. !w = 0.485884
  42. out = U + w * V
  43. do i = 1,nstep
  44. write(*,100) i,i*h,U(i), w*V(i), out(i)
  45. enddo
  46. 100 format (1x,I5,f10.2,f15.6,f15.6,f15.6)
  47. end program
  48.  
  49. subroutine xprsysU(n,x,f)
  50. ! x prime of system of equations
  51.  
  52. real(8), dimension(0:n) :: x,f
  53. integer :: n
  54. ! time is introduced as a new differential equation
  55. f(0) = 1.d0
  56. f(1) = x(2)
  57. f(2) = 2.*x(0)/(1+x(0)*x(0))*x(2)- 2./(1.+x(0)*x(0))*x(1) + 1.0
  58. end subroutine
  59.  
  60. subroutine xprsysV(n,x,f)
  61. ! x prime of system of equations
  62.  
  63. real(8), dimension(0:n) :: x,f
  64. integer :: n
  65. ! time is introduced as a new differential equation
  66. f(0) = 1.d0
  67. f(1) = x(2)
  68. f(2) = 2.*x(0)/(1+x(0)*x(0))*x(2)- 2./(1.+x(0)*x(0))*x(1)
  69. end subroutine
  70.  
  71.  
  72.  
  73. subroutine rks4U(n,h,x,nstep,xout)
  74.  
  75. implicit none
  76. real(8) :: x(0:n)
  77. real(8) :: y(0:n), f(0:n,4)
  78. real(8) :: xout(nstep)
  79. integer :: i,k,n,nstep
  80. real(8) :: h
  81. !write(*,200) "k","t","x","y"
  82. !write(*,100) 0,x
  83. f = 0.
  84. out: do k = 1,nstep
  85. call xprsysU(n,x,f(0,1))
  86. in1: do i = 0,n
  87. y(i) = x(i) + 0.5*h*f(i,1)
  88. !print *, f(i,1)
  89. end do in1
  90. call xprsysU(n,y,f(0:n,2))
  91. in2: do i = 0,n
  92. y(i) = x(i) + 0.5*h*f(i,2)
  93. end do in2
  94. call xprsysU(n,y,f(0:n,3))
  95. in3: do i = 0,n
  96. y(i) = x(i) + h*f(i,3)
  97. end do in3
  98. call xprsysU(n,y,f(0:n,4))
  99. in4: do i = 0,n
  100. x(i) = x(i) + (h/6.0)* (f(i,1) + 2.0*(f(i,2) + f(i,3)) + f(i,4))
  101. end do in4
  102. xout(k) = x(1)
  103. end do out
  104. !100 format (1x,I5,f10.2,f15.8,f15.8)
  105. !200 format (1x,A4,2A10,A16)
  106. end subroutine rks4U
  107.  
  108.  
  109. subroutine rks4V(n,h,x,nstep,xout)
  110.  
  111. implicit none
  112. real(8) :: x(0:n)
  113. real(8) :: y(0:n), f(0:n,4)
  114. real(8) :: xout(nstep)
  115. integer :: i,k,n,nstep
  116. real(8) :: h
  117. !write(*,200) "k","t","x","y"
  118. !write(*,100) 0,x
  119. f = 0.
  120. out: do k = 1,nstep
  121. call xprsysV(n,x,f(0,1))
  122. in1: do i = 0,n
  123. y(i) = x(i) + 0.5*h*f(i,1)
  124. !print *, f(i,1)
  125. end do in1
  126. call xprsysV(n,y,f(0:n,2))
  127. in2: do i = 0,n
  128. y(i) = x(i) + 0.5*h*f(i,2)
  129. end do in2
  130. call xprsysV(n,y,f(0:n,3))
  131. in3: do i = 0,n
  132. y(i) = x(i) + h*f(i,3)
  133. end do in3
  134. call xprsysV(n,y,f(0:n,4))
  135. in4: do i = 0,n
  136. x(i) = x(i) + (h/6.0)* (f(i,1) + 2.0*(f(i,2) + f(i,3)) + f(i,4))
  137. end do in4
  138. xout(k) = x(1)
  139. end do out
  140. !100 format (1x,I5,f10.2,f15.8,f15.8)
  141. !200 format (1x,A4,2A10,A16)
  142. end subroutine rks4V
  143.  
  144. program test_linsht
  145. implicit none
  146. ...
  147. call rk4(n, h, x, nstep, xprsysU, U)
  148. call rk4(n, h, x, nstep, xprsysV, V)
  149. ...
  150. contains
  151.  
  152. subroutine xprsysU(n,x,f)
  153. ! x prime of system of equations
  154. real(8), dimension(0:n) :: x,f
  155. integer :: n
  156. ! time is introduced as a new differential equation
  157. f(0) = 1.d0
  158. f(1) = x(2)
  159. f(2) = 2.*x(0)/(1+x(0)*x(0))*x(2)- 2./(1.+x(0)*x(0))*x(1) + 1.0
  160. end subroutine
  161.  
  162. subroutine rk4(n, h, x, nstep, sub, xout)
  163. real(8) :: x(0:n)
  164. real(8) :: y(0:n), f(0:n,4)
  165. interface
  166. subroutine sub(n, x, f)
  167. real(8), dimension(0:n) :: x,f
  168. integer :: n
  169. end subroutine sub
  170. end interface
  171. ...
  172. end subroutine
  173. end program test_linsht
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement