3

I am attempting to code a problem for a meteorology class. Our initial equation was as follows: $$\tfrac{\partial u}{\partial t} = \nu \tfrac{\partial^2 u}{\partial x^2} (*)$$ We were then assigned this finite differencing to program: $$\tfrac{\zeta_{j}^{n+1}-\zeta_{j}^{n-1}} {2\Delta t} = \nu \tfrac{\zeta_{j+1}^{n}-\zeta_{j}^{n+1}-\zeta_{j}^{n-1}+\zeta_{j-1}^{n}}{\Delta x^{2}} (\&)$$
n and j denote time and space respectively.

The problem reads as follows: "Graph (for t = 10000s) one wavelength of the discrete solutions for k=2 [i.e., interval: -$\pi /4$, 3$\pi /4$] and k=4 [i.e., interval: -$\pi /8$, 3$\pi /8$] assuming $\zeta_{0}^{n}=0,\zeta_{j}^{n}=0,\zeta_{j}^{1}=\zeta_{j}^{0}$ for j = 1,......,nx-1, and $\Delta x = L/(nx-1)$, where $L=2\pi /k$. Use $\Delta t = 10s, 1000s, 2000s$, and $5000s.$ How does your analytic solution from solving (*) above compare to the discrete solution a t = 10000s add the analytic solution for t = 10000s to your graph)? Note that there should be two graphs with 5 curves here! What happens to your discrete solution as $\Delta t$ increases?"

I have never seen such a differencing as in the RHS of (&). The LHS is clearly central differencing, yet the right hand side is something I unfamiliar with. It is clearly not forward space, central space, or back space. I am supposed to somehow rearrange (&) to solve for $\zeta_{j}^{n+1}$ and then program the above problem. I do not see how I am supposed to do this, either mathematically or analytically as I do not know of any such differencing methods, and I have spent over 6 hours trying to answer this question.

My apologies if this belongs in the coding forum as I was uncertain whether to post this in math or coding. I am completely unaware of any ways to simultaneously go through two different loops in code, and I have never seen this kind of differencing that combines time and space.

Alex
  • 33
  • There's a bit of information you could provide that would help one answer this question. How do these sub-intervals and wavelengths arise? Do you have a specific value of $\nu$? What is $k$? Perhaps, it's the same as $\nu$? What do you mean by $t=10000$s? Perhaps, the number of timesteps? Your condition $\zeta_{j}^{n}=0$ doesn't look right - surely, you don't mean to set your function to zero for all time and space. Perhaps, that should be an initial and/or right boundary condition. What tool or programing language are you working with? – Mark McClure Feb 08 '15 at 16:00
  • I found the values of $\nu$ and $k$ to not be so important in this scenario as they are merely constants that have no bearing on the solution. Now as for $\zeta_{j}^{n}=0$, I assure you that that is indeed correct. I interpreted it the same way, so I am also confused. I am attempting to code this in R. – Alex Feb 08 '15 at 16:05
  • Are you sure it is not supposed to be $\zeta^n_{j_{\rm max}}=0$? This means that we impose the boundary condition $\zeta(t,x=x_{\rm max}) = 0$ (just as $\zeta^n_0$ is the boundary condition $\zeta(t,x=x_{\rm min}) = 0$). This is the only sense I can make of it. – Winther Feb 08 '15 at 16:35
  • Agreed. I am merely conveying the information given to me. I almost wonder if the formulation is incorrect. – Alex Feb 08 '15 at 16:49

3 Answers3

2

I will give some pointers on how to solve the system numerically.

For simplicity set $\alpha = \left(\frac{\Delta x^2}{2\nu\Delta t}\right)$ and then solve the equation for $\zeta^{n+1}_j$ to find

$$\zeta_{j}^{n+1} = \frac{\zeta_{j+1}^{n}+\zeta_{j-1}^{n}}{1+\alpha} + \frac{\alpha-1}{\alpha + 1}\zeta_{j}^{n-1}$$

This equation tells us that if we know, for any fixed $n$, $\zeta_j^n$ and $\zeta_j^{n-1}$ for all $j$ then the equation above can be used to calculate $\zeta_{j}^{n+1}$ for all $j$.

A simple way to codes this up (there are better memory-saving ways to do this, but this should be good enough here) is to first make an array $\zeta[0:n_t,0:n_x]$ where $n_t$ is the number of time-steps and $n_x$ is the number of grid-cells.

Next we need to input the initial condition $\zeta^1_j = \zeta^0_j = \ldots$ in the entries $\zeta[0,0:n_x]$ and $\zeta[1,0:n_x]$.

Finally we need to propagate the equation to get the solution at all times. This can be best explained by the following pseudo-code

// Loop over time-steps

Loop over $n = 2,3,4,\ldots,n_t$

// Set the boundary condition $\zeta_0^n = \zeta_{n_x}^n = 0$

<p>$\zeta[n,0] = 0$</p>

<p>$\zeta[n,n_x] = 0$</p>

<p>// Calculate the solution for all $j$</p>

<p>Loop over $j=1,2,\ldots, n_x-1$</p>

<blockquote>
  <p>$\zeta[n,j] = \frac{\zeta[n,j+1]+\zeta[n,j-1]}{1+\alpha} + \frac{\alpha-1}{\alpha + 1}\zeta[n-1,j]$</p>
</blockquote>

After this is done you have the solution $\zeta(t,x)$ at the points $(t,x) = \left(n\Delta t, x_{\rm min} + \frac{(x_{\rm max}-x_{\rm min})j}{n_x}\right)$ stored in $\zeta[n,j]$.

Another thing to keep in mind when solving is that a stability analysis give that the numerical scheme is only stable if $\alpha \geq 1$. So if you get nonsensical solutions then I would check if this condition is satisfied for your choice of $\Delta x$, $\Delta t$.

Winther
  • 24,478
  • I have already asked my professor, and he assured me that the solutions in this particular case are indeed stable. Would your matrix loop method result in $\zeta(t,x) = f(t,x)$? Or would I merely get a numerical result? – Alex Feb 08 '15 at 16:10
  • @Alex What do you mean by 'would I merely get a numerical result'? What is $f(t,x)$? This method will give you the numerical solution for $\zeta(t,x)$ at every point of your grid and every time $t = n\Delta t$. – Winther Feb 08 '15 at 16:20
  • According to my professor, I need this to result in a graph of $\zeta$ vs. $x$ – Alex Feb 08 '15 at 16:25
  • @Alex See my last point "After this is done you have...". You will from this be able to plot $\zeta(x,t)$ as a function of $x$ for any $t = n\Delta t$. – Winther Feb 08 '15 at 16:27
  • It will require some careful looping, but I believe that I shall give this a try. – Alex Feb 08 '15 at 16:48
  • Good luck! The thing to be extra careful about is the end-points. Make sure the $j$-loop does not extend over the end-points as then you will be trying to use points $\zeta[n,j+1]$ that are out of bounds (do not exist). – Winther Feb 08 '15 at 16:51
2

I find it useful to visualize a computational stencil for the algorithm. There are several of these listed on the Wikipedia page for the finite difference method, though, not yours. Yours can be visualized like so:

enter image description here

The dots all represent points in a grid where you want to compute an estimate for your solution. The large dots represent values known at the outset from either an initial or boundary condition. The small dots represent points where we plan to compute the value. At any step, the green disks will represent known values, while the red disk represents the value you will compute, in terms of the known green values using the formula presented by Withner: $$\zeta_{j}^{n+1} = \frac{\zeta_{j+1}^{n}+\zeta_{j-1}^{n}}{1+\alpha} + \frac{\alpha-1}{\alpha + 1}\zeta_{j}^{n-1}.$$ Your loop will essentially move the stencil so that the red disk traces out the row of small dots near the bottom. You'll then increment $j$ to move the stencil up to the next row and repeat.

Mark McClure
  • 30,510
0

what is on the right hand side is the second derivative approximation of $u$ with respect to $x.$ to see this in one variable case compute $f(1+x) - 2f(1) + f(1-x)$ using taylor series.

$$f(1+h) = f(1) + hf'(1) + \frac{1}{2}h^2f''(1) + \cdots, f(1-h) = f(1) - hf'(1) + \frac{1}{2}h^2f''(1) + \cdots $$

adding the two you get $$f(1+h) + f(1-h) = 2f(1) + h^2f''(1) + \cdots $$

approximating this gets you $f''(1) \simeq \dfrac{f(1+h) - 2f(1) + f(1-h)}{h^2}$

abel
  • 29,170
  • 1
    Close. You've really got a function $u(x,y)$ of two variables and a difference quotient that looks like $$u_{xx}\approx \frac{u(x+h,t)-u(x,t+k)-u(x,t-k)+u(x-h,t)}{h^2}.$$ Of course, $t$ is constant and $k\rightarrow0$, so the analysis works out almost identically with a little more bookkeeping. – Mark McClure Feb 08 '15 at 14:46
  • I'm guessing OP knows the formula presented here. The question as I see it is why suddenly the $u(\cdot, t\pm k)$ terms (in @MarkMcClure formula above) and not just $u(\cdot, t)$ as is the 'standard' discretisation. – Winther Feb 08 '15 at 15:11
  • @Winther, think they are looking at the diamond of points $(j, n-1, j+1, n), (j, n+1), (j-1, n)$ and evaluating $u_t = u_{xx}$ at the point $(j, n)$ – abel Feb 08 '15 at 15:22
  • 1
    But $\zeta_{j}^{n}$ doesn't appear in the equations so we can't be solving for that, as we would for Laplace's equation. I think we're looking at the points $(j,n-1)$, $(j-1,n)$, and $(j+1,n)$ to deduce the value at $(j,n+1)$. This answers the question of @Winther - We need the discretization of $u_{xx}$ to fit well with the symmetric difference quotient used for $u_t$. I do think your answer has some relevant points but I also think the bivariate nature of the function is crucial. It's a simple fix that would make the answer perfectly worthy of an upvote, in my opinion. – Mark McClure Feb 08 '15 at 15:34
  • @MarkMcClure, thanks for your comments. let me see what the error terms look like in their scheme. if i have something better i will edit he post. – abel Feb 08 '15 at 15:44
  • @MarkMcClure you indeed are correct that we are using $(j,n-1),(j-1,n)$, and $(j+1,n)$ to deduce not just a value, but $\zeta = f(x,t)$ at $(j,n+1)$. – Alex Feb 08 '15 at 16:13