====== Lab 7: Solving nonlinear equations with Newton search ====== In this lab you will * write your first numerical algorithm * learn the most widely-used algorithm for solving nonlinear equations, and which underlies Matlab's ''fsolve'' function * solve a practical, real-world problem involving a nonlinear equation * gain more experience in programming with ''for'' loops and ''if'' statements ---- ** Problem 1:** Write a function ''x = newtonsearch(f, xguess)'' that finds the solution ''x'' of the equation ''f(x) = 0'' for an input function ''f'' and an initial guess ''xguess'' using the Newton search algorithm. - Use a ''for'' loop to perform the Newton-search iteration $x_{n+1} = x_n + \Delta x$. Take up to ten Newton steps. - Use an ''if'' statement inside the ''for'' loop to test if either $|f(x)| < \epsilon$ or $|\Delta x| < \epsilon$ for some specified tolerance $\epsilon$. If either condition is true, use a ''break'' statement to terminate the iteration and return from the function. For our purposes $\epsilon = 10^{-7}$ is a decent choice. ---- **Problem 2:** Test your Newton-search algorithm by solving the following problems. Check your answers by plugging the answer ''x'' back into ''f'' and verifying that ''f(x)'' is approximately zero. (a) Find an $x$ for which $x^3 - 7x - 13 = 0$. (b) Find the cube root of 72 by devising and solving an equation of the form $f(x) = 0$ whose solution is $x = \sqrt[3]{72}$. Is there a simpler way to calculate $\sqrt[3]{72}$ in Matlab? Do that, and compare your answers. %%(c)%% Find an $x$ for which $\sqrt{4-x^2} = x \tan x$. Hint: find good initial guesses for the Newton search by plotting each function and roughly estimating an $x$ position at which $f(x)$ is zero. ---- **Problem 3:** Use your Newton-search algorithm to solve the following problem. Utility companies must avoid freezing water mains in cold weather. If we assume uniform soil conditions, the temperature $T(x,t)$ at distance $x$ below the surface and time $t$ after the beginning of a cold spell is given approximately by \begin{eqnarray*} \frac{T(x,t) - T_s}{T_i-T_s} = \text{erf} \left(\frac{x}{\sqrt{2 \alpha t}}\right) \end{eqnarray*} where * $T_s$ is the constant surface temperature during the cold spell, * $T_i$ is the initial soil temperature before the cold spell started, * $\alpha$ is the thermal conductivity of the soil, and * $\erf$ is the {\it error function}, computed by the built-in Matlab function ''erf''. If $x$ is in meters and $t$ is in seconds, the $\alpha = 0.138 \times 10^{-6} \; m^2/s$. Let $T_i= 20\,C$ and $T_S = -15\,C$, and recall that water freezes at $T = 0\,C$. Use your Newton-search algorithm to determine how deep a water main must be buried so that it will not freeze until at least 60 days' exposure to these conditions.