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:
d[sup]2[/sup]V(x,t)/dt[sup]2[/sup] + 2b·dV(x,t)/dt - v[sup]2[/sup]·d[sup]2[/sup]V(x,t)/dx[sup]2[/sup] = 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[sup](-bt)[/sup], and the variables τ=bt and z=bx/v, then I get:
d[sup]2[/sup]u(z,τ)/dτ[sup]2[/sup] - u(z,τ) - d[sup]2[/sup]u(z,τ)/dz[sup]2[/sup]= 0
And translating into Finite Differences: u[sup]n+1[/sup][sub]j[/sub]= [2+(Δτ)[sup]2[/sup]-2α]u[sup]n[/sup][sub]j[/sub]+α(u[sup]n[/sup][sub]j+1[/sub]+u[sup]n[/sup][sub]j-1[/sub])-u[sup]n-1[/sup][sub]j[/sub]
where α=((Δτ)[sup]2[/sup])/((Δz)[sup]2[/sup]). The idea is to solve this equation with the Initial condition V(z,τ=0)=C1·e[sup](-C2·z[sup]2[/sup])[/sup]
d[sup]2[/sup]V(x,t)/dt[sup]2[/sup] + 2b·dV(x,t)/dt - v[sup]2[/sup]·d[sup]2[/sup]V(x,t)/dx[sup]2[/sup] = 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[sup](-bt)[/sup], and the variables τ=bt and z=bx/v, then I get:
d[sup]2[/sup]u(z,τ)/dτ[sup]2[/sup] - u(z,τ) - d[sup]2[/sup]u(z,τ)/dz[sup]2[/sup]= 0
And translating into Finite Differences: u[sup]n+1[/sup][sub]j[/sub]= [2+(Δτ)[sup]2[/sup]-2α]u[sup]n[/sup][sub]j[/sub]+α(u[sup]n[/sup][sub]j+1[/sub]+u[sup]n[/sup][sub]j-1[/sub])-u[sup]n-1[/sup][sub]j[/sub]
where α=((Δτ)[sup]2[/sup])/((Δz)[sup]2[/sup]). The idea is to solve this equation with the Initial condition V(z,τ=0)=C1·e[sup](-C2·z[sup]2[/sup])[/sup]
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