Welberg123
Programmer
Hi,
I'm trying to write a program in Fortran77 to solve the telegraph's equation using Finite Differences, but there seems to be a problem somewhere in the code. This is what I got so far:
Explanation of the idea
Code:
As you can see, the result is nonsensical (I guess due to some mistake in the iteration), if someone can pinpoint where have I gone wrong, it would be of great help.
Thanks
I'm trying to write a program in Fortran77 to solve the telegraph's equation using Finite Differences, but there seems to be a problem somewhere in the code. This is what I got so far:
Explanation of the idea
Original equation:
d2V(x,t)/dt2 + 2b·dV(x,t)/dt - v2·d2V(x,t)/dx2 = 0
where b-1 is a time parameter and v the propagation speed. Now, to make the equation user-friendly, I used V(x,t)=u(x,t)·e(-bt), and the variables τ=bt and z=bx/v, then I get:
d2u(z,τ)/dτ2 - u(z,τ) - d2u(z,τ)/dz2= 0
And translating into Finite Differences: un+1j= [2+(Δτ)2-2α]unj+α(unj+1+unj-1)-un-1j
where α=((Δτ)2)/((Δz)2). The idea is to solve this equation with the Initial condition V(z,τ=0)=C1·e(-C2·z2)
d2V(x,t)/dt2 + 2b·dV(x,t)/dt - v2·d2V(x,t)/dx2 = 0
where b-1 is a time parameter and v the propagation speed. Now, to make the equation user-friendly, I used V(x,t)=u(x,t)·e(-bt), and the variables τ=bt and z=bx/v, then I get:
d2u(z,τ)/dτ2 - u(z,τ) - d2u(z,τ)/dz2= 0
And translating into Finite Differences: un+1j= [2+(Δτ)2-2α]unj+α(unj+1+unj-1)-un-1j
where α=((Δτ)2)/((Δz)2). The idea is to solve this equation with the Initial condition V(z,τ=0)=C1·e(-C2·z2)
Code:
Code:
program Telegraph
integer*4 Nspatial, Ntemporal, Npoints, i,j
real*8 dx, dt, p, L, T(1001,10000), u(1001,10000), c1, alpha,c3,c2
open(unit=1, file="Telegraph.txt")
! Variables:
Ntemporal= 500
dx=0.001
dt = 0.001
Npoints=100
alpha=(dt*dt)/(dx*dx)
C1=0.7283656
C2=5.5555555
C3=(2.0d0+2.0d0*dt-2.0d0*alpha)
!!!!! Main program:
write(1,*) "---------------------------------------------------"
write(1,*) " u(i,j) || time || position "
write(1,*) "---------------------------------------------------"
! Initial conditions:
do i=1, Npoints-1
u(i,1)=C1*exp(-C2*(i**2.0d0))
enddo
! Iteration:
do j=3, Ntemporal
do i=2, Npoints
u(i,j)=c3*u(i,j-1)+alfa*(u(i+1,j-1)+u(i-1,j-1))-u(i,j-2)
write(1,*) u(i,j), i, j
end do
end do
write(*,*) "----------------------------------------"
write(*,*) "u(i,j) saved at Telegraph.txt"
write(*,*) "----------------------------------------"
end
As you can see, the result is nonsensical (I guess due to some mistake in the iteration), if someone can pinpoint where have I gone wrong, it would be of great help.
Thanks