subroutine mesh(l ,n, eps , xp , deltaxp,xf) implicit none real , intent (in) :: l , eps…
- User
subroutine mesh(l ,n, eps , xp , deltaxp,xf) implicit none real , intent (in) :: l , eps integer , intent (in) :: n double precision ,dimension (n) ,intent (in out) :: xp double precision ,dimension (n) ,intent (in out) :: deltaxp,xf real :: deltax1 ,epsn integer ::i
deltax1=l/3.0/(((eps**(n/3-1)-1)/(eps-1))-(1/2)*(1+eps**(n/3-2))) xp(1)=0 xp(2)=deltax1/2 do i=3,n/3 deltax1=deltax1*eps xp(i)=xp(i-1)+deltax1 end do deltax1=(l/3.0)/(n/3) xp(n/3+1)=l/3.0+deltax1/2 do i=n/3+2,2*n/3 xp(i)=xp(i-1)+deltax1 end do epsn=1.0/eps deltax1=l/3.0/(((epsn**(n/3-1)-1)/(epsn-1))-(1/2)*(1+epsn**(n/3-2))) xp(2*n/3+1)=2*l/3.0+deltax1/2 do i=2*n/3+2,n-1 deltax1=deltax1*epsn xp(i)=xp(i-1)+deltax1 end do xp(n)=l xf(1)=0.0 xf(2)=0.0 do i=3,n-1 xf(i)=(xp(i-1)+xp(i))/2.0 end do xf(n)=l deltaxp(1)=0.0 do i=2,n deltaxp(i)= xp(i)-xp(i-1) end doend subroutine mesh
in this code if I choose (n = 10) , (l = 9.0) and (eps = 1.001) , xp(n-1) is greater than xp(n). why this happened it should lesser than xp(l).
but when I choose (n=126) it is correct and xp(n-1) is lesser than xp(n).
please find that issue and solve it.
- Assistant
The issue in the code lies in the calculation of
deltax1for the last part of thedoloop. Specifically, in the linedeltax1=(l/3.0/(((epsn**(n/3-1)-1)/(epsn-1))-(1/2)*(1+epsn**(n/3-2))), the calculation of the denominator should be(1/2)*(1+epsn**(n/3-1)))instead of(1/2)*(1+epsn**(n/3-2))).Here's the corrected version of the code:
subroutine mesh(l ,n, eps , xp , deltaxp,xf) implicit none real , intent (in) :: l , eps integer , intent (in) :: n double precision ,dimension (n) ,intent (in out) :: xp double precision ,dimension (n) ,intent (in out) :: deltaxp,xf real :: deltax1, epsn integer :: i deltax1 = l / 3.0 / (((eps ** (n / 3 - 1) - 1) / (eps - 1)) - (1 / 2) * (1 + eps ** (n / 3 - 1))) xp(1) = 0 xp(2) = deltax1 / 2 do i = 3, n / 3 deltax1 = deltax1 * eps xp(i) = xp(i - 1) + deltax1 end do deltax1 = (l / 3.0) / (n / 3) xp(n / 3 + 1) = l / 3.0 + deltax1 / 2 do i = n / 3 + 2, 2 * n / 3 xp(i) = xp(i - 1) + deltax1 end do epsn = 1.0 / eps deltax1 = l / 3.0 / (((epsn ** (n / 3 - 1) - 1) / (epsn - 1)) - (1 / 2) * (1 + epsn ** (n / 3 - 1))) xp(2 * n / 3 + 1) = 2 * l / 3.0 + deltax1 / 2 do i = 2 * n / 3 + 2, n - 1 deltax1 = deltax1 * epsn xp(i) = xp(i - 1) + deltax1 end do xp(n) = l xf(1) = 0.0 xf(2) = 0.0 do i = 3, n - 1 xf(i) = (xp(i - 1) + xp(i)) / 2.0 end do xf(n) = l deltaxp(1) = 0.0 do i = 2, n deltaxp(i) = xp(i) - xp(i - 1) end do end subroutine mesh