Jeff Faraci
Jeff Faraci

Reputation: 413

Difference of finite difference solution between two timesteps

I have a solution to a discretized differential equation given by

f(i)  

where i is a spatial index. How can I find the difference between the solution at each adjacent time step? To be more clear:

The solution is defined by an array

real,dimension(0:10) :: f

I discretize the differential equation and solve it by stepping forward in time. If the time index is k, a portion of my code is

do k=1,25
   do i = 1,10
      f(i) = f(i+1)+f(i-1)+f(i)
   end do
end do

I can print the solution, f(i) at each time step k by the following code

print*, "print f(i) for k=, k
print "(//(5(5x,e22.14)))", f

How can I find the difference between the solution at each adjacent time step? That is, time steps k+1,k. I will store this value in a new array g, which has a dimension given by

real,dimension(0:10) :: g  

So I am trying to find

!g(i)=abs(f(i;k+1)-f(i;k))...Not correct code.

How can I do this? What is the way to implement this code? I am not sure how to do this using if /then statements or whatever code would need be needed to do this. Thanks

Upvotes: 0

Views: 94

Answers (2)

Typically, in explicit time integration methods or iterative methods, you have to save the last time-step last solution, the current time-step solution and possibly even some more.

So you have

  real,dimension(0:10) :: f0, f

where f0 is the previous value

You iterate your Jacobi or Gauss-Seidel discretization:

f = f0

do k=1,25

   do i = 1,9
      f(i) = f(i+1)+f(i-1)+f(i)
   end do

   max_diff = maxval(abs(f-f0))

   if (diff small enough) exit

   f0 = f 
end do

If you have a time-evolving problem like a heat equation:

f = f0

do k=1,25

   do i = 1,9
      f(i) = f0(i) + dt * viscosity * (f0(i+1)+f0(i-1)+f0(i))
   end do

   max_diff = maxval(abs(f-f0))

   f0 = f 
end do

Upvotes: 1

duffymo
duffymo

Reputation: 308998

You have a spatial mesh at each point time. Transient problems require that you calculate the value at the end of a time step based on the values at the start:

f(i, j+1) = f(i, j) + f(dot)(i, j)*dt  // Euler integration where f(dot) = df/dt derivative

i is the spatial index; j is the temporal one.

Upvotes: 0

Related Questions