Differential equation to Difference equation?
I have the following equation :
$$\frac{dx}{dt} = -5(x-2)$$
$$\frac{dy}{dt} = 0$$
How do I change this differential equation to a difference equation ? Do I use Euler forward method ? I remember taking this before but I have totally forgotten about it. I tried reading online to refresh my memory but I did not really grasp the idea. I would really appreciate if someone can solve this particular equation step by step so that I can fully understand the solution, along with supporting key concept points to grasp the idea. Thanks.
$\endgroup$ 24 Answers
$\begingroup$You seem to be interested in the general techniques for solving differential equations numerically.
Given $x'(t), y'(t)$ there are many ways you can come up with a differencing equation to approximate the solution on a discretized domain. Most of these are derived from Taylor series expansions.
$$x(t+\Delta t) = x(t) + x'(t) \Delta t + \ldots$$
Truncating the expansion here gives you forward differencing. As this is a problem rooted in time integration, this is most likely the kind of thing you would want to do.
However, as often as not one prefers more sophisticated approaches. Euler's method is simple but also not very good. 4th order Runge-Kutta is often used, as it strikes a balance between simplicity and accuracy that is usually pretty good. This too can, in principle, be derived from Taylor series expansions, but that's a bit more involved.
$\endgroup$ $\begingroup$$\frac{dx}{dt}=-5(x-2)$ then $\frac{dx}{(x-2)}=-5dt$ :integrate both side$$ln(x-2)=-5t+c $$$$x=e^{-5t+c}+2$$ and $y(t)=2t+c$
$\endgroup$ 3 $\begingroup$You can put $y$ in terms of $x$ by noting $dy/dx = (dy/dt) / (dx / dt)$.
For your first question, $dy/dx = (0) / (-5(x-2)) = 0$, so integrating, $y = C$ for some constant $C$.
Your second question is more complicated as it has both $x$ and $y$ in it, so I'm not sure this method will apply for that equation.
$\endgroup$ 1 $\begingroup$I will consider the first equation:
$$\frac{dx}{dt} = -5 (x-2)$$
There are many schemes for discretization. In my experience, centered difference works because the error is second order and the computation relatively light. In this, we assume that we have a vector of sample points $x_k$, $k \in \{1,2,3,\ldots,n\}$, each $x_k$ corresponding to a value of $t_k = (k-1) \Delta t$. The discrete equation then reads
$$\frac{x_{k+1/2} - x_{k-1/2}}{\Delta t} = - 5 (x_k - 2)$$
Why the half-steps? Again, it is a centered difference whose symmetry cancels out 1st-order error. We may compute the values of $x$ on the half steps by, e.g., averaging (so that $x_{k+1/2} = (1/2) (x_k + x_{k+1})$.
Now, in order to use this equation, you need an initial value, i.e., $x(0) = x_0$. Assume $x_{-1/2}=0$. You now have enough to propagate a solution through all of the $x_k$.
$\endgroup$ 2