MATLABMATLAB · Chapterبەش 6 · Lectureوانە 6.3

Project: Finding Roots Numericallyپڕۆژە: دۆزینەوەی ڕەگ بە ڕێگە ژمارەییەکان

An advanced project: solve f(x)=0f(x) = 0 when no formula helps. Look at the graph and the signs, see why a sign change guarantees a root (the Intermediate Value Theorem), build the bisection method and Newton's method as MATLAB function files with a tolerance and an iteration cap, see when Newton fails, check the answers with fzero, roots and polyval, meet the wider toolbox (fminbnd, fminsearch, integral, polyfit, interp1 …) and the guide's other methods, and compare everything in a small project.

پڕۆژەیەکی پێشکەوتوو: f(x)=0f(x) = 0 شیکار بکە کاتێک هیچ فۆرموولەیەک یارمەتی نادات. سەیری و هێماکان بکە، ببینە بۆچی گۆڕانی هێما ڕەگێک مسۆگەر دەکات ()، و وەک MATLAB بە سنووری هەڵەی ڕێگەپێدراو و سنووری دروست بکە، ببینە کەی سەرناکەوێت، وەڵامەکان بە fzero، roots و polyval بپشکنە، ئامرازی زیاتر بناسە (fminbnd، fminsearch، integral، polyfit، ⁦interp1⁩ …) و ڕێگاکانی تری ڕێبەرەکە و هەموو شتێک لە پڕۆژەیەکی بچووکدا بەراورد بکە.

19 min read20 خولەک خوێندنەوە · 15 stepsهەنگاو

What you will learnچی فێر دەبیت

  • Locate roots from a graph or a sign table and explain the Intermediate Value Theorem and its limits.
  • Carry out bisection by hand and as a function file, and predict the number of halvings from the tolerance.
  • Implement Newton's method with a given derivative and recognise its failures.
  • Check results with fzero, roots and polyval and use today's names fminbnd, fminsearch and integral.
  • Compare methods on several functions and report roots and step counts.
  • ڕەگەکان لە یان خشتەیەکی هێماوە بدۆزیتەوە و و سنوورەکانی ڕوون بکەیتەوە.
  • بە دەست و وەک ئەنجام بدەیت و ژمارەی نیوەکردنەکان لە سنووری هەڵەی ڕێگەپێدراوەوە پێشبینی بکەیت.
  • بە دراو جێبەجێ بکەیت و شکستەکانی بناسیتەوە.
  • ئەنجامەکان بە fzero، roots و polyval بپشکنیت و ناوەکانی ئەمڕۆ fminbnd، fminsearch و integral بەکار بهێنیت.
  • ڕێگاکان لەسەر چەند بەراورد بکەیت و ڕەگ و ژمارەی هەنگاوەکان ڕاپۆرت بکەیت.

The problem: solve f(x)=0f(x) = 0 when no formula helpsکێشەکە: f(x)=0f(x) = 0 شیکار بکە کاتێک هیچ فۆرموولەیەک یارمەتی نادات

For ax2+bx+c=0ax^2+bx+c=0 there is a formula (the solver). For most other equations there is none you would want to use: x3−3x−1=0x^3-3x-1=0 (the course guide's example), x−cos⁡x=0x-\cos x=0, e−x=xe^{-x}=x. find the answer differently: they start from a rough guess and improve it step by step until it is accurate enough. This project lesson builds two such methods as MATLAB function files, then checks them with MATLAB's own tools. Every file runs in MATLAB, MATLAB Online (free version) and GNU Octave.

بۆ ax2+bx+c=0ax^2+bx+c=0 فۆرموولەیەک هەیە (چارەسەرکەرەکە). بۆ زۆربەی تر هیچ فۆرموولەیەکی بەکەڵک نییە: x3−3x−1=0x^3-3x-1=0 (نموونەی ڕێبەری کۆرسەکە)، x−cos⁡x=0x-\cos x=0، e−x=xe^{-x}=x. وەڵامەکە بە شێوەیەکی تر دەدۆزنەوە: لە مەزەندەیەکی نزیکەوە دەست پێدەکەن و هەنگاو بە هەنگاو باشتری دەکەن تا بەس ورد دەبێت. ئەم وانە پڕۆژەییە دوو ڕێگەی لەو جۆرە وەک MATLAB دروست دەکات، پاشان بە ئامرازەکانی خودی MATLAB دەیانپشکنێت. هەموو لە MATLAB، ⁦MATLAB Online⁩ (وەشانی بەخۆڕایی) و ⁦GNU Octave⁩ـدا کار دەکات.

A root of f is a number r with f(r)=0f(r) = 0 — where the graph meets the x-axis. Always look before you compute: draw f (fplot) or make a small table and watch the signs.

ڕەگی f ژمارەیەکی rrـە کە f(r)=0f(r) = 0 — ئەو شوێنەی x دەبڕێت. هەمیشە پێش هەژمارکردن سەیر بکە: f بکێشە (fplot) یان خشتەیەکی بچووک دروست بکە و سەیری هێماکان بکە.

A sign table for x3−3x−1x^3 - 3x - 1خشتەی هێما بۆ x3−3x−1x^3 - 3x - 1MATLABType in a terminalلە تێرمیناڵدا بینووسە
f = @(x) x.^3 - 3*x - 1;
x = -2:0.5:2;
[x' f(x)']
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

' turns both rows into columns, so each output line is one pair: x and f(x)f(x). The sign changes between −2-2 and −1.5-1.5, between −0.5-0.5 and 0, and between 1.5 and 2 — three roots.

' هەردوو ڕیزەکە دەکات بە ستوون، بۆیە هەر دێڕێکی جووتێکە: x و f(x)f(x). هێما لە نێوان −2-2 و −1.5-1.5، لە نێوان −0.5-0.5 و 0 و لە نێوان ⁦1.5⁩ و 2دا دەگۆڕێت — سێ ڕەگ.

Guess the numberژمارەکە بدۆزەرەوە

Dilan thinks of a number from 1 to 100; Aram may only ask higher or lower? Aram says 50, then 25 or 75, always the middle of what is left. After seven questions he knows the number. Bisection, the first method here, plays this game with a curve: is the root left or right of the middle?

دیلان ژمارەیەک لە 1 تا ⁦100⁩ لە بیر دەگرێت؛ ئارام تەنها دەتوانێت بپرسێت زیاتر یان کەمتر؟ ئارام دەڵێت ⁦50⁩، پاشان ⁦25⁩ یان ⁦75⁩، هەمیشە ناوەڕاستی ئەو ژمارانەی هێشتا دەکرێت بن. دوای حەوت پرسیار ژمارەکە دەزانێت. ، یەکەم ڕێگەی ئێرە، ئەم یارییە لەگەڵ چەماوەیەک دەکات: ڕەگەکە لە لای چەپی ناوەڕاستە یان لای ڕاستی؟

Where the analogy stops workingسنووری ئەم نموونەیە

In the game the answer is a whole number and you hit it exactly. A root is usually an endless decimal, so the method stops when the remaining interval is small enough, not when it is exact.

لە یارییەکەدا وەڵامەکە و ڕێک دەیدۆزیتەوە. ڕەگ زۆرجار ژمارەیەکی دەیی بێکۆتایە، بۆیە ڕێگاکە کاتێک دەوەستێت کە بەس بچووک بێت، نەک کاتێک ڕێک بێت.

Quick checkپشکنینی خێراBetween which two table values must a root of x3−3x−1x^3 - 3x - 1 lie?ڕەگێکی x3−3x−1x^3 - 3x - 1 دەبێت لە نێوان کام دوو خشتەکەدا بێت؟

Why a sign change means a rootبۆچی گۆڕانی هێما واتە ڕەگ

If you draw a curve without lifting the pen from a point below the x-axis to a point above it, the pen must cross the axis somewhere. Mathematicians call such a function a , and the fact is the : if f is continuous on [a, b], it takes every value between f(a)f(a) and f(b)f(b). When f(a)f(a) and f(b)f(b) have opposite signs, 0 lies between them, so f(c)=0f(c) = 0 for some c in [a, b].

ئەگەر چەماوەیەک بەبێ هەڵگرتنی قەڵەم لە خاڵێکی خوار x بۆ خاڵێکی سەرووی بکێشیت، قەڵەمەکە دەبێت لە شوێنێکدا ببڕێت. بیرکارزانان بەم جۆرە دەڵێن و ئەم ڕاستییەش ـە: ئەگەر f لەسەر ⁦[a, b]⁩ بەردەوام بێت، هەموو لە نێوان f(a)f(a) و f(b)f(b)دا وەردەگرێت. کاتێک f(a)f(a) و f(b)f(b) هێمای پێچەوانەیان هەیە، 0 لە نێوانیاندایە، بۆیە f(c)=0f(c) = 0 بۆ cـێک لە ⁦[a, b]⁩ـدا.

f(a)⋅f(b)<0  ⟹  f(c)=0 for some c∈[a,b]f(a)\cdot f(b) < 0 \;\Longrightarrow\; f(c)=0 \text{ for some } c \in [a,b]

The test MATLAB code uses: a negative product means opposite signs. OpenStax's calculus book shows it for x−cos⁡xx-\cos x on [0,π/2][0, \pi/2]: f(0)=−1<0f(0) = -1 < 0 and f(π/2)=π/2>0f(\pi/2) = \pi/2 > 0, so a root exists there.

ئەو تاقیکردنەوەیەی کۆدی MATLAB بەکاری دەهێنێت: لێکدانی سالب واتە هێمای پێچەوانە. کتێبی ژمێرکاریی OpenStax بۆ x−cos⁡xx-\cos x لەسەر [0,π/2][0, \pi/2] پیشانی دەدات: f(0)=−1<0f(0) = -1 < 0 و f(π/2)=π/2>0f(\pi/2) = \pi/2 > 0، بۆیە ڕەگێک لەوێدا هەیە.

Three warnings. (1) The theorem says a root exists, not where it is — finding it is the job of the methods below. (2) Continuity matters: 1/x is −1-1 at −1-1 and 1 at 1, but it jumps at 0 and has no root. (3) No sign change does not prove there is no root: x2x^2 on [−1,1][-1, 1] is positive at both ends yet touches 0 at x=0x = 0, and an interval may also hide several roots — [−2,2][-2, 2] holds all three roots of the cubic.

سێ ئاگاداری. ⁦(1)⁩ دەڵێت ڕەگێک هەیە، نەک لە کوێیە — دۆزینەوەی کاری ئەو ڕێگایانەیە کە لە خوارەوەن. ⁦(2)⁩ بەردەوامی گرنگە: ⁦1/x⁩ لە −1-1دا −1-1ـە و لە 1دا 1ـە، بەڵام لە 0دا باز دەدات و ڕەگی نییە. ⁦(3)⁩ نەبوونی گۆڕانی هێما ئەوە ناسەلمێنێت کە ڕەگ نییە: x2x^2 لەسەر [−1,1][-1, 1] لە هەردوو سەردا موجەبە، بەڵام لە x=0x = 0دا بەر 0 دەکەوێت و دەشێت چەند ڕەگێکیش بشارێتەوە — [−2,2][-2, 2] هەر سێ ڕەگی لەخۆ دەگرێت.

Quick checkپشکنینی خێراf(1)=−2f(1) = -2 and f(3)=5f(3) = 5 for a continuous f. What can you be sure of?بۆ fـێکی بەردەوام f(1)=−2f(1) = -2 و f(3)=5f(3) = 5. دڵنیای لە چی؟

The bisection methodڕێگەی نیوەکردن

The keeps a bracket [a, b] with a sign change and halves it again and again:

کەوانەیەکی ⁦[a, b]⁩ بە گۆڕانی هێماوە دەهێڵێتەوە و دووبارە و دووبارە نیوەی دەکات:

  1. Midpointخاڵی ناوەڕاست

    Compute p=(a+b)/2p = (a + b)/2 and f(p)f(p).

    p=(a+b)/2p = (a + b)/2 و f(p)f(p) هەژمار بکە.

  2. Choose the halfنیوەکە هەڵبژێرە

    If f(a)⋅f(p)<0f(a)\cdot f(p) < 0 the sign changes in [a, p], so set b=pb = p; otherwise it changes in [p, b], so set a=pa = p.

    ئەگەر f(a)⋅f(p)<0f(a)\cdot f(p) < 0 هێما لە ⁦[a, p]⁩ـدا دەگۆڕێت، بۆیە b=pb = p دابنێ؛ ئەگینا لە ⁦[p, b]⁩ـدا دەگۆڕێت، بۆیە a=pa = p دابنێ.

  3. Repeatدووبارەکردنەوە

    Stop when the bracket is small enough; the midpoint of the last bracket is the answer.

    کاتێک کەوانەکە بەس بچووک بوو بوەستە؛ خاڵی ناوەڕاستی دوایین کەوانە وەڵامەکەیە.

The curve of x³ − 3x − 1 on −2 to 2 with three marked points: a = −2 where f(a) = −3 < 0, b = 2 where f(b) = 1 > 0, and the midpoint p = 0 where f(p) = −1 < 0. Below the graph, nested bars show the brackets [−2, 2], [0, 2], [1, 2], [1.5, 2] and [1.75, 2] shrinking towards a dashed line at the root 1.8794.چەماوەی x³ − 3x − 1 لەسەر −2 تا 2 بە سێ خاڵی نیشانکراو: a = −2 کە f(a) = −3 < 0، b = 2 کە f(b) = 1 > 0 و خاڵی ناوەڕاست p = 0 کە f(p) = −1 < 0. لە ژێر هێڵکاریی نەخشەکەدا شریتە هێلانەکراوەکان کەوانەکانی [−2, 2]، [0, 2]، [1, 2]، [1.5, 2] و [1.75, 2] پیشان دەدەن کە بەرەو هێڵێکی پچڕپچڕ لە ڕەگی 1.8794 بچووک دەبنەوە.
Bisection on [−2,2][-2, 2]: f(p)<0f(p) < 0 like f(a)f(a), so the root is in [p, b]. Drawn by CêIT from values computed with Python. لەسەر [−2,2][-2, 2]: f(p)<0f(p) < 0 وەک f(a)f(a)، بۆیە ڕەگەکە لە ⁦[p, b]⁩ـدایە. CêIT کێشاویەتی لە بە Python هەژمار کراون.CêIT
Trace of the first eight halvings on [−2,2][-2, 2] (computed with Python)بەدواداچوونی هەشت نیوەکردنی یەکەم لەسەر [−2,2][-2, 2] (بە Python هەژمار کراوە)
nabpf(p)kept halfنیوەی هێڵراوە
1−2-220−1.0000-1.0000[p, b]
2021−3.0000-3.0000[p, b]
3121.5−2.1250-2.1250[p, b]
41.521.75−0.8906-0.8906[p, b]
51.7521.875−0.0332-0.0332[p, b]
61.87521.93750.4607[a, p]
71.8751.93751.906250.2082[a, p]
81.8751.906251.8906250.0861[a, p]

The interval [−2,2][-2, 2] holds three roots, yet bisection follows only one of them: the rule keeps a half with a sign change, and here that leads to 1.8794. To find −1.5321-1.5321 or −0.3473-0.3473, start from [−2,−1][-2, -1] or [−1,0][-1, 0]. Bisection finds one root per bracket.

[−2,2][-2, 2] سێ ڕەگی تێدایە، بەڵام تەنها شوێنی یەکێکیان دەکەوێت: یاساکە نیوەیەک بە گۆڕانی هێماوە دەهێڵێتەوە و لێرەدا ئەوە دەگاتە ⁦1.8794⁩. بۆ دۆزینەوەی −1.5321-1.5321 یان −0.3473-0.3473، لە [−2,−1][-2, -1] یان [−1,0][-1, 0]ـەوە دەست پێبکە. بۆ هەر کەوانەیەک یەک ڕەگ دەدۆزێتەوە.

The bisection methodڕێگەی نیوەکردن

How many halvings?چەند جار نیوەکردن؟

Each halving cuts the bracket in two, so after n halvings its width is

هەر نیوەکردنێک کەوانەکە دەکاتە دوو بەش، بۆیە دوای n جار نیوەکردن پانییەکەی دەبێتە

widthn=b−a2n,∣pn−r∣≤b−a2n+1\text{width}_n=\frac{b-a}{2^n},\qquad |p_n - r| \le \frac{b-a}{2^{n+1}}

The midpoint is at most half a width from the root r. Every halving gives about one more binary digit; ten halvings divide the width by 210=10242^{10} = 1024, about three more decimal digits.

خاڵی ناوەڕاست زۆرترین نیوەی پانییەک لە ڕەگی r دوورە. هەر نیوەکردنێک نزیکەی یەک زیاتر دەدات؛ دە نیوەکردن پانییەکە دابەشی 210=10242^{10} = 1024 دەکات، نزیکەی سێ دەیی زیاتر.

To be within tol of the root you need b−a2n+1≤tol\frac{b-a}{2^{n+1}}\le \text{tol}, so n≥log⁡2b−atol−1n \ge \log_2\frac{b-a}{\text{tol}}-1 — the same halving logarithm as in binary search. For [−2,2][-2, 2] and tol = 0.0001: log⁡2(40 000)−1≈14.3\log_2(40\,000) - 1 \approx 14.3, so 15 halvings.

بۆ ئەوەی هەڵەکە لە tol زیاتر نەبێت پێویستت بە b−a2n+1≤tol\frac{b-a}{2^{n+1}}\le \text{tol} هەیە، کەواتە n≥log⁡2b−atol−1n \ge \log_2\frac{b-a}{\text{tol}}-1 — هەمان نیوەکردن وەک لە گەڕانی دووانی. بۆ [−2,2][-2, 2] و ⁦tol = 0.0001⁩: log⁡2(40 000)−1≈14.3\log_2(40\,000) - 1 \approx 14.3، کەواتە ⁦15⁩ نیوەکردن.

Animation in eight frames: bisection on x³ − 3x − 1 over [−2, 2]. Each frame shows the current bracket shaded, the midpoint p with the sign of f(p), and the kept half in green: p = 0, 1, 1.5, 1.75, 1.875, 1.9375, 1.90625, 1.890625, while the new width falls 2, 1, 0.5, 0.25, 0.125, 0.0625, 0.03125, 0.015625 towards the root 1.8794.
The bracket halves at every stepکەوانەکە لە هەموو هەنگاوێکدا نیوە دەبێتCêIT
Quick checkپشکنینی خێراA bracket of width 1 is halved 10 times. About how wide is it now?کەوانەیەکی پانی 1 دە جار نیوە دەکرێت. ئێستا نزیکەی چەندە پانە؟

Bisection as a function fileڕێگەی نیوەکردن وەک فایلی کردار

The guide writes the method as a function file and calls it with a function handle, exactly as the functions lesson showed. Here is a version in this track's style: inputs f, a, b and a tolerance; a check of the sign change first; a while loop (while loops) with a cap on the number of steps; and two outputs, the root and the number of steps.

ڕێبەرەکە ڕێگاکە وەک دەنووسێت و بە بانگی دەکات، ڕێک وەک وانەی کردارەکان پیشانی دا. ئەمە وەشانێکە بە شێوازی ئەم بەشە: f، a، b و سنووری هەڵەی ڕێگەپێدراو ⁦(tol)⁩؛ سەرەتا پشکنینی گۆڕانی هێما؛ while (خولەکانی while) بە سنوورێک بۆ ژمارەی هەنگاوەکان؛ و دوو ، ڕەگەکە و ژمارەی هەنگاوەکان.

my_bisection.mفایلی ⁦my_bisection.m⁩my_bisection.mMATLAB
function [root, steps] = my_bisection(f, a, b, tol)
%MY_BISECTION  A root of f in [a, b] by halving the interval.
if f(a)*f(b) >= 0
    error('f(a) and f(b) must have opposite signs.')
end
max_steps = 100;                  % a safety cap
steps = 0;
while (b - a)/2 > tol && steps < max_steps
    p = (a + b)/2;
    steps = steps + 1;
    if f(p) == 0
        a = p;  b = p;            % an exact root: stop
    elseif f(a)*f(p) < 0
        b = p;                    % the sign changes in [a, p]
    else
        a = p;                    % the sign changes in [p, b]
    end
end
root = (a + b)/2;
end
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3-5

No sign change, no guarantee: stop with a clear message instead of a wrong answer.

گۆڕانی هێما نییە، مسۆگەری نییە: بە پەیامێکی ڕوون بوەستە لە جیاتی وەڵامێکی هەڵە.

Lineدێڕی 8

Two reasons to stop: accurate enough, or too many steps.

دوو هۆکاری وەستان: بەس ورد، یان هەنگاوی زۆر.

Lineدێڕی 11-17

The three cases of one halving.

سێ حاڵەتەکەی یەک نیوەکردن.

Calling itبانگکردنی کردارەکەMATLABType in a terminalلە تێرمیناڵدا بینووسە
f = @(x) x.^3 - 3*x - 1;
[r, n] = my_bisection(f, -2, 2, 1e-6)
f(r)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

21 halvings bring [−2,2][-2, 2] down to a width of 4/221≈1.9×10−64/2^{21} \approx 1.9\times 10^{-6}.

⁦21⁩ نیوەکردن [−2,2][-2, 2] بۆ پانیی 4/221≈1.9×10−64/2^{21} \approx 1.9\times 10^{-6} دادەبەزێنێت.

Lineدێڕی 3

Always check: f(r)f(r) is close to 0, not exactly 0.

هەمیشە بپشکنە: f(r)f(r) نزیکە لە 0، نەک ڕێک 0.

Bisection as a function fileڕێگەی نیوەکردن وەک فایلی کردار

Spot the problems in the guide's routineکێشەکانی ڕوتینەکەی ڕێبەرەکە بدۆزەرەوە

The guide's version (also printed on a second page as 'Section Method' — the same routine under another name) differs in four ways:

وەشانەکەی ڕێبەرەکە (کە لە پەڕەیەکی تریشدا بە ناوی «⁦Section Method⁩» چاپ کراوە — هەمان ڕوتین بە ناوێکی تر) لە چوار شتدا جیاوازە:

  • A fixed number of halvings. It takes n and halves exactly n times: with n=4n = 4 on [−2,2][-2, 2] it returns p=1.75p = 1.75, which is still 0.13 away from the root. A tolerance says what you really want.
  • Typos that break or confuse. ferintf should be fprintf (an error the moment that line runs); the messages say 'intevral', 'contaion' and 'no soll'; and the last line End (capital E) does not close the outer if — MATLAB is case-sensitive, so the file does not run at all.
  • Printing instead of stopping. With no sign change it only prints a message and the function returns nothing useful; error(…) stops clearly.
  • x^3 in the handle and a missing ;. @(x) x^3-3*x-1 works for one number but not for vectors or fplot; the 'Section Method' copy also prints p at every step because p=(a+b)/2 has no semicolon.
  • ژمارەیەکی جێگیری نیوەکردن. n وەردەگرێت و ڕێک n جار نیوە دەکات: بە n=4n = 4 لەسەر [−2,2][-2, 2]، p=1.75p = 1.75 دەگەڕێنێتەوە کە هێشتا ⁦0.13⁩ لە ڕەگەکەوە دوورە. سنووری هەڵەی ڕێگەپێدراو ئەوە دەڵێت کە بەڕاستی دەتەوێت.
  • هەڵەی نووسین کە دەیشکێنن یان سەرگەردان دەکەن. ferintf دەبێت fprintf بێت (هەر کە ئەو دێڕە جێبەجێ بوو دەدات)؛ پەیامەکان دەڵێن «intevral»، «contaion» و «⁦no soll⁩»؛ و دوایین دێڕ End (بە Eـی گەورە) ifـە دەرەکییەکە داناخات — MATLAB ، بۆیە هەر جێبەجێ نابێت.
  • چاپکردن لە جیاتی وەستان. بە بێ گۆڕانی هێما تەنها پەیامێک چاپ دەکات و هیچ شتێکی بەکەڵک ناگەڕێنێتەوە؛ error(…) بە ڕوونی دەوەستێت.
  • x^3 لە و ونبوو. @(x) x^3-3*x-1 بۆ یەک ژمارە کار دەکات بەڵام بۆ یان fplot نا؛ کۆپییەکەی «⁦Section Method⁩» هەروەها لە هەموو هەنگاوێکدا p چاپ دەکات چونکە p=(a+b)/2 نییە.
Quick checkپشکنینی خێراWhy does the guide's bisection_method file not run at all, even before any typo is reached?بۆچی bisection_methodـی ڕێبەرەکە هەر جێبەجێ نابێت، تەنانەت پێش ئەوەی بگاتە هەڵەی نووسینەکان؟

Newton's method: follow the tangentڕێگەی نیوتن: شوێن هێڵە بەرکەوتووەکە بکەوە

Bisection is safe but slow: one binary digit per step. is usually much faster. From a guess x0x_0, draw the to the curve at (x0,f(x0))(x_0, f(x_0)) and see where it meets the x-axis; that point is the next guess. The tangent's slope is the f′(x0)f'(x_0), which gives the formula

سەلامەتە بەڵام هێواشە: هەر هەنگاوێک یەک . زۆرجار زۆر خێراترە. لە مەزەندەیەکی x0x_0ـەوە، بۆ چەماوەکە لە (x0,f(x0))(x_0, f(x_0))ـدا بکێشە و سەیر بکە لە کوێ x دەبڕێت؛ ئەو خاڵە مەزەندەی داهاتووە. بریتییە لە f لە x0x_0ـدا، واتە f′(x0)f'(x_0)، کە ئەم فۆرموولەیە دەدات

xn+1=xn−f(xn)f′(xn)x_{n+1}=x_n-\frac{f(x_n)}{f'(x_n)}

CêIT does not teach derivatives yet, so here f′f' is given: for f(x)=x3−3x−1f(x) = x^3 - 3x - 1 it is f′(x)=3x2−3f'(x) = 3x^2 - 3. For any polynomial MATLAB can find the coefficients of the derivative: polyder([1 0 -3 -1]) gives 3 0 -3.

CêIT هێشتا فێر ناکات، بۆیە لێرەدا f′f' دراوە: بۆ f(x)=x3−3x−1f(x) = x^3 - 3x - 1 بریتییە لە f′(x)=3x2−3f'(x) = 3x^2 - 3. بۆ هەر MATLAB دەتوانێت بدۆزێتەوە: polyder([1 0 -3 -1]) ئەمە دەدات 3 0 -3.

A close-up of x³ − 3x − 1 between 1.7 and 2.6. From x0 = 2.5 a dashed tangent line runs down to the x-axis at x1 = 2.0476; from the curve above x1 a second tangent reaches the axis at x2 = 1.8970, already close to where the curve crosses at 1.8794.وێنەیەکی نزیکی x³ − 3x − 1 لە نێوان 1.7 و 2.6. لە x0 = 2.5ـەوە هێڵێکی بەرکەوتووی پچڕپچڕ دادەبەزێت بۆ تەوەرەی x لە x1 = 2.0476؛ لە چەماوەکەی سەرووی x1ـەوە هێڵێکی بەرکەوتووی دووەم دەگاتە تەوەرەکە لە x2 = 1.8970، کە ئێستاش نزیکە لەو شوێنەی چەماوەکە لە 1.8794دا دەیبڕێت.
Two Newton steps; the picture starts at x0=2.5x_0 = 2.5 so the steps are big enough to see. Drawn by CêIT from values computed with Python.دوو هەنگاوی ؛ وێنەکە لە x0=2.5x_0 = 2.5ـەوە دەست پێدەکات بۆ ئەوەی هەنگاوەکان بەس گەورە بن بۆ بینین. CêIT کێشاویەتی لە بە Python هەژمار کراون.CêIT
Newton from x₀ = 2 (computed with Python; the root is 1.879385241571817) لە x0=2x_0 = 2ـەوە (بە Python هەژمار کراوە؛ ڕەگەکە ⁦1.879385241571817⁩ـە)
nx_ncorrect digits دروست
020
11.8888892
21.8794524
31.8793852459
41.87938524157181716

The number of correct digits roughly doubles each step, so four Newton steps beat about fifty halvings. As a function file, with the same safety ideas as my_bisection:

ژمارەی لە هەر هەنگاوێکدا نزیکەی دوو هێندە دەبێت، بۆیە چوار هەنگاوی لە نزیکەی پەنجا نیوەکردن باشترن. وەک ، بە هەمان بیرۆکەکانی سەلامەتیی my_bisection:

my_newton.mفایلی ⁦my_newton.m⁩my_newton.mMATLAB
function [x, steps] = my_newton(f, df, x0, tol)
%MY_NEWTON  Newton's method from x0; df is the derivative of f.
x = x0;
steps = 0;
step = Inf;
while abs(step) > tol && steps < 50
    if df(x) == 0
        error('df(x) is 0: the tangent line never meets the x-axis.')
    end
    step = f(x)/df(x);
    x = x - step;
    steps = steps + 1;
end
end
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 6

Stop when the last step was tiny, or after 50 steps.

کاتێک دوایین هەنگاو زۆر بچووک بوو بوەستە، یان دوای ⁦50⁩ هەنگاو.

Lineدێڕی 7-9

A horizontal tangent (f′=0)(f' = 0) never meets the axis.

ئاسۆیی (f′=0)(f' = 0) هەرگیز نابڕێت.

Five steps to full accuracyپێنج هەنگاو بۆ وردیی تەواوMATLABType in a terminalلە تێرمیناڵدا بینووسە
f  = @(x) x.^3 - 3*x - 1;
df = @(x) 3*x.^2 - 3;
format long
[x, n] = my_newton(f, df, 2, 1e-10)
format short
Newton raphson method numerical analaysis kurdish(فێرکاری کوردی)mohammad E. · Kurdish (Sorani)کوردی (سۆرانی) · 1.4K viewsبینین · 40 likesلایک

In Sorani Kurdish, worked by hand: the Newton–Raphson formula and its repeated steps for x³ − x − 1 from x₀ = 1.5, with a calculator.

بە کوردیی سۆرانی و بە دەست: و دووبارەکردنەوەی هەنگاوەکانی بۆ x³ − ⁦x − 1⁩ لە x₀ = ⁦1.5⁩ـەوە، بە .

In Sorani, worked by hand on paper (filmed with a phone): the formula and its repeated steps. The tangent-line picture is on this screen.

بە سۆرانی، بە دەست لەسەر کاغەز (بە مۆبایل تۆمارکراوە): و دووبارەکردنەوەی هەنگاوەکان. وێنەی لەسەر ئەم شاشەیەیە.

Newton's method: follow the tangentڕێگەی نیوتن: شوێن هێڵە بەرکەوتووەکە بکەوە

When Newton's method failsکاتێک ڕێگەی نیوتن سەرناکەوێت

Speed has a price: Newton has no bracket, so nothing keeps it near the root. OpenStax's calculus book lists the classic failures; here they are with this lesson's numbers:

خێرایی نرخێکی هەیە: کەوانەی نییە، بۆیە هیچ شتێک لە نزیک ڕەگەکە نایهێڵێتەوە. کتێبی ژمێرکاریی OpenStax شکستە باوەکان ڕیز دەکات؛ ئەمانەن بە ژمارەکانی ئەم وانەیە:

Three ways Newton's method goes wrong (values computed with Python)سێ ڕێگا کە تێیدا هەڵە دەڕوات ( بە Python هەژمار کراون)
ProblemکێشەExampleنموونەWhat happensچی ڕوو دەدات
f′(x0)=0f'(x_0) = 0سفربوونی f′(x0)f'(x_0)x3−3x−1x^3 - 3x - 1 from x0=1(f′(1)=0)x_0 = 1 (f'(1) = 0)x3−3x−1x^3 - 3x - 1 لە x0=1x_0 = 1ـەوە (f′(1)=0)(f'(1) = 0)the tangent is horizontal: division by zero; my_newton stops with its error ئاسۆییە: دابەشکردن بە سفر؛ my_newton بە دەوەستێت
A start far from the wanted rootدەستپێکێک دوور لە ڕەگی ویستراوx3−3x−1x^3 - 3x - 1 from x0=0.9x_0 = 0.9x3−3x−1x^3 - 3x - 1 لە x0=0.9x_0 = 0.9ـەوەit jumps to −4.31-4.31 and ends at the other root −1.5321-1.5321باز دەدات بۆ −4.31-4.31 و لە ڕەگەکەی تر −1.5321-1.5321 کۆتایی دێت
Oscillationلەرینەوەx3−2x+2x^3 - 2x + 2 from x0=0x_0 = 0x3−2x+2x^3 - 2x + 2 لە x0=0x_0 = 0ـەوە0, 1, 0, 1, … for ever — the cap of 50 steps saves the loop0، 1، 0، 1، … بۆ هەمیشە — سنووری ⁦50⁩ هەنگاو ڕزگار دەکات

Two practical rules follow: start Newton close to the root (a few bisection steps or a graph give a good x₀), and always keep an iteration cap. The real root of x3−2x+2x^3 - 2x + 2 is about −1.7693-1.7693; Newton reaches it from x0=−2x_0 = -2.

دوو ئامۆژگاریی بەکەڵک لێرەوە دێن: لە نزیک ڕەگەکەوە دەست پێبکە (چەند هەنگاوێکی یان x0x_0ـێکی باش دەدەن) و هەمیشە سنوورێک بۆ ژمارەی دابنێ. ڕەگی ڕاستیی x3−2x+2x^3 - 2x + 2 نزیکەی −1.7693-1.7693ـە؛ لە x0=−2x_0 = -2ـەوە دەیگاتێ.

Quick checkپشکنینی خێراWhy does my_newton keep the cap steps < 50?بۆچی my_newton سنووری steps < 50 دەهێڵێتەوە؟

Newton's Method (2 of 2: Potential Dangers)Eddie Woo · Englishئینگلیزی · 48K viewsبینین · 1K likesلایک

Three ways Newton's method goes wrong, drawn on the board: a horizontal tangent, a guess that runs away, and guesses that jump back and forth.

سێ ڕێگە کە تێیدا شکست دەهێنێت، لەسەر تەختە کێشراون: ئاسۆیی، مەزەندەیەک کە دوور دەکەوێتەوە و مەزەندەگەلێک کە بەملاولادا باز دەدەن.

Danger 1: a horizontal tangent at a stationary point; danger 2: a first guess on the wrong side of a stationary point; danger 3: f(x) = x^3 - 5x from x0 = 1, where the guesses jump between 1 and -1.

مەترسیی یەکەم: ئاسۆیی لە خاڵێکی وەستاودا؛ مەترسیی دووەم: یەکەم مەزەندە لە لای هەڵەی خاڵێکی وەستاو؛ مەترسیی سێیەم: f(x)=x3−5xf(x) = x^3 - 5x کە لە x0=1x_0 = 1 دەست پێدەکات و مەزەندەکان لە نێوان 1 و ⁦-1⁩ دێن و دەچن.

Checking with MATLAB's own tools: fzero and rootsپشکنین بە ئامرازەکانی خودی MATLAB: fzero و roots

Your own methods teach how root finding works; for real work MATLAB has tested tools. fzero(f, [a b]) needs a sign change on [a, b] and returns a point where f changes sign. For a polynomial, roots takes the coefficients in descending powers — x3+0x2−3x−1x^3 + 0x^2 - 3x - 1 is [1 0 -3 -1] — and returns all roots, complex ones too; polyval evaluates the polynomial to check them.

ڕێگاکانی خۆت فێرت دەکەن دۆزینەوەی ڕەگ چۆن کار دەکات؛ بۆ کاری ڕاستەقینە MATLAB ئامرازی تاقیکراوەی هەیە. fzero(f, [a b]) پێویستی بە گۆڕانی هێما لەسەر ⁦[a, b]⁩ هەیە و خاڵێک دەگەڕێنێتەوە کە هێمای f تێیدا دەگۆڕێت. بۆ ، roots بە ڕیزبەندیی دابەزینی وەردەگرێت — x3+0x2−3x−1x^3 + 0x^2 - 3x - 1 دەبێتە [1 0 -3 -1] — و هەموو ڕەگەکان دەگەڕێنێتەوە، ڕەگە ئاوێتەکانیشەوە؛ polyval هەژمار دەکات بۆ پشکنینیان.

fzero and rootsfzero و rootsMATLABType in a terminalلە تێرمیناڵدا بینووسە
f = @(x) x.^3 - 3*x - 1;
fzero(f, [1 2])
fzero(f, [-2 -1])
r = roots([1 0 -3 -1])
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 4

Three real roots; the order MATLAB lists them in may differ from this.

سێ ڕەگی ڕاستی؛ لەوانەیە ڕیزبەندیی MATLAB لەمە جیاواز بێت.

polyval([1 0 -3 -1], r) then gives three numbers of the size 1e-15 — zero up to round-off. Two more facts from MathWorks' pages: fzero cannot find a root where the curve only touches zero, such as x2x^2 at 0 (there is no sign change), and roots shows complex roots as complex numbers: roots([1 0 0 -8]) gives 2 and −1±1.7321i-1 \pm 1.7321i, the three cube roots of 8.

پاشان polyval([1 0 -3 -1], r) سێ ژمارە لە قەبارەی ⁦1e-15⁩ دەدات — سفر تا ئاستی . دوو ڕاستیی تر لە پەڕەکانی MathWorks: fzero ناتوانێت ڕەگێک بدۆزێتەوە کە چەماوەکە تەنها بەر سفر دەکەوێت، وەک x2x^2 لە 0دا (گۆڕانی هێما نییە) و roots ڕەگە ئاوێتەکان وەک پیشان دەدات: roots([1 0 0 -8]) 2 و −1±1.7321i-1 \pm 1.7321i دەدات، سێ 8.

Quick checkپشکنینی خێراWhich call finds the root of x3−3x−1x^3 - 3x - 1 near −0.35-0.35?کام بانگکردن ڕەگی x3−3x−1x^3 - 3x - 1 لە نزیک −0.35-0.35 دەدۆزێتەوە؟

The wider toolbox: polynomials, interpolation, minima, integralsئامرازی زیاتر: ڕادەدارەکان، پڕکردنەوەی نێوان خاڵەکان، کەمترین نرخ، تەواوکارییەکان

The commands list ends with a page of numerical-methods commands. Each one solves a whole problem that you could also program yourself. One row each, for further study — all of them exist in MATLAB and Octave:

لیستی بە پەڕەیەک لە کۆتایی دێت. هەریەکەیان کێشەیەکی تەواو چارەسەر دەکات کە خۆت دەتوانیت بەرنامەی بۆ بنووسیت. هەریەکە یەک ڕیز، بۆ خوێندنی زیاتر — هەموویان لە MATLAB و Octaveـدا هەن:

Numerical-methods commands (results computed with Python) (ئەنجامەکان بە Python هەژمار کراون)
CommandWhat it doesچی دەکاتExample → resultنموونە ← ئەنجام
poly(r)polynomial from its roots (the inverse of roots) لە ڕەگەکانییەوە (پێچەوانەی roots)poly([2 3]) → 1 -5 6
polyval(p, x)value of a polynomial at x لە xـداpolyval([1 -5 6], 4) → 2
conv / deconvmultiply / divide polynomialsلێکدان / دابەشکردنی conv([1 2], [1 -3]) → 1 -1 -6
polyfit(x, y, n)best polynomial of degree n through dataباشترین بە پلەی n بە ناو polyfit(1:4, [2.1 3.9 6.2 7.8], 1) → 1.9400 0.1500
polyder(p)coefficients of the derivative polyder([1 0 -3 -1]) → 3 0 -3
interp1 / interp2values between data points (1-D / 2-D) نێوان خاڵەکانی (یەک / دوو ڕەهەند)interp1([1 2 3], [10 20 15], 2.5) → 17.5000
spline / unmkppa smooth curve through the points / its piecesچەماوەیەکی لووس بە ناو خاڵەکاندا / پارچەکانیsee the documentationبڕوانە بەڵگەنامەکان
fzeroa root of one function (sign change)ڕەگێکی یەک (گۆڕانی هێما)fzero(f, [1 2]) → 1.8794
fminbnd(f, a, b)a minimum of f on [a, b]کەمترین f لەسەر ⁦[a, b]⁩fminbnd(f, 0, 2) → 1.0000
fminsearch(f, x0)a minimum of a function of several variables, from x0کەمترین بە چەند ، لە ⁦x0⁩ـەوەsee the documentationبڕوانە بەڵگەنامەکان
integral(f, a, b)area under a curve (definite integral)ڕووبەری ژێر چەماوەیەک ()integral(@(x) exp(-x.^2), 0, 1) → 0.7468
trapz(x, y)area from data points (trapezoids)ڕووبەر لە خاڵەکانی (لاکێشەی لار)trapz(0:0.5:2, (0:0.5:2).^2) → 2.7500
diff(v)differences of neighboursجیاوازیی دراوسێکانdiff([1 4 9 16]) → 3 5 7
eig(A)eigenvalues of a square matrixنرخە تایبەتەکانی see the documentationبڕوانە بەڵگەنامەکان

Two of the results are worth a second look. fminbnd(f, 0, 2) finds the bottom of the cubic's valley at x=1x = 1, where f(1)=−3f(1) = -3. trapz gives 2.75 instead of the exact area 8/3≈2.66678/3 \approx 2.6667 because it joins only five points with straight lines — more points, smaller error, just as with plot.

دوو لە ئەنجامەکان شایەنی سەیرکردنی دووبارەن. fminbnd(f, 0, 2) بنی دۆڵەکەی لە x=1x = 1دا دەدۆزێتەوە، کە f(1)=−3f(1) = -3. trapz لە جیاتی ڕووبەری ڕێک 8/3≈2.66678/3 \approx 2.6667، ⁦2.75⁩ دەدات چونکە تەنها پێنج خاڵ بە هێڵی ڕاست دەبەستێتەوە — خاڵی زیاتر، هەڵەی کەمتر، هەروەک لە plotـدا.

The guide's other methods, in one sentence eachڕێگاکانی تری ڕێبەرەکە، هەریەکە بە یەک ڕستە

The course guide lists more numerical methods with code. You will meet them in a numerical-analysis course; here is what each one is for.

ڕێبەری کۆرسەکە زیاتر لەگەڵ کۆد ڕیز دەکات. لە کۆرسێکی شیکاریی ژمارەییدا دەیانبینیت؛ ئەمە ئەوەیە کە هەریەکەیان بۆ چییە.

Further methods named in the course guideڕێگاکانی تر کە لە ڕێبەری کۆرسەکەدا ناو براون
MethodڕێگاProblemکێشەIdea in one sentenceبیرۆکە بە یەک ڕستە
False position (regula falsi)شوێنی هەڵە ⁦(regula falsi)⁩a root of f(x)=0f(x) = 0ڕەگی f(x)=0f(x) = 0like bisection, but the new point is where the straight line from (a,f(a))(a, f(a)) to (b,f(b))(b, f(b)) crosses the axisوەک ، بەڵام خاڵی نوێ ئەو شوێنەیە کە هێڵی ڕاست لە (a,f(a))(a, f(a))ـەوە بۆ (b,f(b))(b, f(b)) دەبڕێت
Secant methodڕێگەی بڕەرa root of f(x)=0f(x) = 0ڕەگی f(x)=0f(x) = 0like Newton, but the tangent is replaced by the line through the last two guesses, so no f′f' is neededوەک ، بەڵام بە هێڵی نێوان دوو دوایین مەزەندە دەگۆڕدرێت، بۆیە پێویست بە f′f' ناکات
Fixed-point iterationدووبارەکردنەوەی خاڵی جێگیرx=g(x)x = g(x)شیکارکردنی x=g(x)x = g(x)repeat x←g(x)x \leftarrow g(x); it settles on a solution when g changes slowly enoughx←g(x)x \leftarrow g(x) دووبارە بکەرەوە؛ کاتێک g بەس هێواش دەگۆڕێت لەسەر شیکارێک جێگیر دەبێت
Back substitutionجێگرتنەوەی دواوەUx=bUx = b with U upper triangularUx=bUx = b کە U سێگۆشەیی سەرەوەیەsolve the last equation first, then work upwardsیەکەم جار دوایین شیکار بکە، پاشان بەرەو سەرەوە بڕۆ
LU factorisation (Doolittle)شیتەڵکردنی LU (دووڵیتڵ)Ax=bAx = b, many right-hand sidesAx=bAx = b، زۆر لای ڕاستی جیاوازwrite A=LUA = LU (lower × upper triangular), then solve two triangular systems; MATLAB: [L, U, P] = lu(A)A=LUA = LU بنووسە (سێگۆشەیی خوارەوە × سەرەوە)، پاشان دوو سیستەمی سێگۆشەیی شیکار بکە؛ MATLAB: [L, U, P] = lu(A)
Cholesky factorisationشیتەڵکردنی چۆلێسکیAx=bAx = b, A symmetric and positive definiteAx=bAx = b، A و ئەرێنیی دیاریکراوwrite A=RTRA = R^TR with one triangular R, about twice as fast as LU; MATLAB: chol(A)A=RTRA = R^TR بنووسە بە یەک Rـی سێگۆشەیی، نزیکەی دوو هێندە خێراتر لە LU؛ MATLAB: chol(A)
Jacobi iterationدووبارەکردنەوەی یاکۆبیAx=bAx = b, large systemsAx=bAx = b، سیستەمی گەورەimprove every unknown from the previous guess of all the othersهەموو نەزانراوێک لە مەزەندەی پێشووی هەموو ئەوانی ترەوە باشتر بکە
Gauss–Seidel iterationدووبارەکردنەوەی گاوس–زایدلAx=bAx = b, large systemsAx=bAx = b، سیستەمی گەورەlike Jacobi, but each new value is used at once, usually converging fasterوەک یاکۆبی، بەڵام هەر نوێ یەکسەر بەکار دێت و زۆرجار خێراتر نزیک دەبێتەوە

For a system Ax=bAx = b in everyday MATLAB you simply write x = A\b (left division); MATLAB chooses a suitable factorisation itself. The hand method behind it, Gauss–Jordan elimination, is in inverse by Gauss–Jordan, and inverses in general in the inverse of a matrix.

بۆ سیستەمێکی Ax=bAx = b لە MATLABـی ڕۆژانەدا بە سادەیی دەنووسیت x = A\b (دابەشکردنی چەپ)؛ MATLAB خۆی شیتەڵکردنێکی گونجاو هەڵدەبژێرێت. ڕێگا دەستییەکەی پشتییەوە، ، لە پێچەوانە بە گاوس–جۆرداندایە و بە گشتی لە پێچەوانەی ماتریکس.

Project: compare the methodsپڕۆژە: ڕێگاکان بەراورد بکە

Put everything together. Your tasks:

هەموو شتێک پێکەوە دابنێ. ئەرکەکانت:

  1. Save my_bisection.m and my_newton.m from this lesson in one folder.
  2. Test them on three functions: x3−3x−1x^3 - 3x - 1 on [1, 2]; x−cos⁡xx-\cos x on [0,π/2][0, \pi/2]; and x2+1x^2 + 1 on [−1,1][-1, 1], which has no sign change (and no real root).
  3. Compare with fzero and report a small table: method, root, steps.
  4. Plot each function with its root marked by a red circle.
  1. my_bisection.m و my_newton.mـی ئەم وانەیە لە یەک هەڵبگرە.
  2. لەسەر سێ تاقییان بکەرەوە: x3−3x−1x^3 - 3x - 1 لەسەر ⁦[1, 2]⁩؛ x−cos⁡xx-\cos x لەسەر [0,π/2][0, \pi/2]؛ و x2+1x^2 + 1 لەسەر [−1,1][-1, 1] کە گۆڕانی هێمای نییە (و ڕەگی ڕاستیی نییە).
  3. بەراوردی بکە لەگەڵ fzero و خشتەیەکی بچووک ڕاپۆرت بکە: ڕێگا، ڕەگ، هەنگاو.
  4. هەر بە ڕەگەکەیەوە بکێشە کە بە بازنەیەکی سوور نیشان کراوە.
ExerciseڕاهێنانWorked answerوەڵامی چارەسەرکراو

Write the script, run it, and explain each line of output.

بنووسە، جێبەجێی بکە و هەر دێڕێکی ڕوون بکەرەوە.

Show answerوەڵامەکە پیشان بدە
project.mMATLABType in a terminalلە تێرمیناڵدا بینووسە
% project.m - compare the methods on three functions
f1 = @(x) x.^3 - 3*x - 1;   d1 = @(x) 3*x.^2 - 3;
f2 = @(x) x - cos(x);        d2 = @(x) 1 + sin(x);
[r1, n1] = my_bisection(f1, 1, 2, 1e-6);
[x1, m1] = my_newton(f1, d1, 1.5, 1e-10);
[r2, n2] = my_bisection(f2, 0, pi/2, 1e-6);
[x2, m2] = my_newton(f2, d2, pi/4, 1e-10);
fprintf('f1: bisection %.6f (%d steps), Newton %.6f (%d steps), fzero %.6f\n', r1, n1, x1, m1, fzero(f1, [1 2]))
fprintf('f2: bisection %.6f (%d steps), Newton %.6f (%d steps), fzero %.6f\n', r2, n2, x2, m2, fzero(f2, [0 pi/2]))
fplot(f1, [-2 2]), hold on, plot(r1, f1(r1), 'ro'), grid on, hold off
r3 = my_bisection(@(x) x.^2 + 1, -1, 1, 1e-6);   % no sign change
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 5,7

Newton starts from the middle of each bracket.

لە ناوەڕاستی هەر کەوانەیەکەوە دەست پێدەکات.

Lineدێڕی 10

hold on keeps the curve while the root is added as a red circle.

hold on چەماوەکە دەهێڵێتەوە لە کاتێکدا ڕەگەکە وەک بازنەیەکی سوور زیاد دەکرێت.

Lineدێڕی 11

The last call stops the script on purpose: x2+1x^2 + 1 is positive at both ends.

دوایین بانگکردن بە مەبەست دەوەستێنێت: x2+1x^2 + 1 لە هەردوو سەردا موجەبە.

Root (number of steps); values computed with Pythonڕەگ (ژمارەی هەنگاو)؛ بە Python هەژمار کراون
FunctionBisection (tol 1e-6) ⁦(tol 1e-6)⁩Newton (tol 1e-10) ⁦(tol 1e-10)⁩fzero
x^3 - 3x - 11.879386 (19)1.879385 (6)1.879385
x - cos(x)0.739086 (20)0.739085 (4)0.739085
x^2 + 1error: no sign change: گۆڕانی هێما نییەnot triedتاقی نەکراوەتەوەerror: no sign change: گۆڕانی هێما نییە

Bisection's last digit differs because it stops within 1e-6, while Newton's tolerance was 1e-10. Newton needed 4–6 steps against 19–20 halvings. The third function shows why the sign test comes first; the error line's exact wording differs a little between MATLAB and Octave.

دوایین جیاوازە چونکە کاتێک هەڵەکەی لە ⁦1e-6⁩ کەمتر بوو دەوەستێت، لە کاتێکدا سنووری هەڵەی ⁦1e-10⁩ بوو. پێویستی بە ⁦4–6⁩ هەنگاو بوو بەرامبەر ⁦19–20⁩ نیوەکردن. سێیەم پیشان دەدات بۆچی تاقیکردنەوەی هێما یەکەم دێت؛ دەقی ڕێکی دێڕی لە نێوان MATLAB و Octaveـدا کەمێک جیاوازە.

Common mistakesهەڵە باوەکان

Root-finding mistakes and their fixesهەڵەکانی دۆزینەوەی ڕەگ و چاککردنیان
MistakeهەڵەWhat happensچی ڕوو دەداتFixچاککردن
No sign change in [a, b]گۆڕانی هێما لە ⁦[a, b]⁩ـدا نییەbisection and fzero stop with an error, or a hand-made loop halves towards nothing و fzero بە دەوەستن، یان دەستکرد بەرەو هیچ نیوە دەکاتplot first; choose a bracket whose ends have opposite signsسەرەتا بیکێشە؛ کەوانەیەک هەڵبژێرە کە سەرەکانی هێمای پێچەوانەیان هەبێت
x^3 in a handle that is also plottedx^3 لە کە دەکێشرێتیشworks for one number, fails on vectors and fplotبۆ یەک ژمارە کار دەکات، لەسەر و fplot سەرناکەوێتx.^3x.^3
A while loop with no cap while بێ سنوورan oscillating Newton runs for ever لەرەوە بۆ هەمیشە کار دەکاتadd && steps < max_steps&& steps < max_steps زیاد بکە
A tolerance far below what doubles allow (1e-20)سنووری هەڵەی ڕێگەپێدراو زۆر لە خوار توانای double ⁦(1e-20)⁩the condition can never be met هەرگیز بەدی نایەتuse about 1e-6 … 1e-12, and keep the capنزیکەی ⁦1e-6⁩ … ⁦1e-12⁩ بەکار بهێنە و سنوورەکە بهێڵەوە
Trusting a 'root' without checkingمتمانەکردن بە «ڕەگ»ێک بەبێ پشکنینa wrong answer goes unnoticedوەڵامێکی هەڵە دەرناکەوێتcheck that f(r) is close to 0بپشکنە کە f(r) نزیکە لە 0
Newton started where f′=0f' = 0, or far away لە شوێنێک کە f′=0f' = 0، یان دوور دەست پێکراوەdivision by zero, or another rootدابەشکردن بە سفر، یان ڕەگێکی ترstart near the root (graph or a few bisection steps)لە نزیک ڕەگەکەوە دەست پێبکە ( یان چەند هەنگاوێکی )

More practiceڕاهێنانی زیاتر

Exerciseڕاهێنان1. Where e−xe^{-x} meets xx1. شوێنی یەکگرتنی e−xe^{-x} و xx

Solve e−x=xe^{-x}=x with my_bisection on [0, 1] and tol = 1e-6. Write the handle first — what must f(x)f(x) be? Check with fzero.

e−x=xe^{-x}=x بە my_bisection لەسەر ⁦[0, 1]⁩ و ⁦tol = 1e-6⁩ شیکار بکە. سەرەتا بنووسە — f(x)f(x) دەبێت چی بێت؟ بە fzero بیپشکنە.

Show answerوەڵامەکە پیشان بدە
MATLABType in a terminalلە تێرمیناڵدا بینووسە
f = @(x) exp(-x) - x;          % e^(-x) = x  means  f(x) = 0
[r, n] = my_bisection(f, 0, 1, 1e-6)
fzero(f, [0 1])

Move everything to one side: f(x)=e−x−xf(x) = e^{-x} - x. f(0)=1>0f(0) = 1 > 0 and f(1)≈−0.63<0f(1) \approx -0.63 < 0, so [0, 1] brackets the root 0.567143.

هەموو شتێک بگوازەرەوە بۆ یەک لا: f(x)=e−x−xf(x) = e^{-x} - x. f(0)=1>0f(0) = 1 > 0 و f(1)≈−0.63<0f(1) \approx -0.63 < 0، بۆیە ⁦[0, 1]⁩ ڕەگی ⁦0.567143⁩ لەخۆ دەگرێت.

Exerciseڕاهێنان2. A square root by Newton2. ڕەگێکی دووجا بە ڕێگەی نیوتن

5\sqrt{5} is the positive root of x2−5=0x^2 - 5 = 0. Use my_newton with f′(x)=2xf'(x) = 2x from x0=2x_0 = 2. Write down the first three iterates and compare with sqrt(5).

5\sqrt{5} ڕەگە موجەبەکەی x2−5=0x^2 - 5 = 0ـە. my_newton بە f′(x)=2xf'(x) = 2x لە x0=2x_0 = 2ـەوە بەکار بهێنە. سێ مەزەندەی یەکەم بنووسە و لەگەڵ sqrt(5) بەراوردیان بکە.

Show answerوەڵامەکە پیشان بدە
MATLABType in a terminalلە تێرمیناڵدا بینووسە
format long
[x, n] = my_newton(@(x) x.^2 - 5, @(x) 2*x, 2, 1e-12)
sqrt(5)
format short

The iterates are 2.25, 2.236111, 2.2360680 and then 2.2360679775 — Newton's method for x2−ax^2-a is the ancient Babylonian (Heron's) rule for square roots: x←12(x+ax)x \leftarrow \tfrac{1}{2}\left(x+\tfrac{a}{x}\right).

مەزەندەکان بریتین لە ⁦2.25⁩، ⁦2.236111⁩، ⁦2.2360680⁩ و پاشان ⁦2.2360679775⁩ — بۆ x2−ax^2-a هەمان یاسا کۆنەکەی بابلییەکان (یاسای هیرۆن)ـە بۆ : x←12(x+ax)x \leftarrow \tfrac{1}{2}\left(x+\tfrac{a}{x}\right).

Exerciseڕاهێنان3. Predict the number of halvings3. ژمارەی نیوەکردنەکان پێشبینی بکە

Without running anything: how many halvings does my_bisection need on [0, 1] with tol = 1e-8?

بەبێ جێبەجێکردنی هیچ شتێک: my_bisection لەسەر ⁦[0, 1]⁩ بە ⁦tol = 1e-8⁩ پێویستی بە چەند نیوەکردن هەیە؟

Show answerوەڵامەکە پیشان بدە
n≥log⁡2110−8−1≈26.6−1=25.6  ⇒  n=26n \ge \log_2\frac{1}{10^{-8}}-1 \approx 26.6-1 = 25.6 \;\Rightarrow\; n = 26

26 halvings (a test run with Python agrees). Each extra decimal digit costs about 3.3 halvings.

⁦26⁩ نیوەکردن (جێبەجێکردنێکی تاقیکاری بە Python هەمان دەڵێت). هەر دەیی زیاتر نزیکەی ⁦3.3⁩ نیوەکردنی تێدەچێت.

Summaryپوختە

  • Look first: a graph or a sign table shows where roots are. A sign change of a continuous f on [a, b] guarantees a root (the Intermediate Value Theorem), but not where, and not how many.
  • Bisection halves the bracket and keeps the half with the sign change: safe, one binary digit per step, (b−a)/2n(b - a)/2^n after n steps.
  • Newton's method follows the tangent: x←x−f(x)/f′(x)x \leftarrow x - f(x)/f'(x); very fast near the root, but it can fail (f′ = 0, a far start, oscillation).
  • Write methods as function files with a tolerance, an iteration cap and an error for bad input.
  • Check with fzero(f, [a b]), roots(p) and polyval; today's names are fminbnd/fminsearch and integral (not fmin/fmins, quad).
  • For systems Ax=bAx = b use A\b; LU, Cholesky, Jacobi and Gauss–Seidel are what happens inside such tools.
  • سەرەتا سەیر بکە: یان خشتەیەکی هێما پیشان دەدات ڕەگەکان لە کوێن. گۆڕانی هێمای fـێکی بەردەوام لەسەر ⁦[a, b]⁩ ڕەگێک مسۆگەر دەکات ()، بەڵام نەک لە کوێ و نەک چەند.
  • کەوانەکە نیوە دەکات و ئەو نیوەیە دەهێڵێتەوە کە هێمای تێدا دەگۆڕێت: سەلامەت، هەر هەنگاوێک یەک ، دوای n هەنگاو (b−a)/2n(b - a)/2^n.
  • شوێن دەکەوێت: x←x−f(x)/f′(x)x \leftarrow x - f(x)/f'(x)؛ لە نزیک ڕەگەکە زۆر خێرایە، بەڵام دەشێت سەرنەکەوێت (f′=0f' = 0، دەستپێکی دوور، لەرینەوە).
  • ڕێگاکان وەک بنووسە بە سنووری هەڵەی ڕێگەپێدراو، سنووری و error بۆ خراپ.
  • بە fzero(f, [a b])، roots(p) و polyval بپشکنە؛ ناوەکانی ئەمڕۆ fminbnd/fminsearch و integralـن (نەک fmin/fmins، quad).
  • بۆ سیستەمی Ax=bAx = b A\b بەکار بهێنە؛ LU، چۆلێسکی، یاکۆبی و گاوس–زایدل ئەوەن کە لەناو ئەو ئامرازانەدا ڕوو دەدەن.
Review questionsپرسیارەکانی پێداچوونەوە
Why can bisection never fail once the sign test passes?بۆچی ڕێگەی نیوەکردن هەرگیز سەرناکەوێت نابێت دوای ئەوەی تاقیکردنەوەی هێما سەرکەوت؟

Every step keeps a bracket with a sign change, so by the Intermediate Value Theorem the bracket always contains a root of a continuous f, and its width halves each time.

هەر هەنگاوێک کەوانەیەک بە گۆڕانی هێماوە دەهێڵێتەوە، بۆیە بە پێی کەوانەکە هەمیشە ڕەگێکی fـی بەردەوام لەخۆ دەگرێت و پانییەکەی هەموو جارێک نیوە دەبێت.

When would you still choose bisection over Newton?کەی هێشتا ڕێگەی نیوەکردن لە ڕێگەی نیوتن هەڵدەبژێریت؟

When f′f' is unknown or hard to compute, or when you need a guaranteed result; a few bisection steps are also a good way to find a start for Newton.

کاتێک f′f' نەزانراوە یان هەژمارکردنی قورسە، یان کاتێک پێویستت بە ئەنجامێکی مسۆگەرە؛ چەند هەنگاوێکی هەروەها ڕێگایەکی باشە بۆ دۆزینەوەی دەستپێکێک بۆ .

What does roots([1 0 -3 -1]) return?roots([1 0 -3 -1]) چی دەگەڕێنێتەوە؟

All three roots of x3−3x−1x^3 - 3x - 1 as a column: 1.8794, −1.5321-1.5321 and −0.3473 (the order may differ).

هەر سێ ڕەگی x3−3x−1x^3 - 3x - 1 وەک ستوونێک: ⁦1.8794⁩، −1.5321-1.5321 و ⁦−0.3473⁩ (لەوانەیە ڕیزبەندییەکە جیاواز بێت).

Lesson complete!وانەکە تەواو بوو!

Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.

Key termsزاراوە سەرەکییەکان

References and further readingسەرچاوەکان و خوێندنەوەی زیاتر

This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.