MATLABMATLAB · Chapterبەش 6 · Lectureوانە 6.3
Project: Finding Roots Numericallyپڕۆژە: دۆزینەوەی ڕەگ بە ڕێگە ژمارەییەکان
An advanced project: solve 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.
پڕۆژەیەکی پێشکەوتوو: شیکار بکە کاتێک هیچ فۆرموولەیەک یارمەتی نادات. سەیری و هێماکان بکە، ببینە بۆچی گۆڕانی هێما ڕەگێک مسۆگەر دەکات ()، و وەک MATLAB بە سنووری هەڵەی ڕێگەپێدراو و سنووری دروست بکە، ببینە کەی سەرناکەوێت، وەڵامەکان بە fzero، roots و polyval بپشکنە، ئامرازی زیاتر بناسە (fminbnd، fminsearch، integral، polyfit، interp1 …) و ڕێگاکانی تری ڕێبەرەکە و هەموو شتێک لە پڕۆژەیەکی بچووکدا بەراورد بکە.
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 when no formula helpsکێشەکە: شیکار بکە کاتێک هیچ فۆرموولەیەک یارمەتی نادات
For there is a formula (the solver). For most other equations there is none you would want to use: (the course guide's example), , . 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.
بۆ فۆرموولەیەک هەیە (چارەسەرکەرەکە). بۆ زۆربەی تر هیچ فۆرموولەیەکی بەکەڵک نییە: (نموونەی ڕێبەری کۆرسەکە)، ، . وەڵامەکە بە شێوەیەکی تر دەدۆزنەوە: لە مەزەندەیەکی نزیکەوە دەست پێدەکەن و هەنگاو بە هەنگاو باشتری دەکەن تا بەس ورد دەبێت. ئەم وانە پڕۆژەییە دوو ڕێگەی لەو جۆرە وەک MATLAB دروست دەکات، پاشان بە ئامرازەکانی خودی MATLAB دەیانپشکنێت. هەموو لە MATLAB، MATLAB Online (وەشانی بەخۆڕایی) و GNU Octaveـدا کار دەکات.
A root of f is a number r with — 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 ژمارەیەکی ـە کە — ئەو شوێنەی x دەبڕێت. هەمیشە پێش هەژمارکردن سەیر بکە: f بکێشە (fplot) یان خشتەیەکی بچووک دروست بکە و سەیری هێماکان بکە.
f = @(x) x.^3 - 3*x - 1;
x = -2:0.5:2;
[x' f(x)']ans =
-2.0000 -3.0000
-1.5000 0.1250
-1.0000 1.0000
-0.5000 0.3750
0 -1.0000
0.5000 -2.3750
1.0000 -3.0000
1.5000 -2.1250
2.0000 1.0000Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
'turns both rows into columns, so each output line is one pair: x and . The sign changes between and , between and 0, and between 1.5 and 2 — three roots.'هەردوو ڕیزەکە دەکات بە ستوون، بۆیە هەر دێڕێکی جووتێکە: x و . هێما لە نێوان و ، لە نێوان و 0 و لە نێوان 1.5 و 2دا دەگۆڕێت — سێ ڕەگ.
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.
لە یارییەکەدا وەڵامەکە و ڕێک دەیدۆزیتەوە. ڕەگ زۆرجار ژمارەیەکی دەیی بێکۆتایە، بۆیە ڕێگاکە کاتێک دەوەستێت کە بەس بچووک بێت، نەک کاتێک ڕێک بێت.
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 and . When and have opposite signs, 0 lies between them, so for some c in [a, b].
ئەگەر چەماوەیەک بەبێ هەڵگرتنی قەڵەم لە خاڵێکی خوار x بۆ خاڵێکی سەرووی بکێشیت، قەڵەمەکە دەبێت لە شوێنێکدا ببڕێت. بیرکارزانان بەم جۆرە دەڵێن و ئەم ڕاستییەش ـە: ئەگەر f لەسەر [a, b] بەردەوام بێت، هەموو لە نێوان و دا وەردەگرێت. کاتێک و هێمای پێچەوانەیان هەیە، 0 لە نێوانیاندایە، بۆیە بۆ cـێک لە [a, b]ـدا.
The test MATLAB code uses: a negative product means opposite signs. OpenStax's calculus book shows it for on : and , so a root exists there.
ئەو تاقیکردنەوەیەی کۆدی MATLAB بەکاری دەهێنێت: لێکدانی سالب واتە هێمای پێچەوانە. کتێبی ژمێرکاریی OpenStax بۆ لەسەر پیشانی دەدات: و ، بۆیە ڕەگێک لەوێدا هەیە.
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 at and 1 at 1, but it jumps at 0 and has no root. (3) No sign change does not prove there is no root: on is positive at both ends yet touches 0 at , and an interval may also hide several roots — holds all three roots of the cubic.
سێ ئاگاداری. (1) دەڵێت ڕەگێک هەیە، نەک لە کوێیە — دۆزینەوەی کاری ئەو ڕێگایانەیە کە لە خوارەوەن. (2) بەردەوامی گرنگە: 1/x لە دا ـە و لە 1دا 1ـە، بەڵام لە 0دا باز دەدات و ڕەگی نییە. (3) نەبوونی گۆڕانی هێما ئەوە ناسەلمێنێت کە ڕەگ نییە: لەسەر لە هەردوو سەردا موجەبە، بەڵام لە دا بەر 0 دەکەوێت و دەشێت چەند ڕەگێکیش بشارێتەوە — هەر سێ ڕەگی لەخۆ دەگرێت.
The bisection methodڕێگەی نیوەکردن
The keeps a bracket [a, b] with a sign change and halves it again and again:
کەوانەیەکی [a, b] بە گۆڕانی هێماوە دەهێڵێتەوە و دووبارە و دووبارە نیوەی دەکات:
- Midpointخاڵی ناوەڕاست
Compute and .
و هەژمار بکە.
- Choose the halfنیوەکە هەڵبژێرە
If the sign changes in [a, p], so set ; otherwise it changes in [p, b], so set .
ئەگەر هێما لە [a, p]ـدا دەگۆڕێت، بۆیە دابنێ؛ ئەگینا لە [p, b]ـدا دەگۆڕێت، بۆیە دابنێ.
- Repeatدووبارەکردنەوە
Stop when the bracket is small enough; the midpoint of the last bracket is the answer.
کاتێک کەوانەکە بەس بچووک بوو بوەستە؛ خاڵی ناوەڕاستی دوایین کەوانە وەڵامەکەیە.
n | a | b | p | f(p) | kept halfنیوەی هێڵراوە |
|---|---|---|---|---|---|
| 1 | 2 | 0 | [p, b] | ||
| 2 | 0 | 2 | 1 | [p, b] | |
| 3 | 1 | 2 | 1.5 | [p, b] | |
| 4 | 1.5 | 2 | 1.75 | [p, b] | |
| 5 | 1.75 | 2 | 1.875 | [p, b] | |
| 6 | 1.875 | 2 | 1.9375 | 0.4607 | [a, p] |
| 7 | 1.875 | 1.9375 | 1.90625 | 0.2082 | [a, p] |
| 8 | 1.875 | 1.90625 | 1.890625 | 0.0861 | [a, p] |
The interval 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 or , start from or . Bisection finds one root per bracket.
سێ ڕەگی تێدایە، بەڵام تەنها شوێنی یەکێکیان دەکەوێت: یاساکە نیوەیەک بە گۆڕانی هێماوە دەهێڵێتەوە و لێرەدا ئەوە دەگاتە 1.8794. بۆ دۆزینەوەی یان ، لە یان ـەوە دەست پێبکە. بۆ هەر کەوانەیەک یەک ڕەگ دەدۆزێتەوە.
The bisection methodڕێگەی نیوەکردن
How many halvings?چەند جار نیوەکردن؟
Each halving cuts the bracket in two, so after n halvings its width is
هەر نیوەکردنێک کەوانەکە دەکاتە دوو بەش، بۆیە دوای n جار نیوەکردن پانییەکەی دەبێتە
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 , about three more decimal digits.
خاڵی ناوەڕاست زۆرترین نیوەی پانییەک لە ڕەگی r دوورە. هەر نیوەکردنێک نزیکەی یەک زیاتر دەدات؛ دە نیوەکردن پانییەکە دابەشی دەکات، نزیکەی سێ دەیی زیاتر.
To be within tol of the root you need , so — the same halving logarithm as in binary search. For and tol = 0.0001: , so 15 halvings.
بۆ ئەوەی هەڵەکە لە tol زیاتر نەبێت پێویستت بە هەیە، کەواتە — هەمان نیوەکردن وەک لە گەڕانی دووانی. بۆ و tol = 0.0001: ، کەواتە 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.](/_astro/matlab-numerical-methods-bisection-steps.DOkSQMMt.gif)
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) بە سنوورێک بۆ ژمارەی هەنگاوەکان؛ و دوو ، ڕەگەکە و ژمارەی هەنگاوەکان.
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;
endExplain 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.
سێ حاڵەتەکەی یەک نیوەکردن.
f = @(x) x.^3 - 3*x - 1;
[r, n] = my_bisection(f, -2, 2, 1e-6)
f(r)r =
1.8794
n =
21
ans =
5.3676e-06Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
21 halvings bring down to a width of .
21 نیوەکردن بۆ پانیی دادەبەزێنێت.
- Lineدێڕی 3
Always check: is close to 0, not exactly 0.
هەمیشە بپشکنە: نزیکە لە 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 on it returns , which is still 0.13 away from the root. A tolerance says what you really want.
- Typos that break or confuse.
ferintfshould befprintf(an error the moment that line runs); the messages say 'intevral', 'contaion' and 'no soll'; and the last lineEnd(capital E) does not close the outerif— 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^3in the handle and a missing;.@(x) x^3-3*x-1works for one number but not for vectors orfplot; the 'Section Method' copy also prints p at every step becausep=(a+b)/2has no semicolon.
- ژمارەیەکی جێگیری نیوەکردن. n وەردەگرێت و ڕێک n جار نیوە دەکات: بە لەسەر ، دەگەڕێنێتەوە کە هێشتا 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نییە.
Newton's method: follow the tangentڕێگەی نیوتن: شوێن هێڵە بەرکەوتووەکە بکەوە
Bisection is safe but slow: one binary digit per step. is usually much faster. From a guess , draw the to the curve at and see where it meets the x-axis; that point is the next guess. The tangent's slope is the , which gives the formula
سەلامەتە بەڵام هێواشە: هەر هەنگاوێک یەک . زۆرجار زۆر خێراترە. لە مەزەندەیەکی ـەوە، بۆ چەماوەکە لە ـدا بکێشە و سەیر بکە لە کوێ x دەبڕێت؛ ئەو خاڵە مەزەندەی داهاتووە. بریتییە لە f لە ـدا، واتە ، کە ئەم فۆرموولەیە دەدات
CêIT does not teach derivatives yet, so here is given: for it is . For any polynomial MATLAB can find the coefficients of the derivative: polyder([1 0 -3 -1]) gives 3 0 -3.
CêIT هێشتا فێر ناکات، بۆیە لێرەدا دراوە: بۆ بریتییە لە . بۆ هەر MATLAB دەتوانێت بدۆزێتەوە: polyder([1 0 -3 -1]) ئەمە دەدات 3 0 -3.
n | x_n | correct digits دروست |
|---|---|---|
| 0 | 2 | 0 |
| 1 | 1.888889 | 2 |
| 2 | 1.879452 | 4 |
| 3 | 1.879385245 | 9 |
| 4 | 1.879385241571817 | 16 |
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:
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
endExplain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 6
Stop when the last step was tiny, or after 50 steps.
کاتێک دوایین هەنگاو زۆر بچووک بوو بوەستە، یان دوای 50 هەنگاو.
- Lineدێڕی 7-9
A horizontal tangent never meets the axis.
ئاسۆیی هەرگیز نابڕێت.
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 shortx =
1.879385241571817
n =
5In 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 شکستە باوەکان ڕیز دەکات؛ ئەمانەن بە ژمارەکانی ئەم وانەیە:
| Problemکێشە | Exampleنموونە | What happensچی ڕوو دەدات |
|---|---|---|
| سفربوونی | from لە ـەوە | the tangent is horizontal: division by zero; my_newton stops with its error ئاسۆییە: دابەشکردن بە سفر؛ my_newton بە دەوەستێت |
| A start far from the wanted rootدەستپێکێک دوور لە ڕەگی ویستراو | from لە ـەوە | it jumps to and ends at the other root باز دەدات بۆ و لە ڕەگەکەی تر کۆتایی دێت |
| Oscillationلەرینەوە | from لە ـەوە | 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 is about ; Newton reaches it from .
دوو ئامۆژگاریی بەکەڵک لێرەوە دێن: لە نزیک ڕەگەکەوە دەست پێبکە (چەند هەنگاوێکی یان ـێکی باش دەدەن) و هەمیشە سنوورێک بۆ ژمارەی دابنێ. ڕەگی ڕاستیی نزیکەی ـە؛ لە ـەوە دەیگاتێ.
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.
مەترسیی یەکەم: ئاسۆیی لە خاڵێکی وەستاودا؛ مەترسیی دووەم: یەکەم مەزەندە لە لای هەڵەی خاڵێکی وەستاو؛ مەترسیی سێیەم: کە لە دەست پێدەکات و مەزەندەکان لە نێوان 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 — 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 بە ڕیزبەندیی دابەزینی وەردەگرێت — دەبێتە [1 0 -3 -1] — و هەموو ڕەگەکان دەگەڕێنێتەوە، ڕەگە ئاوێتەکانیشەوە؛ polyval هەژمار دەکات بۆ پشکنینیان.
f = @(x) x.^3 - 3*x - 1;
fzero(f, [1 2])
fzero(f, [-2 -1])
r = roots([1 0 -3 -1])ans =
1.8794
ans =
-1.5321
r =
1.8794
-1.5321
-0.3473Explain 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 at 0 (there is no sign change), and roots shows complex roots as complex numbers: roots([1 0 0 -8]) gives 2 and , the three cube roots of 8.
پاشان polyval([1 0 -3 -1], r) سێ ژمارە لە قەبارەی 1e-15 دەدات — سفر تا ئاستی . دوو ڕاستیی تر لە پەڕەکانی MathWorks: fzero ناتوانێت ڕەگێک بدۆزێتەوە کە چەماوەکە تەنها بەر سفر دەکەوێت، وەک لە 0دا (گۆڕانی هێما نییە) و roots ڕەگە ئاوێتەکان وەک پیشان دەدات: roots([1 0 0 -8]) 2 و دەدات، سێ 8.
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ـدا هەن:
| Command | What 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 / deconv | multiply / 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 / interp2 | values between data points (1-D / 2-D) نێوان خاڵەکانی (یەک / دوو ڕەهەند) | interp1([1 2 3], [10 20 15], 2.5) → 17.5000 |
spline / unmkpp | a smooth curve through the points / its piecesچەماوەیەکی لووس بە ناو خاڵەکاندا / پارچەکانی | see the documentationبڕوانە بەڵگەنامەکان |
fzero | a 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 , where . trapz gives 2.75 instead of the exact area because it joins only five points with straight lines — more points, smaller error, just as with plot.
دوو لە ئەنجامەکان شایەنی سەیرکردنی دووبارەن. fminbnd(f, 0, 2) بنی دۆڵەکەی لە دا دەدۆزێتەوە، کە . trapz لە جیاتی ڕووبەری ڕێک ، 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.
ڕێبەری کۆرسەکە زیاتر لەگەڵ کۆد ڕیز دەکات. لە کۆرسێکی شیکاریی ژمارەییدا دەیانبینیت؛ ئەمە ئەوەیە کە هەریەکەیان بۆ چییە.
| Methodڕێگا | Problemکێشە | Idea in one sentenceبیرۆکە بە یەک ڕستە |
|---|---|---|
| False position (regula falsi)شوێنی هەڵە (regula falsi) | a root of ڕەگی | like bisection, but the new point is where the straight line from to crosses the axisوەک ، بەڵام خاڵی نوێ ئەو شوێنەیە کە هێڵی ڕاست لە ـەوە بۆ دەبڕێت |
| Secant methodڕێگەی بڕەر | a root of ڕەگی | like Newton, but the tangent is replaced by the line through the last two guesses, so no is neededوەک ، بەڵام بە هێڵی نێوان دوو دوایین مەزەندە دەگۆڕدرێت، بۆیە پێویست بە ناکات |
| Fixed-point iterationدووبارەکردنەوەی خاڵی جێگیر | شیکارکردنی | repeat ; it settles on a solution when g changes slowly enough دووبارە بکەرەوە؛ کاتێک g بەس هێواش دەگۆڕێت لەسەر شیکارێک جێگیر دەبێت |
| Back substitutionجێگرتنەوەی دواوە | with U upper triangular کە U سێگۆشەیی سەرەوەیە | solve the last equation first, then work upwardsیەکەم جار دوایین شیکار بکە، پاشان بەرەو سەرەوە بڕۆ |
| LU factorisation (Doolittle)شیتەڵکردنی LU (دووڵیتڵ) | , many right-hand sides، زۆر لای ڕاستی جیاواز | write (lower × upper triangular), then solve two triangular systems; MATLAB: [L, U, P] = lu(A) بنووسە (سێگۆشەیی خوارەوە × سەرەوە)، پاشان دوو سیستەمی سێگۆشەیی شیکار بکە؛ MATLAB: [L, U, P] = lu(A) |
| Cholesky factorisationشیتەڵکردنی چۆلێسکی | , A symmetric and positive definite، A و ئەرێنیی دیاریکراو | write with one triangular R, about twice as fast as LU; MATLAB: chol(A) بنووسە بە یەک Rـی سێگۆشەیی، نزیکەی دوو هێندە خێراتر لە LU؛ MATLAB: chol(A) |
| Jacobi iterationدووبارەکردنەوەی یاکۆبی | , large systems، سیستەمی گەورە | improve every unknown from the previous guess of all the othersهەموو نەزانراوێک لە مەزەندەی پێشووی هەموو ئەوانی ترەوە باشتر بکە |
| Gauss–Seidel iterationدووبارەکردنەوەی گاوس–زایدل | , large systems، سیستەمی گەورە | like Jacobi, but each new value is used at once, usually converging fasterوەک یاکۆبی، بەڵام هەر نوێ یەکسەر بەکار دێت و زۆرجار خێراتر نزیک دەبێتەوە |
For a system 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.
بۆ سیستەمێکی لە MATLABـی ڕۆژانەدا بە سادەیی دەنووسیت x = A\b (دابەشکردنی چەپ)؛ MATLAB خۆی شیتەڵکردنێکی گونجاو هەڵدەبژێرێت. ڕێگا دەستییەکەی پشتییەوە، ، لە پێچەوانە بە گاوس–جۆرداندایە و بە گشتی لە پێچەوانەی ماتریکس.
Project: compare the methodsپڕۆژە: ڕێگاکان بەراورد بکە
Put everything together. Your tasks:
هەموو شتێک پێکەوە دابنێ. ئەرکەکانت:
- Save
my_bisection.mandmy_newton.mfrom this lesson in one folder. - Test them on three functions: on [1, 2]; on ; and on , which has no sign change (and no real root).
- Compare with
fzeroand report a small table: method, root, steps. - Plot each function with its root marked by a red circle.
my_bisection.mوmy_newton.mـی ئەم وانەیە لە یەک هەڵبگرە.- لەسەر سێ تاقییان بکەرەوە: لەسەر [1, 2]؛ لەسەر ؛ و لەسەر کە گۆڕانی هێمای نییە (و ڕەگی ڕاستیی نییە).
- بەراوردی بکە لەگەڵ
fzeroو خشتەیەکی بچووک ڕاپۆرت بکە: ڕێگا، ڕەگ، هەنگاو. - هەر بە ڕەگەکەیەوە بکێشە کە بە بازنەیەکی سوور نیشان کراوە.
Write the script, run it, and explain each line of output.
بنووسە، جێبەجێی بکە و هەر دێڕێکی ڕوون بکەرەوە.
Show answerوەڵامەکە پیشان بدە
% 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 changef1: bisection 1.879386 (19 steps), Newton 1.879385 (6 steps), fzero 1.879385 f2: bisection 0.739086 (20 steps), Newton 0.739085 (4 steps), fzero 0.739085 Error using my_bisection (line 4) f(a) and f(b) must have opposite signs.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 5,7
Newton starts from the middle of each bracket.
لە ناوەڕاستی هەر کەوانەیەکەوە دەست پێدەکات.
- Lineدێڕی 10
hold onkeeps the curve while the root is added as a red circle.hold onچەماوەکە دەهێڵێتەوە لە کاتێکدا ڕەگەکە وەک بازنەیەکی سوور زیاد دەکرێت.- Lineدێڕی 11
The last call stops the script on purpose: is positive at both ends.
دوایین بانگکردن بە مەبەست دەوەستێنێت: لە هەردوو سەردا موجەبە.
| Function | Bisection (tol 1e-6) (tol 1e-6) | Newton (tol 1e-10) (tol 1e-10) | fzero |
|---|---|---|---|
x^3 - 3x - 1 | 1.879386 (19) | 1.879385 (6) | 1.879385 |
x - cos(x) | 0.739086 (20) | 0.739085 (4) | 0.739085 |
x^2 + 1 | error: 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هەڵە باوەکان
| 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 , or far away لە شوێنێک کە ، یان دوور دەست پێکراوە | division by zero, or another rootدابەشکردن بە سفر، یان ڕەگێکی تر | start near the root (graph or a few bisection steps)لە نزیک ڕەگەکەوە دەست پێبکە ( یان چەند هەنگاوێکی ) |
More practiceڕاهێنانی زیاتر
Solve with my_bisection on [0, 1] and tol = 1e-6. Write the handle first — what must be? Check with fzero.
بە my_bisection لەسەر [0, 1] و tol = 1e-6 شیکار بکە. سەرەتا بنووسە — دەبێت چی بێت؟ بە fzero بیپشکنە.
Show answerوەڵامەکە پیشان بدە
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])r =
0.5671
n =
19
ans =
0.5671Move everything to one side: . and , so [0, 1] brackets the root 0.567143.
هەموو شتێک بگوازەرەوە بۆ یەک لا: . و ، بۆیە [0, 1] ڕەگی 0.567143 لەخۆ دەگرێت.
is the positive root of . Use my_newton with from . Write down the first three iterates and compare with sqrt(5).
ڕەگە موجەبەکەی ـە. my_newton بە لە ـەوە بەکار بهێنە. سێ مەزەندەی یەکەم بنووسە و لەگەڵ sqrt(5) بەراوردیان بکە.
Show answerوەڵامەکە پیشان بدە
format long
[x, n] = my_newton(@(x) x.^2 - 5, @(x) 2*x, 2, 1e-12)
sqrt(5)
format shortx =
2.236067977499790
n =
5
ans =
2.236067977499790The iterates are 2.25, 2.236111, 2.2360680 and then 2.2360679775 — Newton's method for is the ancient Babylonian (Heron's) rule for square roots: .
مەزەندەکان بریتین لە 2.25، 2.236111، 2.2360680 و پاشان 2.2360679775 — بۆ هەمان یاسا کۆنەکەی بابلییەکان (یاسای هیرۆن)ـە بۆ : .
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وەڵامەکە پیشان بدە
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, after n steps.
- Newton's method follows the tangent: ; 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
errorfor bad input. - Check with
fzero(f, [a b]),roots(p)andpolyval; today's names arefminbnd/fminsearchandintegral(notfmin/fmins,quad). - For systems use
A\b; LU, Cholesky, Jacobi and Gauss–Seidel are what happens inside such tools.
- سەرەتا سەیر بکە: یان خشتەیەکی هێما پیشان دەدات ڕەگەکان لە کوێن. گۆڕانی هێمای fـێکی بەردەوام لەسەر [a, b] ڕەگێک مسۆگەر دەکات ()، بەڵام نەک لە کوێ و نەک چەند.
- کەوانەکە نیوە دەکات و ئەو نیوەیە دەهێڵێتەوە کە هێمای تێدا دەگۆڕێت: سەلامەت، هەر هەنگاوێک یەک ، دوای n هەنگاو .
- شوێن دەکەوێت: ؛ لە نزیک ڕەگەکە زۆر خێرایە، بەڵام دەشێت سەرنەکەوێت (، دەستپێکی دوور، لەرینەوە).
- ڕێگاکان وەک بنووسە بە سنووری هەڵەی ڕێگەپێدراو، سنووری و
errorبۆ خراپ. - بە
fzero(f, [a b])،roots(p)وpolyvalبپشکنە؛ ناوەکانی ئەمڕۆfminbnd/fminsearchوintegralـن (نەکfmin/fmins،quad). - بۆ سیستەمی
A\bبەکار بهێنە؛ LU، چۆلێسکی، یاکۆبی و گاوس–زایدل ئەوەن کە لەناو ئەو ئامرازانەدا ڕوو دەدەن.
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 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.
کاتێک نەزانراوە یان هەژمارکردنی قورسە، یان کاتێک پێویستت بە ئەنجامێکی مسۆگەرە؛ چەند هەنگاوێکی هەروەها ڕێگایەکی باشە بۆ دۆزینەوەی دەستپێکێک بۆ .
What does roots([1 0 -3 -1]) return?roots([1 0 -3 -1]) چی دەگەڕێنێتەوە؟
All three roots of as a column: 1.8794, and −0.3473 (the order may differ).
هەر سێ ڕەگی وەک ستوونێک: 1.8794، و −0.3473 (لەوانەیە ڕیزبەندییەکە جیاواز بێت).
Lesson complete!وانەکە تەواو بوو!
Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.
Key termsزاراوە سەرەکییەکان
Related lecturesوانە پەیوەندیدارەکان
- Making Decisions: Relational Operators and if / elseif / elseبڕیاردان: کارپێکەرەکانی بەراوردکردن و if / elseif / else
- Matrix Operations vs Element-by-Element Operationsکارەکانی ماتریکس بەرامبەر کارەکانی توخم بە توخم
- Logarithms and Their Rulesلۆگاریتمەکان و یاساکانیان
- Quadratic Inequalities and the Sign Chartلاسەنگە پلەدووەکان و خشتەی هێماکان
- Finding the Inverse by Row Operations (Gauss–Jordan)دۆزینەوەی پێچەوانە بە کارە سەرەتاییەکانی ڕیز (لابردنی گاوس–جۆردان)
- The Inverse of a Matrixپێچەوانەی ماتریکس
References and further readingسەرچاوەکان و خوێندنەوەی زیاتر
- OpenStax Calculus Volume 1, 2.4 Continuity (Intermediate Value Theorem)
- OpenStax Calculus Volume 1, 4.9 Newton's Method
- MathWorks: fzero
- MathWorks: roots
- MathWorks: fminbnd
- MathWorks: fminsearch
- MathWorks: quad (not recommended, use integral)
- MathWorks: integral
- GNU Octave manual: Solvers (fzero)
- GNU Octave manual: Functions of One Variable (integral, quad, trapz)
This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.