User Tools

Site Tools


Lab 7: Nonlinear equations and Newton's method

In this lab you will

  • write your first numerical algorithm
  • learn the most widely-used algorithm for solving nonlinear equations
  • solve a practical, real-world problem involving a nonlinear equation
  • learn about anonymous functions
  • 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.

Your Newton search algorithm should

1. Use a for loop to perform the Newton-search iteration $x_{n+1} = x_n + \Delta x$. Take up to ten Newton-search iterations.

2. Use an if statement inside the for loop to test if either $|f(x)| < tol$ or $|\Delta x| < tol$ for some specified tolerance $tol$.

3. If either of those conditions is true, use a break statement to terminate the iteration and return from the function. For our purposes $tol = 10^{-13}$ 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

\frac{T(x,t) - T_s}{T_i-T_s} = \text{erf} \left(\frac{x}{\sqrt{2 \alpha t}}\right)


  • $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
  • $\text{erf}$ is the error function, computed by the built-in Matlab function erf.

If $x$ is in meters and $t$ is in seconds, the thermal conductivity of soil is $\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(x,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.

gibson/teaching/spring-2018/math445/lab7.txt · Last modified: 2018/03/29 06:55 by gibson