Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Please, help to understand the method which is used in the following snippet:

% delta - time step
% d - x step

% initialization
for all x from 0 to 1 in increments of delta
   f[x,0] = 1/2 * x^2

% run the differential equation forward
for t from 0 to T in increments of d

  % Compute the derivative
  for all x
    fderiv[x,t] = f[x,t] - f[x-d,t]  % adjust this appropriately at the boundary x = 0

  % Step the differential equation forward one unit
  for all x from 0 to 1 in increments of delta
    f[x,t+delta] = ( x - f[x,t])/fderiv[x,t] - x

    % f[x, t+delta] must stay <= x and >= 0
    if f[x, t+delta] > x then set f[x, t+delta] = x
    if f[x, t+delta] < 0 then set f[x, t+delta] = 0


If you are not familiar with programming, here is the same thing in the math notation:

$$f(x, 0) := \frac{x^2}{2}$$ $$f'(x, t) := f(x, t) - f(x - \delta_x, t)$$ $$f(x, t + \delta_t) := \min\left(\max\left(\frac{x - f(x, t)}{f'(x,t)} - x, x\right), 0\right)$$

This is the foreign code and it may contain errors. It looks like the method of solving some differential equation using the finite differences, but have only brief knowledge in this field. Thank you in advance!

share|cite|improve this question
As this is not a programming site, it may be advisable to leave only the math part of the question and expand it as well. – nbubis Oct 31 '12 at 0:11
What is the context? – Tpofofn Oct 31 '12 at 0:27
@Tpofofn It doesn't have a context. I was asked to translate this pseoudo-code to some programming language, but without understanding the meaning of the function I can't fix some bugs. – Alexander Solovets Oct 31 '12 at 2:11
@nbubis I think that people are unfamiliar with programming will just skip it, but it may help to those who are familiar. Can you give an advice about expanding the math part? – Alexander Solovets Oct 31 '12 at 2:14
up vote 0 down vote accepted

It looks like you're trying to solve the PDE:

$$ \partial_t f \cdot \partial_x f + f(1 + \partial_x) = x$$

With the initial condition: $$f(x,t = 0) = x^2/2$$

It moreover looks like the code is neglecting to put in $dx,dt$ when calculating the derivatives.

share|cite|improve this answer
Thanks! Now it makes much more sense for me. Can you suggest how to set the right values at the boundary: $f'(x,t) := f(x,t) - f(x - \delta_x, t)$ ? Here the left finite difference is used. Can I change it to the right finite difference (i.e. $f(x + \delta_x, t) - f(x, t)$), when $x$ equals $0$? – Alexander Solovets Nov 1 '12 at 5:02
You can always change it to the right difference, but you have to be consistent throughout - i.e. you should't mix left an right differences in the the same implementation, which won't really help you to much. To know the value of the derivative there, I think you need to know a little bit more about the problem - a.k.a what the derivative is at the boundary. – nbubis Nov 1 '12 at 5:14

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.