1
$\begingroup$

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   end;  end; 

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!

  • 0
    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.2012-10-31
  • 0
    What is the context?2012-10-31
  • 0
    @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.2012-10-31
  • 0
    @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?2012-10-31

1 Answers 1

0

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.

  • 0
    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$?2012-11-01
  • 0
    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.2012-11-01