next up previous
Next: Your job Up: Approximating iteratively: CSC270, Assignment Previous: A warm-up function


A more challenging function

In the previous section, you may have wondered ``why bother with an iterative solution when I can easily see that $ f(x)$ $ =$ $ x^2$ is a solution?'' However, what if you were modelling a problem where things were a bit messier? Suppose you knew that for some constant $ K$ you had:

$\displaystyle f'(x)$ $\displaystyle =$ $\displaystyle -f(x) - K(f(x) - \exp(-x))$ (1)
$\displaystyle f(0)$ $\displaystyle =$ $\displaystyle 2.$ (2)
       

Once again you have an unknown function, $ f(x)$, defined in terms of its derivative.1However, our current definition is more complicated, since the right-hand side is an expression in both $ x$ and our unknown function $ f(x)$. You might have studied differential equations (which Equation 1 is) and be able to use a well-studied bag of tricks (integrals, integrating factors, etc.)2to work out with paper and pencil that

$\displaystyle f(x) = \exp(-(K+1)x) + \exp(-x)$ (3)

However, if what you need is an approximation, you can proceed as in the previous section:

  1. Decide on a step size that you'll increment $ x$ by (we used 0.1 in the last section, but your mileage may vary), and call it $ h$.
  2. Denote your approximate function as $ f_x$ to distinguish it from the exact function $ f(x)$. Also denote the approximate derivative as $ f'_x$, since it won't always be identical to $ f'(x)$.
  3. Since you are given the exact value $ f(0)$ $ =$ 2, start things out on the right foot by setting $ f_0$ $ =$ 2, and the initial value of $ x$ $ =$ 0.
  4. $ f_h$ is $ f'_0\times h$ $ +$ $ f_0$, and $ f'_0$ is calculated by by substituting $ f_0$ for $ f(0)$ in Equation 1.
  5. $ f_{2h}$ is $ f'_h\times h$ $ +$ $ f_h$, substituting $ f_h$ for $ f(h)$ in Equation 1.
  6. And so on...
Since you've been given the exact expression for $ f(x)$, you can always compare your approximation $ f_x$ to $ f(x)$.


next up previous
Next: Your job Up: Approximating iteratively: CSC270, Assignment Previous: A warm-up function
Danny Heap 2002-09-22