MATLABMATLAB · Chapterبەش 5 · Lectureوانە 5.2
Making Decisions: Relational Operators and if / elseif / elseبڕیاردان: کارپێکەرەکانی بەراوردکردن و if / elseif / else
Compare values with == ~= < <= > >=, get logical arrays from whole vectors and pick elements with logical indexing, find and any/all, combine conditions with & | ~ xor and the short-circuit && ||, write if, if-else and if-elseif-else blocks (elseif is one word), and debug the lab's quadratic-equation solver.
بە == ~= < <= > >= بەراورد بکە، لە تەواو وەربگرە و بە ، find و any/all هەڵبژێرە، بە & | ~ xor و && ||ـی کورتبڕ پێکەوە ببەستە، بلۆکەکانی if، if-else و if-elseif-else بنووسە (elseif یەک وشەیە) و هەڵەکانی چارەسەرکەری تاقیگەکە بدۆزەرەوە.
What you will learnچی فێر دەبیت
- Use the six relational operators and read their logical answers.
- Compare whole vectors, select elements with logical indexing and use find, any, all, isnan, isinf, isfinite, isempty and isreal.
- Combine conditions with & | ~ xor and with the short-circuit && and ||.
- Write if, if-else, if-elseif-else and nested if blocks, and explain why elseif is one word.
- Find and fix the mistakes of a quadratic-equation solver with well-chosen test data.
- شەش بەکار بهێنیت و وەڵامە لۆجیکییەکانیان بخوێنیتەوە.
- تەواو بەراورد بکەیت، بە هەڵبژێریت و find، any، all، isnan، isinf، isfinite، isempty و isreal بەکار بهێنیت.
- بە & | ~ xor و بە && و ||ـی کورتبڕ پێکەوە ببەستیت.
- بلۆکەکانی if، if-else، if-elseif-else و بنووسیت و ڕوونی بکەیتەوە بۆچی elseif یەک وشەیە.
- هەڵەکانی چارەسەرکەرێکی بە باش هەڵبژێردراو بدۆزیتەوە و چاکیان بکەیتەوە.
Comparing values: relational operatorsبەراوردکردنی نرخەکان: کارپێکەرەکانی بەراوردکردن
A program decides by asking yes/no questions: is the mark at least 50? is the divisor zero? In MATLAB you ask with a (MathWorks calls them relational operators). The answer is a logical value: 1 for true, 0 for false. MATLAB shows it with the word logical above it.
بە پرسیاری بەڵێ/نەخێر بڕیار دەدات: ئایا نمرەکە لانیکەم 50ـە؟ ئایا دابەشکەرەکە سفرە؟ لە MATLABـدا بە دەپرسیت (MathWorks پێیان دەڵێت relational operators). وەڵامەکە لۆجیکییە: 1 بۆ ڕاست، 0 بۆ هەڵە. MATLAB بە وشەی logical لە سەرەوەی پیشانی دەدات.
| Operator | Asksدەپرسێت | Exampleنموونە | Answerوەڵام |
|---|---|---|---|
== | equal to?یەکسانە؟ | 7 == 7 | 1 |
~= | not equal to?یەکسان نییە؟ | 4 ~= 4 | 0 |
< | less than?بچووکترە؟ | 3 < 8 | 1 |
> | greater than?گەورەترە؟ | 3 > 8 | 0 |
<= | less than or equal to?بچووکترە یان یەکسانە؟ | 50 <= 50 | 1 |
>= | greater than or equal to?گەورەترە یان یەکسانە؟ | 49.5 >= 50 | 0 |
5 > 3
7 == 7.0
4 ~= 4ans = logical 1 ans = logical 1 ans = logical 0
Two warnings. First, = stores a value and == compares — x = 5 gives x the value 5, x == 5 asks a question (“=” in algebra and in programming). Second, not equal is ~= in MATLAB. Octave also accepts != (its manual lists both), but write ~= so your code runs in both programs.
دوو ئاگادارکردنەوە. یەکەم، = هەڵدەگرێت و == بەراورد دەکات — x = 5 5 دەدات بە x، x == 5 پرسیارێک دەکات («=» لە جەبر و لە بەرنامەسازیدا). دووەم، یەکسان نییە لە MATLABـدا ~=ـە. Octave !=ـیش قبووڵ دەکات (ڕێبەرەکەی هەردووکیان ناو دەبات)، بەڵام ~= بنووسە تاکو کۆدەکەت لە هەردوو بەرنامەکەدا کار بکات.
Comparing values: relational operatorsبەراوردکردنی نرخەکان: کارپێکەرەکانی بەراوردکردن
Comparing a whole vectorبەراوردکردنی ئاڕاستەبڕێکی تەواو
Because every value in MATLAB is an array, a comparison works element by element. v > 5 compares each element of v with 5 and gives a of the same size: a row of 1s and 0s, sometimes called a mask. Used as an index, that mask picks out the elements where it has a 1 — this is :
لەبەر ئەوەی هەموو لە MATLABـدا ، بەراوردکردن بە کار دەکات. v > 5 هەر v لەگەڵ 5 بەراورد دەکات و بە هەمان قەبارە دەدات: ڕیزێک لە 1 و 0، کە هەندێک جار پێی دەڵێن ماسک. ئەگەر وەک بەکار بێت، ئەو ماسکە ئەو هەڵدەبژێرێت کە 1ـی بەرامبەرە — ئەمە :
v = [3 8 1 9 6];
mask = v > 5
big = v(v > 5)
pos = find(v > 5)mask =
1×5 logical array
0 1 0 1 1
big =
8 9 6
pos =
2 4 5Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
v(mask)keeps the elements where the mask is 1.v(mask)ئەو دەهێڵێتەوە کە ماسکەکە بۆیان 1ـە.- Lineدێڕی 4
findgives the positions of the 1s instead of the values.findلە جیاتی شوێنی 1ـەکان دەدات.
any(v > 8)
all(v > 0)
count = sum(v > 5)
v(v > 5) = 0ans =
logical
1
ans =
logical
1
count =
3
v =
3 0 1 0 0Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
any: is at least one element true? (9 > 8, so yes.)any: ئایا لانیکەم یەک ڕاستە؟ (9 > 8، بۆیە بەڵێ.)- Lineدێڕی 2
all: are all elements true?all: ئایا هەموو ڕاستن؟- Lineدێڕی 3
Adding up a mask counts its 1s: 3 elements are above 5. (
sumis taught in functions for arrays.)کۆکردنەوەی ماسکێک 1ـەکانی دەژمێرێت: سێ لە 5 گەورەترن. (
sumلە کردارەکانی ڕیزەکدا دەخوێنرێت.)- Lineدێڕی 4
Logical indexing on the left of
=replaces the chosen elements.لە لای چەپی
=دەگۆڕێت.
The comparison needs two arrays of the same (or compatible) size, or an array and one number. Indexing itself — v(3), v(2:4), end — is taught in indexing.
بەراوردکردنەکە پێویستی بە دوو بە هەمان قەبارە (یان قەبارەی گونجاو) هەیە، یان و یەک ژمارە. خودی — v(3)، v(2:4)، end — لە وانەی ئیندێکسەکاندا دەخوێنرێت.
A MATLAB beginner course by Phil Parisi: the Command Window, variables, vectors and matrices, indexing, scripts, sections and naming rules, each in short parts.
کۆرسێکی سەرەتایی MATLAB لەلایەن Phil Parisi: Command Window، ، و ، ، ، بەشەکان و یاساکانی ناونان، هەر یەکەیان بە بەشێکی کورت.
Chapter 'Example 3 - Logic' (1:05:29-1:20:24): single comparisons such as 55 ~= 20 give logical 1 or 0, then y > 0.8 on a million sine values gives a logical array and sum counts its 1s (25.74 %).
بەشی Example 3 - Logic (لە 1:05:29 تا 1:20:24): بەراوردی تاک، وەک 55 ~= 20، ئەنجامی لۆجیکیی 1 یان 0 دەدات، پاشان y > 0.8 لەسەر ملیۆنێک دەدات و sum ژمارەی 1ـەکانی دەژمێرێت (25.74٪).
Comparing values: relational operatorsبەراوردکردنی نرخەکان: کارپێکەرەکانی بەراوردکردن
Test functions: isnan, isinf, isfinite, isempty, isrealکردارەکانی پشکنین: isnan، isinf، isfinite، isempty، isreal
Some questions cannot be asked with an operator. NaN (not a number, the result of 0/0) is not equal to anything — MathWorks notes that NaN is not even equal to another NaN — so x == NaN is always false. The commands list names test functions for such cases; each answers with 1 or 0 for every element:
هەندێک پرسیار بە ناکرێن. NaN (ژمارە نییە، ئەنجامی 0/0) یەکسان نییە بە هیچ شتێک — MathWorks دەڵێت NaN تەنانەت یەکسان نییە بە NaNـێکی تر — بۆیە x == NaN هەمیشە هەڵەیە. لیستی پشکنین بۆ ئەم حاڵەتانە ناو دەبات؛ هەریەکەیان بۆ هەر 1 یان 0 دەداتەوە:
| Function | True whenکەی ڕاستە |
|---|---|
isnan(x) | the element is NaN NaNـە |
isinf(x) | the element is Inf or -Inf Inf یان -Infـە |
isfinite(x) | the element is an ordinary number (not NaN, not ±Inf) ژمارەیەکی ئاساییە (نە NaN، نە ±Inf) |
isempty(x) | x has no elements at all, like []x هیچ نییە، وەک [] |
isreal(x) | x has no imaginary partx بەشی خەیاڵیی نییە |
x = [1 -Inf NaN 4];
isnan(x)
isinf(x)
isfinite(x)
isempty([])
isreal(3 + 4i)
NaN == NaNans = 1×4 logical array 0 0 1 0 ans = 1×4 logical array 0 1 0 0 ans = 1×4 logical array 1 0 0 1 ans = logical 1 ans = logical 0 ans = logical 0
Combining conditions: logical operatorsپێکەوەبەستنی مەرجەکان: کارپێکەرە لۆجیکییەکان
To ask two things at once, join comparisons with a . They follow the truth tables you know from logic: AND is true only when both sides are true (AND), OR when at least one is (OR), NOT turns true into false, and XOR is true when exactly one side is true.
بۆ پرسینی دوو شت بەیەکەوە، بەراوردەکان بە پێکەوە ببەستە. ئەمانە خشتەکانی ڕاستی دەگرنە بەر کە لە لۆجیکدا دەیانناسیت: AND تەنها کاتێک ڕاستە کە هەردوو لا ڕاست بن (AND)، OR کاتێک لانیکەم یەکێکیان ڕاست بێت (OR)، NOT ڕاست دەکات بە هەڵە و XOR کاتێک ڕاستە کە ڕێک یەک لا ڕاست بێت.
| Operator | Meaningواتا | Use it forبۆ چی بەکاری بهێنە |
|---|---|---|
& | AND, element by elementAND، بە | arrays and masks: v > 2 & v < 9 و ماسکەکان: v > 2 & v < 9 |
| | OR, element by elementOR، بە | arrays and masks و ماسکەکان |
~ | NOT: turns true into falseNOT: ڕاست دەکات بە هەڵە و هەڵە بە ڕاست | ~isempty(x), ~(a > b) |
xor(a, b) | exclusive OR (a function)XOR: ORـی تایبەت () | “one or the other, not both”«یەکێکیان، نەک هەردووکیان» |
&& | AND with short-circuitAND بە | conditions of if and while (single true/false values) if و while (یەک ڕاست/هەڵە) |
|| | OR with short-circuitOR بە | conditions of if and while if و while |
a = [1 0 1 0]; b = [1 1 0 0];
a & b
a | b
~a
xor(a, b)
x = 7;
x > 0 && x < 10ans = 1×4 logical array 1 0 0 0 ans = 1×4 logical array 1 1 1 0 ans = 1×4 logical array 0 1 0 1 ans = 1×4 logical array 0 1 1 0 ans = logical 1
Combining conditions: logical operatorsپێکەوەبەستنی مەرجەکان: کارپێکەرە لۆجیکییەکان
Short-circuit && and ||, and bitwise functions&& و ||ـی کورتبڕ، هەروەها کردارەکانی bitwise
Short-circuit means MATLAB stops as soon as the answer is known: in p && q, if p is false, q is never looked at. That makes tests like ~isempty(v) && v(1) > 0 safe — when v is empty, MATLAB never tries to read v(1). MathWorks recommends && and || in the conditions of if and while. Precedence matters too: comparisons come before &, & before |, and ~ binds tighter than a comparison, so write ~(a > b) with brackets.
واتە MATLAB هەر کە وەڵامەکە زانرا دەوەستێت: لە p && qـدا ئەگەر p هەڵە بێت، هەرگیز سەیری q ناکرێت. ئەمە پشکنینی وەک ~isempty(v) && v(1) > 0 سەلامەت دەکات — کاتێک v بەتاڵە، MATLAB هەرگیز هەوڵی خوێندنەوەی v(1) نادات. MathWorks && و || بۆ if و while پێشنیار دەکات. گرنگە: بەراوردەکان پێش & دێن و & پێش | دێت. هەروەها ~ لە بەراورد بەهێزترە، بۆیە ~(a > b) بە کەوانەوە بنووسە.
The Kurdish guide of the course also mentions bitwise operations. Those work on the binary digits of whole numbers and use functions: bitand(12, 10) is 8 (in binary ), bitor(12, 10) is 14 and bitxor(12, 10) is 6. They are not the same as & and |, which only ask true or false.
ڕێبەرە کوردییەکەی وانەکە باسی کارەکانی bitwiseـیش دەکات. ئەوانە لەسەر کار دەکەن و بەکار دەهێنن: bitand(12, 10) دەکاتە 8 (بە دوویی )، bitor(12, 10) دەکاتە 14 و bitxor(12, 10) دەکاتە 6. ئەمانە وەک & و | نین، کە تەنها ڕاست یان هەڵە دەپرسن.
if, else and elseifif، else و elseif
Choosing between paths is selection, one of the three building blocks of every algorithm; a flowchart draws it as a diamond. MATLAB writes it with an in three sizes. Each one ends with the keyword end:
هەڵبژاردنی یەک ڕێگا لە نێوان چەند ڕێگایەکدا پێی دەڵێن selection، یەکێک لە سێ بەردی بناغەی هەموو ؛ وەک چوارگۆشەیەکی لار (ئەڵماس) دەیکێشێت. MATLAB بە لە سێ قەبارەدا دەینووسێت. هەریەکەیان بە end کۆتایی دێت:
% 1) if ... end: do something or nothing
if mark >= 50
disp('Passed')
end
% 2) if ... else ... end: one of two paths
if mark >= 50
disp('Passed')
else
disp('Try again')
end
% 3) if ... elseif ... else ... end: one of several paths
if a < b
j = -1;
elseif a > b
j = 2;
else
j = 3;
endExplain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
The follows
ifdirectly; brackets around it are allowed but not needed.ڕاستەوخۆ دوای
ifدێت؛ کەوانە لە دەوری ڕێگەپێدراوە بەڵام پێویست نییە.- Lineدێڕی 14-20
MATLAB tests from the top and runs only the first block whose condition is true;
elsecatches everything else.MATLAB لە سەرەوە دەپشکنێت و تەنها یەکەم بلۆک جێبەجێ دەکات کە ڕاستە؛
elseهەموو ئەوانی تر دەگرێت.
a = 6; b = 6;
if a < b
j = -1;
elseif a > b
j = 2;
else
j = 3;
end
j
a = 6; b = 2;
if a < b
j = -1;
elseif a > b
j = 2;
end
jj =
3
j =
2Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1-9
a equals b, so both tests fail and
elsegives .a یەکسانە بە b، بۆیە هەردوو پشکنینەکە سەرناکەون و
elseئەنجامی دەدات.- Lineدێڕی 11-17
No
elsehere; a > b is true, so .لێرەدا
elseنییە؛ a > b ڕاستە، بۆیە .
Indentation (the spaces before j = -1;) is for people: MATLAB does not need it, but it shows at a glance which lines belong to which branch. The Editor indents for you — select the code and press Ctrl+I (Smart Indent). If you know Python: Python writes elif and uses indentation to end a block (elif); MATLAB writes elseif and ends with end.
(ئەو پێش j = -1;) بۆ خەڵکە: MATLAB پێویستی پێی نییە، بەڵام بە یەک سەیرکردن پیشان دەدات کام دێڕ سەر بە کام لقە. Editor خۆی بۆت دەیکات — کۆدەکە دیاری بکە و Ctrl+I دابگرە (Smart Indent). ئەگەر Python دەزانیت: Python elif دەنووسێت و بە بلۆک کۆتایی پێدەهێنێت (elif)؛ MATLAB elseif دەنووسێت و بە end کۆتایی پێدەهێنێت.
How if, else and elseif choose a path: each one is drawn as a flowchart first and then written and run in MATLAB.
if، else و elseif چۆن ڕێگەیەک هەڵدەبژێرن: هەر یەکەیان سەرەتا وەک دەکێشرێت، پاشان لە MATLABـدا دەنووسرێت و جێبەجێ دەکرێت.
Three parts: if (0:00), else (4:22), elseif (7:08); each with a flowchart and a MATLAB script.
سێ بەش: if (0:00)، else (4:22)، elseif (7:08)؛ هەر یەکەیان بە و MATLAB.
if, else and elseifif، else و elseif
elseif is one wordelseif یەک وشەیە
The course's Kurdish guide writes else if with a space and closes with a capital End. In MATLAB that is a different program. MathWorks warns that the space creates a nested if — a new if inside the else — and that nested if needs its own end. With only one end, MATLAB reports that an end is missing (a ), and End with a capital E is not a keyword at all.
ڕێبەرە کوردییەکەی وانەکە else if بە دەنووسێت و بە Endـی پیت گەورە دایدەخات. لە MATLABـدا ئەوە ترە. MathWorks ئاگادار دەکاتەوە کە دروست دەکات — ifـێکی نوێ لەناو elseـدا — و ئەو پێویستی بە endـی تایبەتی خۆی هەیە. بە تەنها یەک end، MATLAB ڕادەگەیەنێت کە endـێک کەمە (). هەروەها End بە Eـی گەورە هەرگیز نییە.
| As in the guide (fails)وەک ڕێبەرەکە (سەرناکەوێت) | Correct: one wordدروست: یەک وشە | Also correct: a real nested ifئەمەش دروستە: ڕاستەقینە |
|---|---|---|
if a < b
j = -1;
else if a > b
j = 2;
else
j = 3;
End | if a < b
j = -1;
elseif a > b
j = 2;
else
j = 3;
end | if a < b
j = -1;
else
if a > b
j = 2;
else
j = 3;
end
end |
if, else and elseifif، else و elseif
An if inside an ififـێک لەناو ifـدا
A is an if placed inside a branch of another one. The guide shows the idea in pseudocode: if FALSE then NO, else (if TRUE then YES_YES, else YES_NO). Here it is in MATLAB with two real conditions — the inner if has its own end:
ئەو ifـەیە کە لەناو لقێکی ifـێکی تردا دادەنرێت. ڕێبەرەکە بیرۆکەکە بە پیشان دەدات: ئەگەر هەڵە بوو NO، دەنا (ئەگەر ڕاست بوو YES_YES، دەنا YES_NO). ئەمە لە MATLABـدا بە دوو ڕاستەقینە — ifـی ناوەوە endـی خۆی هەیە:
if 2 > 3
disp('NO')
else
if 5 > 1
disp('YES_YES')
else
disp('YES_NO')
end
endYES_YES
A practical example: a cinema ticket for Dilan's family. Adults pay 6000 IQD, students 4000 IQD, children under 12 pay 2000 IQD. The first question splits adults from children; only adults are asked whether they study:
نموونەیەکی پراکتیکی: بلیتی سینەما بۆ خێزانەکەی دیلان. گەورەکان 6000 دینار دەدەن، قوتابیان 4000 دینار و منداڵانی خوار 12 ساڵ 2000 دینار. یەکەم پرسیار گەورە و منداڵ لێک جیا دەکاتەوە؛ تەنها لە گەورەکان دەپرسرێت کە ئایا دەخوێنن:
age = input('Age: ');
if age >= 12
student = input('Student? (y/n): ', 's');
if strcmp(student, 'y')
price = 4000;
else
price = 6000;
end
else
price = 2000;
end
fprintf('Ticket: %d IQD\n', price)Age: 19 Student? (y/n): y Ticket: 4000 IQD
Spot the mistakes: the quadratic-equation solverهەڵەکان بدۆزەرەوە: چارەسەرکەری هاوکێشەی پلەدوو
A lab of the course asks for a program that solves the . The plan is good: if the equation is only first-degree; otherwise compute the discriminant and decide — one root if , complex roots if , two real roots otherwise, with . Here is the lab's program, re-typed with its mistakes. Read it slowly before you look further:
تاقیگەیەکی وانەکە دەخوازێت کە چارەسەر بکات. پلانەکە باشە: ئەگەر بێت هاوکێشەکە تەنها پلەیەکە؛ دەنا (discriminant) هەژمار بکە و بڕیار بدە — یەک ڕەگ ئەگەر ، ڕەگی ئاوێتە ئەگەر ، دەنا دوو ڕەگی ڕاستی، بە . ئەمە تاقیگەکەیە، دووبارە نووسراوەتەوە لەگەڵ هەڵەکانی. پێش ئەوەی زیاتر بخوێنیتەوە، بە هێمنی بیخوێنەوە:
a = input('enter the value a=')
b = input('enter the value b=')
c = input('enter the value c=')
if (a ~= 0)
d = b^2 - 4*a*c;
if (d == 0)
x = -b/2*c
elseif (d < 0)
disp('complex case')
else
disp('we have three case')
x1 = (-b + sqrt(d))/(2*c)
x2 = (-b - sqrt(d))/(2*c)
end
else
x = -c/b
disp('first degree eq.')
endenter the value a=1
a =
1
enter the value b=-3
b =
-3
enter the value c=2
c =
2
we have three case
x1 =
1
x2 =
0.5000Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1-3
No semicolons, so MATLAB echoes
a,bandcafter each answer.نییە، بۆیە MATLAB دوای هەر وەڵامێک
a،bوcدەنووسێتەوە.
The test equation is , which factors as : the roots are 2 and 1. The program prints 1 and 0.5. Follow the run in the animation, then read the list of mistakes.
هاوکێشەی تاقیکردنەوەکە ـە، کە شیکار دەکرێت بۆ : ڕەگەکان 2 و 1ن. 1 و 0.5 چاپ دەکات. جێبەجێبوونەکە هەنگاو بە هەنگاو لە ئەنیمەیشنەکەدا ببینە، پاشان لیستی هەڵەکان بخوێنەوە.


Spot the mistakes: the quadratic-equation solverهەڵەکان بدۆزەرەوە: چارەسەرکەری هاوکێشەی پلەدوو
The five mistakesپێنج هەڵەکە
- Line 7:
-b/2*cis worked out left to right as . The repeated root is : write-b/(2*a). For , , the program prints 8 instead of 2. - Lines 12–13: the roots divide by
(2*c); the formula divides by(2*a). - Line 11: the message “we have three case” should say two real roots — a quadratic has at most two roots.
- Line 9: for it only prints “complex case”. MATLAB can give the complex roots directly:
sqrtof a negative number is an multiple (complex numbers). - Line 16: when and there is no equation to solve;
-c/bdivides by zero and prints-InforInf(orNaNwhen c is 0 too).
- دێڕی 7:
-b/2*cلە چەپەوە بۆ ڕاست وەک هەژمار دەکرێت. ڕەگە دووبارەکە ـە: بنووسە-b/(2*a). بۆ ، ، 8 چاپ دەکات لە جیاتی 2. - دێڕەکانی 12–13: ڕەگەکان بەسەر
(2*c)ـدا دابەش دەکەن؛ بەسەر(2*a)ـدا دابەش دەکات. - دێڕی 11: پەیامی «we have three case» دەبێت بڵێت دوو ڕەگی ڕاستی — زۆرترین دوو ڕەگی هەیە.
- دێڕی 9: بۆ تەنها «complex case» چاپ دەکات. MATLAB دەتوانێت ڕاستەوخۆ ڕەگە ئاوێتەکان بدات:
sqrtـی چەند جارێکی (ژمارە ئاوێتەکان). - دێڕی 16: کاتێک و بێت هیچ هاوکێشەیەک نییە بۆ چارەسەرکردن؛
-c/bبەسەر سفردا دابەش دەکات و-InfیانInfچاپ دەکات (یانNaNئەگەر cـیش سفر بێت).
Spot the mistakes: the quadratic-equation solverهەڵەکان بدۆزەرەوە: چارەسەرکەری هاوکێشەی پلەدوو
The corrected solverچارەسەرکەرە چاککراوەکە
The corrected program divides by 2*a, prints honest messages, gives complex roots instead of only a message, and handles the two special cases when . Test data is chosen so that every branch runs at least once:
بەسەر 2*aـدا دابەش دەکات، پەیامی ڕاست چاپ دەکات، لە جیاتی تەنها پەیامێک ڕەگە ئاوێتەکان دەدات و دوو حاڵەتە تایبەتەکەی کاتێک چارەسەر دەکات. وا هەڵبژێردراوە کە هەموو لقێک لانیکەم جارێک جێبەجێ ببێت:
% quadratic.m - solve a*x^2 + b*x + c = 0
a = input('a = ');
b = input('b = ');
c = input('c = ');
if a ~= 0
d = b^2 - 4*a*c; % the discriminant
if d == 0
disp('One repeated root')
x = -b/(2*a)
else
if d > 0
disp('Two real roots')
else
disp('Two complex roots')
end
x1 = (-b + sqrt(d))/(2*a)
x2 = (-b - sqrt(d))/(2*a)
end
elseif b ~= 0
disp('First-degree equation')
x = -c/b
elseif c == 0
disp('Every x is a solution')
else
disp('No solution')
enda = 1
b = -3
c = 2
Two real roots
x1 =
2
x2 =
1Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 6
A
%comment can follow code on the same line.دەتوانێت لە هەمان دێڕدا دوای کۆد بێت.
- Lineدێڕی 16-17
For ,
sqrt(d)is imaginary, so x1 and x2 come out complex — no special code needed.بۆ ،
sqrt(d)خەیاڵییە، بۆیە x1 و x2 ئاوێتە دەردەچن — کۆدی تایبەت پێویست نییە.- Lineدێڕی 19-26
: if b is not 0 it is a ; if too, either every x works () or none does.
: ئەگەر b سفر نەبێت ؛ ئەگەر bـیش سفر بێت، یان هەموو xێک دروستە () یان هیچیان.
| a, b, ca، b، c | Branchلق | Corrected output چاککراو | The lab's program تاقیگە |
|---|---|---|---|
| two real rootsدوو ڕەگی ڕاستی | x1 = 2, x2 = 1 | x1 = 1, x2 = 0.5000 | |
| one repeated rootیەک ڕەگی دووبارە | x = 2 | x = 8 | |
| complex rootsڕەگی ئاوێتە | x1 = -1.0000 + 2.0000i, x2 = -1.0000 - 2.0000i | complex case | |
| first-degreeپلەیەک | x = 3 | x = 3 | |
| no solutionهیچ چارەسەر نییە | No solution | x = -Inf | |
| every xهەموو x | Every x is a solution | x = NaN |
Choosing tests like this is the trace-table habit of the shared lessons: try the normal case, the equal case, the impossible case and the edge cases, not just one happy example.
هەڵبژاردنی تاقیکردنەوەی لەم جۆرە ڕاهاتنی خشتەی بەدواداچوونی وانە هاوبەشەکانە: حاڵەتی ئاسایی، حاڵەتی یەکسان، حاڵەتی مەحاڵ و حاڵەتە سنوورییەکان تاقی بکەرەوە، نەک تەنها یەک نموونەی سەرکەوتوو.
When the condition is an arrayکاتێک مەرجەکە ڕیزەکە
What does if v > 0 mean when v holds several numbers? MathWorks gives the rule: an if condition is true only when its result is not empty and every element is nonzero. So one negative number makes the whole condition false, and an empty array [] is false too. The code below says not all positive although two of the three numbers are positive:
if v > 0 چ واتایەکی هەیە کاتێک v چەند ژمارەیەک هەڵدەگرێت؟ MathWorks یاساکە دەدات: if تەنها کاتێک ڕاستە کە ئەنجامەکەی بەتاڵ نەبێت و هەموو سفر نەبن. بۆیە یەک هەموو هەڵە دەکات و بەتاڵی []ـیش هەڵەیە. کۆدی خوارەوە دەڵێت not all positive هەرچەندە دوو لە سێ ژمارەکە موجەبن:
v = [4 -2 7];
if v > 0
disp('all positive')
else
disp('not all positive')
endnot all positive
Code is clearer when it says what it means. Write if all(v > 0) for every element, and if any(v < 0) for at least one. To ask whether two whole arrays are the same, use isequal(A, B): it gives one answer. A == B gives a whole array of answers, and an error when the sizes do not fit together.
کۆد ڕوونترە کاتێک ئەوە بڵێت کە مەبەستیەتی. بۆ هەموو بنووسە if all(v > 0) و بۆ لانیکەم یەکێک بنووسە if any(v < 0). بۆ پرسینی ئەوەی ئایا دوو تەواو وەک یەکن، isequal(A, B) بەکار بهێنە: یەک وەڵام دەدات. A == B تەواو لە وەڵام دەدات و کاتێک قەبارەکان لەگەڵ یەک ناگونجێن دەدات.
The flow-control keywords, and errorوشە کلیلەییەکانی کۆنترۆڵی ڕەوت، هەروەها error
The commands list and the guide name the words MATLAB uses to steer a program — its . This lesson teaches if, elseif, else and end; the others come next:
لیستی و ڕێبەرەکە ئەو وشانە ناو دەبەن کە MATLAB بۆ ئاڕاستەکردنی بەکاریان دەهێنێت — . ئەم وانەیە if، elseif، else و end فێر دەکات؛ ئەوانی تر دواتر دێن:
| Keywords | Jobئەرک | Whereلە کوێ |
|---|---|---|
if, elseif, else, end | choose a path by a conditionهەڵبژاردنی ڕێگایەک بە | this lessonئەم وانەیە |
switch, case, otherwise | choose a path by a valueهەڵبژاردنی ڕێگایەک بە | the switch statementوانەی ڕستەی switch |
for | repeat for each value of a vectorدووبارەکردنەوە بۆ هەر | for loopsوانەی خولەکانی for |
while, break | repeat while a condition holds; leave a loop earlyدووبارەکردنەوە تا ڕاستە؛ زوو جێهێشتنی | while loops and breakوانەی خولەکانی while و break |
return | leave a function earlyزوو جێهێشتنی | function filesوانەی فایلەکانی کردار |
error, warning | stop with a message / only warnوەستان بە پەیامێک / تەنها ئاگادارکردنەوە | belowخوارەوە |
error('…') stops the program at once and shows your message in red, like any other error. warning('…') prints Warning: and your message, and the program goes on. Use error when continuing would give nonsense:
error('…') دەستبەجێ دەوەستێنێت و پەیامەکەت بە سوور پیشان دەدات، وەک هەر تر. warning('…') Warning: و پەیامەکەت چاپ دەکات و بەردەوام دەبێت. error بەکار بهێنە کاتێک بەردەوامبوون ئەنجامی بێمانا دەدات:
r = input('Radius: ');
if r < 0
error('The radius cannot be negative.')
elseif r == 0
warning('The radius is zero, so the area is 0.')
end
area = pi*r^2Radius: 0
Warning: The radius is zero, so the area is 0.
> In check_radius (line 5)
area =
0Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
With , MATLAB would stop here and print the message in red;
areawould never be computed.بە ، MATLAB لێرە دەوەستێت و پەیامەکە بە سوور چاپ دەکات؛
areaهەرگیز هەژمار ناکرێت.
The 0 < x < 10 trapتەڵەی 0 < x < 10
In maths, means x is between 0 and 10. MATLAB reads 0 < x < 10 differently: left to right, as (0 < x) < 10. The first part gives 0 or 1, and both 0 and 1 are less than 10 — so the whole test is always true, even for . Write two comparisons joined by AND:
لە بیرکاریدا واتە x لە نێوان 0 و 10دایە. MATLAB 0 < x < 10 بە شێوەیەکی تر دەخوێنێتەوە: لە چەپەوە بۆ ڕاست، وەک (0 < x) < 10. بەشی یەکەم 0 یان 1 دەدات و 0 و 1 هەردووکیان لە 10 بچووکترن — بۆیە هەموو پشکنینەکە هەمیشە ڕاستە، تەنانەت بۆ . دوو بەراورد بنووسە کە بە AND پێکەوە بەستراون:
x = 25;
0 < x < 10
0 < x && x < 10ans = logical 1 ans = logical 0
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
(0 < 25)is 1, and1 < 10is 1: wrong.(0 < 25)دەکاتە 1 و1 < 10دەکاتە 1: هەڵەیە.- Lineدێڕی 3
Both conditions must hold: correct.
دەبێت هەردوو ڕاست بن: دروستە.
Python is the exception here: it really does read 0 < x < 10 as two comparisons (chained comparisons). MATLAB, like most languages, does not.
Python لێرەدا جیاوازە: بەڕاستی 0 < x < 10 وەک دوو بەراورد دەخوێنێتەوە (بەراوردی زنجیرەیی). MATLAB، وەک زۆربەی زمانەکان، وا ناکات.
Common mistakesهەڵە باوەکان
| Mistakeهەڵە | What happensچی ڕوودەدات | Fixچارەسەر |
|---|---|---|
if x = 5 (one =)if x = 5 (یەک =) | An error: = stores, it cannot test.: = هەڵدەگرێت، ناتوانێت بپشکنێت. | if x == 5if x == 5 |
else if with a spaceelse if بە | A nested if that needs its own end; usually a missing-end error. کە endـی خۆی پێویستە؛ زۆرجار بە هۆی کەمیی end. | elseif as one wordelseif وەک یەک وشە |
Forgetting end, or writing Endلەبیرچوونی end، یان نووسینی End | MATLAB reports a missing end; End is not a keyword.MATLAB کەمیی end ڕادەگەیەنێت؛ End نییە. | Close every if with lower-case end; let Ctrl+I indent.هەموو ifـێک بە endـی پیت بچووک دابخە؛ با Ctrl+I بکات. |
if name == 'Hana'if name == 'Hana' | An error when the lengths differ. کاتێک درێژییەکان جیاوازن. | if strcmp(name, 'Hana')if strcmp(name, 'Hana') |
if 0 < x < 10if 0 < x < 10 | Always true.هەمیشە ڕاستە. | if 0 < x && x < 10if 0 < x && x < 10 |
if v > 0 for a vector, meaning someif v > 0 بۆ ، بە مەبەستی هەندێکیان | True only if all elements pass.تەنها کاتێک ڕاستە کە هەموو تێپەڕن. | any(v > 0) or all(v > 0)any(v > 0) یان all(v > 0) |
if x == NaNif x == NaN | Always false: NaN equals nothing.هەمیشە هەڵەیە: NaN یەکسان نییە بە هیچ. | if isnan(x)if isnan(x) |
0.1 + 0.2 == 0.30.1 + 0.2 == 0.3 | False because of round-off.هەڵەیە بەهۆی هەڵەی نزیککردنەوە. | abs(a - b) < 1e-10abs(a - b) < 1e-10 |
~a > b meaning not (a > b)~a > b بە مەبەستی نا (a > b) | ~ is applied to a first.~ سەرەتا لەسەر a جێبەجێ دەبێت. | ~(a > b)~(a > b) |
Practiceڕاهێنان
Read a whole number and print whether it is even or odd. Use mod (remainders).
بخوێنەوە و چاپ بکە کە جووتە یان تاک. mod بەکار بهێنە (پاشماوە).
Hintئاماژە
A number is even when the remainder after dividing by 2 is 0.
ژمارەیەک جووتە کاتێک دابەشکردنی بەسەر 2دا سفر بێت.
Show answerوەڵامەکە پیشان بدە
n = input('Whole number: ');
if mod(n, 2) == 0
fprintf('%d is even\n', n)
else
fprintf('%d is odd\n', n)
endWhole number: 7 7 is odd
A year is a leap year if it divides by 4 but not by 100, or if it divides by 400. Write one if with && and || and test 2024, 2026, 1900 and 2000.
ساڵێک کەبیسەیە ئەگەر بەسەر 4دا دابەش بێت بەڵام بەسەر 100دا نا، یان ئەگەر بەسەر 400دا دابەش بێت. یەک if بە && و || بنووسە و 2024، 2026، 1900 و 2000 تاقی بکەرەوە.
Hintئاماژە
Brackets make the AND part one unit before the OR.
کەوانەکان بەشی AND پێش OR دەکەن بە یەک یەکە.
Show answerوەڵامەکە پیشان بدە
y = input('Year: ');
if (mod(y, 4) == 0 && mod(y, 100) ~= 0) || mod(y, 400) == 0
disp('leap year')
else
disp('not a leap year')
endYear: 1900 not a leap year
2024 → leap year, 2026 → not, 1900 → not (divides by 100 but not by 400), 2000 → leap year.
2024 کەبیسەیە، 2026 نا، 1900 نا (بەسەر 100دا دابەش دەبێت بەڵام بەسەر 400دا نا)، 2000 کەبیسەیە.
marks = [45 72 50 38 91 66]; In one line each, find: the marks of 50 or more, how many there are, their positions, and whether anyone got more than 90.
marks = [45 72 50 38 91 66]; هەریەکە بە یەک دێڕ بدۆزەرەوە: نمرەکانی 50 و سەرووتر، چەندن، شوێنەکانیان و ئایا کەسێک لە 90 زیاتری هێناوە.
Show answerوەڵامەکە پیشان بدە
marks = [45 72 50 38 91 66];
passed = marks(marks >= 50)
how_many = sum(marks >= 50)
pos = find(marks >= 50)
any(marks > 90)passed =
72 50 91 66
how_many =
4
pos =
2 3 5 6
ans =
logical
1Using the program from the guide (if a < b j = -1; elseif a > b j = 2; else j = 3; end), what is j for , and ?
بە بەکارهێنانی ڕێبەرەکە (if a < b j = -1; elseif a > b j = 2; else j = 3; end)، j چەندە بۆ ، و ؟
Show answerوەڵامەکە پیشان بدە
; ; .
بۆ دەبێتە ؛ بۆ دەبێتە 2؛ بۆ دەبێتە 3.
Read two numbers and print the bigger one, or say they are equal. Then compare your program with the largest of three flowchart.
دوو ژمارە بخوێنەوە و گەورەترینیان چاپ بکە، یان بڵێ یەکسانن. پاشان لەگەڵ گەورەترینی سێ ژمارە بەراورد بکە.
Show answerوەڵامەکە پیشان بدە
p = input('First: ');
q = input('Second: ');
if p > q
fprintf('%g is bigger\n', p)
elseif q > p
fprintf('%g is bigger\n', q)
else
disp('They are equal')
endFirst: 4.5 Second: 7 7 is bigger
Summaryکورتە
== ~= < <= > >=compare and answer with logical 1 or 0;=stores,==compares; write~=(Octave also knows!=).- On arrays, comparisons work element by element and give a logical array;
v(v > 5)picks elements,findgives positions,any/allsummarise. isnan,isinf,isfinite(notfinite),isempty,isrealtest special values.&,|,~,xorwork element by element; use&&and||(short-circuit) inifconditions.if … elseif … else … end: only the first true branch runs;elseifis one word; everyifends withend.- An array condition is true only if it is not empty and all elements are nonzero;
0 < x < 10is always true — write0 < x && x < 10. - Test every branch: the quadratic solver needed
(2*a), honest messages and the , cases.
== ~= < <= > >=بەراورد دەکەن و بە 1 یان 0ـی لۆجیکی وەڵام دەدەنەوە؛=هەڵدەگرێت و==بەراورد دەکات؛~=بنووسە (Octave!=ـیش دەناسێت).- لەسەر ، بەراوردەکان بە کار دەکەن و دەدەن؛
v(v > 5)هەڵدەبژێرێت،findشوێنەکان دەدات،any/allکورتی دەکەنەوە. isnan،isinf،isfinite(نەکfinite)،isemptyوisrealدەپشکنن.&،|،~وxorبە کار دەکەن؛ لەifـدا&&و||() بەکار بهێنە.if … elseif … else … end: تەنها یەکەم لقی ڕاست جێبەجێ دەبێت؛elseifیەک وشەیە؛ هەمووifـێک بەendکۆتایی دێت.- بە تەنها کاتێک ڕاستە کە بەتاڵ نەبێت و هەموو سفر نەبن؛
0 < x < 10هەمیشە ڕاستە — بنووسە0 < x && x < 10. - هەموو لقێک تاقی بکەرەوە: چارەسەرکەری پێویستی بە
(2*a)، پەیامی ڕاست و حاڵەتەکانی ، بوو.
Why is if x = 3 an error but x = 3 fine?بۆچی if x = 3 هەڵەیە بەڵام x = 3 دروستە؟
An if needs a question; = is an assignment, not a comparison. Use ==.
if پێویستی بە پرسیارێکە؛ = ، نەک بەراورد. == بەکار بهێنە.
What is the difference between & and &&?جیاوازیی & و && چییە؟
& works element by element on arrays; && takes single true/false values and stops early when the answer is known.
& بە لەسەر کار دەکات؛ && تاکە ڕاست/هەڵە وەردەگرێت و کاتێک وەڵامەکە زانرا زوو دەوەستێت.
How do you get the numbers of v that are negative?چۆن ئەو ژمارانەی v وەردەگریت کە سالبن؟
v(v < 0) — logical indexing with the mask v < 0.
v(v < 0) — بە ماسکی v < 0.
Lesson complete!وانەکە تەواو بوو!
Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.
Key termsزاراوە سەرەکییەکان
The switch Statementڕستەی switch
Related lecturesوانە پەیوەندیدارەکان
- Algorithms, Pseudocode and Flowchartsئەلگۆریزمەکان، نیمچەکۆد و هێڵکارییەکانی ڕەوت
- Logic Gates, Truth Tables and the NOT Gateگەیتە لۆجیکییەکان، خشتەکانی ڕاستی و گەیتی NOT
- Quadratic Inequalities and the Sign Chartلاسەنگە پلەدووەکان و خشتەی هێماکان
- Making Decisions: if, elif, elseبڕیاردان:
if،elifوelse - Logical Operators and Nested ifکارپێکەرە لۆجیکییەکان و ڕستەی ifـی ناوەکی
- Vectors and Rangesئاڕاستەبڕەکان و زنجیرەکان
- The switch Statementڕستەی switch
- for Loops: Sums, Series and Searchingخولەکانی for: کۆکردنەوە، زنجیرە و گەڕان
- Indexing, the Colon, Adding and Deleting Elementsئیندێکسەکان، دوو خاڵ، زیادکردن و سڕینەوەی توخمەکان
References and further readingسەرچاوەکان و خوێندنەوەی زیاتر
- MathWorks: Array Comparison with Relational Operators (logical arrays, NaN)
- MathWorks: Find Array Elements That Meet a Condition (logical indexing, find)
- MathWorks: if, elseif, else (truth rule for arrays, && and ||, else if creates a nested if)
- GNU Octave manual: Comparison Ops (!= and ~=)
- Octave wiki: Differences between Octave and Matlab
This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.