[newton_method] Exercise 2 solution stops after the first starting value

Open Beginner friendly
#1,115 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@officialasishkumar is already working on this.

Since Nov 11, 2026.

  • #1125 by @officialasishkumar — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
82/100
Issue type
Documentation
Clarity
Clearly specified
Activity status
Active
Tech stack
markdown, python
Domain
documentation

Research direction

Read lectures/newton_method.md around lines 1082–1095: the loop in the raises-exception cell and the paragraph that follows it. Confirm whether #1073 has landed so the multivariate call uses newton_nd, then wrap each attempt in try/except, drop the tag, and rewrite the prose so attempts 2 and 3 are reported as converging. Also fix the tolerance at line 1061 to $10^{-15}$ or inline code. Done when the built page shows all three attempts, the attempt-1 failure, and matching text.

Written by the indexing model from the issue text.

Description

bug

While reviewing #1073 I noticed that the Exercise 2 solution only ever shows the first of its three starting values. The live page shows the same thing: the solution's output ends with the traceback from attempt 1.

What happens

The solution loops over the three initial price vectors in a cell tagged raises-exception (lines 1082–1090). Attempt 1, $p_0 = (5, 5, 5)$, fails as intended. Its first Newton step overshoots to $p \approx (-0.337, -0.337, -0.337)$, so $\sqrt{p}$ is NaN and newton raises "Solution not found with NaN generated". That exception ends the loop, so attempts 2 and 3 never run.

Here is what each starting value does when run on its own with the lecture's newton (tol=1e-15, max_iter=15):

Attempt Starting value Outcome
1 $(5, 5, 5)$ Fails at iteration 2, because the first step makes prices negative
2 $(1, 1, 1)$ Converges in 6 iterations to $(1.4974, 1.4974, 1.4974)$
3 $(4.5, 0.1, 4)$ Converges in 8 iterations to the same point

The text after the cell (lines 1091–1095) says it "may take a few initial guesses to achieve convergence", then checks "the second initial guess which converges". That reads as though the third guess failed too, but it converges. It just never runs.

Relation to #575

#575 asked about the exception in this cell. It was closed because the failure is deliberate: the exercise is meant to show that Newton's method can fail, and that should stay. This issue is narrower. The failure on attempt 1 shouldn't hide attempts 2 and 3. Making it "fail more gracefully" was offered as an option in #575.

Suggested change
  1. Catch the exception for each attempt, so all three run and the failure is still reported. Then drop the raises-exception tag:
for attempt, init in enumerate(initLs, start=1):
    print(f"Attempt {attempt}: Starting value is {init} \n")
    try:
        p = newton_nd(lambda p: e(p, A, b, c), init, tol=1e-15, max_iter=15)
    except Exception as err:
        print(f"Failed: {err}")
    print("-" * 64)

With the current IPython, %time passes the exception through, so it can stay inside the try if we want to keep the timings.

  1. Update the text after the cell to match. The first starting value fails because its first step takes prices negative, and the other two converge to the same equilibrium. The overshoot is also a useful teaching point about why Newton's method can fail.

  2. While there: line 1061 writes the tolerance as $1e-15$, which renders in math mode as "1e − 15". Use $10^{-15}$ or plain 1e-15 in backticks instead.

Timing

#1073 renames the multivariate newton to newton_nd in this cell (the snippet above already uses the new name), so this is easiest to do after #1073 merges.

Dominant language
TeX
Stars
129
Forks
61
Avg merge
2d 4h
Merged PRs (30d)
12

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from QuantEcon/lecture-python.myst

All issues in QuantEcon/lecture-python.myst

Similar issues

More Documentation issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.