## Finite Differences

## Finite Differences

(OP)

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

d

where b-1 is a time parameter and v the propagation speed. Now, to make the equation user-friendly, I used

d

where α=((Δτ)

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

#### Spoiler:

**Original equation**:

d

^{2}V(x,t)/dt

^{2}+ 2b·dV(x,t)/dt - v

^{2}·d

^{2}V(x,t)/dx

^{2}= 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**, and the variables τ=bt and z=bx/v, then I get:

^{(-bt)}d

^{2}u(z,τ)/dτ

^{2}- u(z,τ) - d

^{2}u(z,τ)/dz

^{2}= 0

**And translating into Finite Differences**: u

^{n+1}

_{j}= [2+(Δτ)

^{2}-2α]u

^{n}

_{j}+α(u

^{n}

_{j+1}+u

^{n}

_{j-1})-u

^{n-1}

_{j}

where α=((Δτ)

^{2})/((Δz)

^{2}). The idea is to solve this equation with the Initial condition V(z,τ=0)=C1·e

^{(-C2·z2)}

Code:

#### Spoiler:

#### CODE --> fortran77

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

## RE: Finite Differences

=> you need to initialize u for n=1 AND for n=2 !

Frantois Jacq

## RE: Finite Differences

do j=1, 2

u(i,j)=u(i,-j)

enddo

but still doesn't work. Could you please be more specific? Thanks again

## RE: Finite Differences

u(i,j)=u(i,-j)

enddo

But you cannot write that ! What means u(i,-j) ????

You could try :

## CODE --> fortran

Frantois Jacq

## RE: Finite Differences

## RE: Finite Differences

## RE: Finite Differences

I guess its a question of stability. Haven't looked into the physics, but dx=dt may causes problems.

try a bigger dx and a smaller dt.

greetings from austria

## RE: Finite Differences

whats this line:

## CODE --> fortran

alfa=alpha?

## RE: Finite Differences

## RE: Finite Differences

## CODE --> fortran77

I'm pretty sure the set up now is the right one, but I get the famous warning message: "Warning: Array reference at (1) is out of bounds (0 < 1) in dimension 2", where (1) is u(i,0). Any ideas as how to solve this?

## RE: Finite Differences

## RE: Finite Differences