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

Plotting Graphsکێشانی هێڵکارییەکان

Draw graphs in MATLAB and Octave: a plot in three steps and why more points give a smoother curve, titles, axis labels, grid and axis limits, colours, markers and line styles with legend and hold on, fplot for function handles, several graphs with subplot (and MATLAB's tiledlayout), bar charts and histograms, other chart types, 3-D surfaces with meshgrid and surf, saving figures, and practice with the calculus course's graphs.

لە MATLAB و Octaveـدا بکێشە: لە سێ هەنگاودا و بۆچی خاڵی زیاتر چەماوەیەکی لووستر دەدات، ناونیشان، ناوی ، تۆڕ و سنووری ، ڕەنگ، نیشانە و شێوەی هێڵ لەگەڵ و ⁦hold on⁩، fplot بۆ ، چەند بە subplot (و tiledlayoutـی MATLAB)، و ، جۆرەکانی تری ، ڕووکاری سێ ڕەهەندی بە meshgrid و surf، پاشەکەوتکردنی figure و ڕاهێنان لەسەر کۆرسی ژمێرکاری.

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

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

  • Plot y against x in three steps and choose enough points for a smooth curve.
  • Add a title, axis labels, a grid and axis limits, and style lines with LineSpec, legend and hold on.
  • Draw a function handle with fplot and arrange several graphs with subplot.
  • Choose bar, histogram (MATLAB) or hist (Octave) and the other chart types correctly.
  • Draw a 3-D surface with meshgrid and surf and save a figure as a picture file.
  • y بەرامبەر x لە سێ هەنگاودا بکێشیت و خاڵی پێویست بۆ چەماوەیەکی لووس هەڵبژێریت.
  • ناونیشان، ناوی ، تۆڕ و سنووری زیاد بکەیت و هێڵەکان بە LineSpec، legend و ⁦hold on⁩ ڕێک بخەیت.
  • بە fplot بکێشیت و چەند بە subplot ڕێک بخەیت.
  • bar، ⁦histogram (MATLAB)⁩ یان ⁦hist (Octave)⁩ و جۆرەکانی تری بە دروستی هەڵبژێریت.
  • ڕووکارێکی سێ ڕەهەندی بە meshgrid و surf بکێشیت و figureـێک وەک وێنە پاشەکەوت بکەیت.

A plot in three stepsهێڵکارییەک لە سێ هەنگاودا

Numbers in a table are hard to read; a picture shows the pattern at once. MATLAB draws a graph in three steps: (1) make the x values, usually a range or linspace (vectors and ranges); (2) compute the y values from them; (3) call plot(x, y). What the graph of a function means is in drawing a graph. All the plotting commands in this lesson also work in GNU Octave (free) and in the free version of MATLAB Online, except where a box says otherwise.

ژمارەکانی ناو خشتەیەک بە ئاستەم دەخوێنرێنەوە؛ وێنەیەک یەکسەر شێوازەکە پیشان دەدات. MATLAB لە سێ هەنگاودا دەکێشێت: ⁦(1)⁩ x دروست بکە، زۆرجار زنجیرەیەک یان linspace (ئاڕاستەبڕەکان و زنجیرەکان)؛ ⁦(2)⁩ y لەوانەوە هەژمار بکە؛ ⁦(3)⁩ plot(x, y) بانگ بکە. واتای لە کێشانی هێڵکارییەکی نەخشەدایە. هەموو کێشانی ئەم وانەیە لە ⁦GNU Octave⁩ (بەخۆڕایی) و وەشانی بەخۆڕایی ⁦MATLAB Online⁩ـیشدا کار دەکەن، جگە لەو شوێنانەی سندووقێک شتێکی تر دەڵێت.

A taxi fare: 3000 IQD plus 750 IQD per kmکرێی تاکسی: ⁦3000⁩ دینار و ⁦750⁩ دینار بۆ هەر کیلۆمەترێکMATLABType in a terminalلە تێرمیناڵدا بینووسە
km = 0:2:20;              % step 1: the x values
fare = 3000 + 750*km;     % step 2: the y values
plot(km, fare)            % step 3: draw
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1

Eleven x values: 0, 2, 4, …, 20 km.

یازدە ⁦x: 0⁩، 2، 4، …، ⁦20⁩ کیلۆمەتر.

Lineدێڕی 3

plot opens a figure window and draws the points joined by lines.

plot figure دەکاتەوە و خاڵەکان بە هێڵ بەیەکەوە دەبەستێت.

A straight rising line from (0, 3000) to (20, 18000) with eleven small circles at 0, 2, 4, … 20 km; the x-axis is labelled km and the y-axis fare (IQD). A note says the 11 points are joined by straight lines.هێڵێکی ڕاستی بەرزبووەوە لە (0, 3000) بۆ (20, 18000) بە یازدە بازنەی بچووک لەسەر 0، 2، 4، … 20 کیلۆمەتر؛ تەوەرەی x ناوی km و تەوەرەی y ناوی fare (IQD)ـی هەیە. تێبینییەک دەڵێت 11 خاڵەکە بە هێڵی ڕاست بەیەکەوە بەستراون.
The taxi-fare plot (the circles and the axis labels km and fare (IQD) are added here; the three-line code draws only the line). Drawn by CêIT from values computed with Python: it shows what the MATLAB code produces (MATLAB's own window looks slightly different). کرێی تاکسی (بازنەکان و ناوی لێرە زیاد کراون؛ کۆدە سێ دێڕییەکە تەنها هێڵەکە دەکێشێت). CêIT کێشاویەتی لە بە Python هەژمار کراون: ئەوە پیشان دەدات کە کۆدی MATLAB دروستی دەکات (پەنجەرەی خودی MATLAB کەمێک جیاوازە).CêIT

plot does not know the formula — it only receives the points and joins neighbours with straight lines. A straight-line rule needs few points, but a curve needs many. For a parabola with new numbers, x = -6:0.5:6; y = x.^2 - 4; plot(x, y) uses 25 points; note .^, which squares each element (element-wise operations).

plot فۆرموولەکە نازانێت — تەنها خاڵەکان وەردەگرێت و دراوسێکان بە هێڵی ڕاست بەیەکەوە دەبەستێت. یاسایەکی هێڵی ڕاست پێویستی بە خاڵی کەمە، بەڵام چەماوەیەک پێویستی بە زۆرە. بۆ بە ژمارەی نوێ، x = -6:0.5:6; y = x.^2 - 4; plot(x, y) ⁦25⁩ خاڵ بەکار دەهێنێت؛ سەرنجی .^ بدە کە هەر دووجا دەکات (کارەکانی توخم بە توخم).

Quick checkپشکنینی خێراWhat are the three steps of a basic MATLAB plot?سێ هەنگاوەکەی سادەی MATLAB چین؟

A plot in three stepsهێڵکارییەک لە سێ هەنگاودا

More points, smoother curveخاڵی زیاتر، چەماوەی لووستر

Watch sin(x) on [0,2π][0, 2\pi] drawn with 5, 9, 17, 50 and 200 points. With 5 points the 'curve' is a zig-zag of four straight pieces; by 200 points the pieces are too short to see. linspace(a, b, n) is the easy way to choose the number of points.

سەیری sin(x) بکە لەسەر [0,2π][0, 2\pi] کە بە 5، 9، ⁦17⁩، ⁦50⁩ و ⁦200⁩ خاڵ کێشراوە. بە 5 خاڵ «چەماوەکە» زیگزاگێکە لە چوار پارچە هێڵی ڕاست؛ تا ⁦200⁩ خاڵ پارچەکان ئەوەندە کورت دەبن کە نابینرێن. linspace(a, b, n) ڕێگای ئاسانی هەڵبژاردنی ژمارەی خاڵەکانە.

Animation in five frames: the sine curve on 0 to 2π drawn with 5, 9, 17, 50 and 200 points. The first frame is a zig-zag through 0, 1, 0, −1, 0; each frame adds points, and at 200 points the line is a smooth wave lying on the dashed true curve.
plot joins points with straight lines: more points, smoother curveplot خاڵەکان بە هێڵی ڕاست دەبەستێتەوە: خاڵی زیاتر، چەماوەی لووسترCêIT
Quick checkپشکنینی خێراA plot of sin(x) looks like a zig-zag. What is the most likely cause? sin(x) وەک زیگزاگ دەردەکەوێت. ئەگەری زۆری هۆکار چییە؟

Titles, labels, grid and axis limitsناونیشان، ناوی تەوەرەکان، تۆڕ و سنووری تەوەرەکان

A graph without labels is a puzzle. After plot, add the words and the layout you want:

بەبێ ناو مەتەڵێکە. دوای plot، ئەو وشە و ڕێکخستنەی دەتەوێت زیادی بکە:

A labelled sine graph (new interval)هێڵکارییەکی سینەسی ناونراو (ماوەی نوێ)MATLABType in a terminalلە تێرمیناڵدا بینووسە
x = linspace(-2*pi, 2*pi, 400);
y = sin(x);
plot(x, y)
title('The sine curve')
xlabel('x')
ylabel('sin(x)')
grid on
axis([-2*pi 2*pi -1.5 1.5])
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 4-6

title, xlabel and ylabel take text in single quotes.

title، xlabel و ylabel دەقێک لەناو '…'ـدا وەردەگرن.

Lineدێڕی 7

grid on draws light grid lines; grid off removes them.

grid on هێڵی تۆڕی کاڵ دەکێشێت؛ grid off لایان دەبات.

Lineدێڕی 8

axis([xmin xmax ymin ymax]) fixes the visible ranges of both axes.

axis([xmin xmax ymin ymax]) سنووری بینراوی هەردوو جێگیر دەکات.

A sine wave from −2π to 2π with the title 'The sine curve', the x-axis label x, the y-axis label sin(x), light grid lines, and the y-axis running from −1.5 to 1.5.شەپۆلێکی سینەس لە −2π تا 2π بە ناونیشانی «The sine curve»، ناوی تەوەرەی x کە xـە، ناوی تەوەرەی y کە sin(x)ـە، هێڵی تۆڕی کاڵ و تەوەرەی y لە −1.5 تا 1.5.
The labelled sine graph. Drawn by CêIT from values computed with Python: it shows what the MATLAB code produces (MATLAB's own window looks slightly different). سینەسی ناونراو. CêIT کێشاویەتی لە بە Python هەژمار کراون: ئەوە پیشان دەدات کە کۆدی MATLAB دروستی دەکات (پەنجەرەی خودی MATLAB کەمێک جیاوازە).CêIT

Two more layout commands from the guide and the commands list: axis equal makes one unit equally long on both axes, so a circle looks round; axis auto goes back to automatic limits. Several commands may share one line when commas separate them: plot(x, y), grid on, axis equal. In titles and labels MATLAB reads ^ and _ as superscript and subscript, so title('y = x^2') shows a small raised 2.

دوو تری ڕێکخستن لە ڕێبەرەکە و لیستی : axis equal یەکەیەک لەسەر هەردوو بە یەک درێژی دادەنێت، بۆیە بازنەیەک خڕ دەردەکەوێت؛ axis auto سنوورەکان دەگەڕێنێتەوە بۆ خۆکار. چەند دەتوانن یەک دێڕ بەش بکەن ئەگەر کۆما لێکیان جیا بکاتەوە: plot(x, y), grid on, axis equal. لە ناونیشان و ناوەکاندا MATLAB ^ و _ وەک نووسینی سەرەوە و خوارەوە دەخوێنێتەوە، بۆیە title('y = x^2') 2ـێکی بچووکی بەرز پیشان دەدات.

Quick checkپشکنینی خێراWhich command shows only x from 0 to 10 and y from −2 to 2?کام تەنها x لە 0 تا ⁦10⁩ و y لە ⁦−2⁩ تا 2 پیشان دەدات؟

Colours, markers and line stylesڕەنگ، نیشانە و شێوەی هێڵ

A third argument, the line specification (MathWorks calls it LineSpec), sets the colour, the marker and the line style in one short text: plot(x, y, 'r--o') draws a red dashed line with circles. The symbols may come in any order, and you may leave any of them out.

سێیەم، دیاریکردنی هێڵ (MathWorks پێی دەڵێت LineSpec)، ڕەنگ، نیشانە و شێوەی هێڵ لە یەک دەقی کورتدا دادەنێت: plot(x, y, 'r--o') هێڵێکی سووری پچڕپچڕ بە بازنە دەکێشێت. هێماکان دەتوانن بە هەر ڕیزبەندییەک بێن و دەتوانیت هەر یەکێکیان نەنووسیت.

The LineSpec symbols (from the commands list, checked against MathWorks' plot page)هێماکانی LineSpec (لە لیستی ، بە پەڕەی plotـی MathWorks پشکنراون)
KindجۆرSymbolsهێماکانMeaningواتا
Colourڕەنگy m c r g b w kyellow, magenta, cyan, red, green, blue, white, blackزەرد، ئەرخەوانی، شینی ئاسمانی، سوور، سەوز، شین، سپی، ڕەش
Markerنیشانە. o x + * s d v ^ < > p hpoint, circle, cross, plus, star, square, diamond, triangles (down, up, left, right), pentagram, hexagramخاڵ، بازنە، خاچی لار، کۆ، ئەستێرە، چوارگۆشە، ئەڵماس، سێگۆشەکان (خوار، سەر، چەپ، ڕاست)، پێنجگۆشەی ئەستێرەیی، شەشگۆشەی ئەستێرەیی
Line styleشێوەی هێڵ- -- : -.solid, dashed, dotted, dash-dotپتەو، پچڕپچڕ، خاڵخاڵ، پچڕ-خاڵ

To draw several lines, give plot several x–y pairs, each with its own LineSpec, and name them with legend — the (the guide's example idea, with new functions):

بۆ کێشانی چەند هێڵێک، چەند جووتێکی ⁦x–y⁩ بدە بە plot، هەریەکە بە LineSpecـی خۆی و بە legend ناویان لێ بنێ — (بیرۆکەی نموونەکەی ڕێبەرەکە، بە نوێ):

Two lines and a legendدوو هێڵ و کلیلێکی هێڵکاریMATLABType in a terminalلە تێرمیناڵدا بینووسە
x = linspace(0, 2*pi, 100);
plot(x, sin(x), 'b-', x, sin(2*x), 'r--')
legend('sin(x)', 'sin(2x)')
title('Two waves'), xlabel('x'), ylabel('y'), grid on
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

Blue solid for sin x, red dashed for sin 2x.

شینی پتەو بۆ ⁦sin x⁩، سووری پچڕپچڕ بۆ ⁦sin 2x⁩.

Lineدێڕی 3

legend takes one label per line, in the same order.

legend بۆ هەر هێڵێک یەک ناو وەردەگرێت، بە هەمان ڕیزبەندی.

A figure window titled Figure 1 containing axes with a solid blue sine wave and a dashed orange sin(2x) wave, the title 'Two waves', the axis labels x and y, grid lines and a legend box. Arrows name the parts: figure window, title, grid lines, axes, legend and axis labels.پەنجەرەیەکی figure بە ناوی Figure 1 کە تەوەرەکان لەخۆ دەگرێت لەگەڵ شەپۆلێکی سینەسی شینی پتەو و شەپۆلێکی sin(2x)ـی پرتەقاڵیی پچڕپچڕ، ناونیشانی «Two waves»، ناوی تەوەرەکان x و y، هێڵی تۆڕ و سندووقی کلیلی هێڵکاری. تیرەکان ناوی بەشەکان دەبەن: پەنجەرەی figure، ناونیشان، هێڵی تۆڕ، تەوەرەکان، کلیلی هێڵکاری و ناوی تەوەرەکان.
The parts of a MATLAB figure. Drawn by CêIT from values computed with Python: it shows what the MATLAB code produces (MATLAB's own window looks slightly different).بەشەکانی figureـێکی ⁦MATLAB. C⁩êIT کێشاویەتی لە بە Python هەژمار کراون: ئەوە پیشان دەدات کە کۆدی MATLAB دروستی دەکات (پەنجەرەی خودی MATLAB کەمێک جیاوازە).CêIT

The other way is hold on: it keeps the current lines so the next plot adds to them instead of replacing them; hold off returns to the normal behaviour. plot(x, sin(x), 'b-'), hold on, plot(x, sin(2*x), 'r--'), hold off gives the same picture. Without a LineSpec, MATLAB chooses the colours itself from its colour order.

ڕێگای تر hold onـە: هێڵەکانی ئێستا دەهێڵێتەوە بۆ ئەوەی plotـی داهاتوو زیادیان بکات نەک جێیان بگرێتەوە؛ hold off دەگەڕێتەوە بۆ ڕەفتاری ئاسایی. plot(x, sin(x), 'b-'), hold on, plot(x, sin(2*x), 'r--'), hold off هەمان وێنە دەدات. بەبێ LineSpec، MATLAB خۆی ڕەنگەکان لە ڕیزبەندیی ڕەنگەکانی هەڵدەبژێرێت.

Quick checkپشکنینی خێراWhat does plot(x, y, 'k:') draw?plot(x, y, 'k:') چی دەکێشێت؟

Colours, markers and line stylesڕەنگ، نیشانە و شێوەی هێڵ

Markers for measured dataنیشانە بۆ دراوەی پێوراو

Measured values are single points, so show each one with a marker. A bakery in Duhok counts the loaves it sells in one week (made-up numbers):

خاڵی تاکن، بۆیە هەریەکەیان بە نیشانەیەک پیشان بدە. نانەواخانەیەک لە دهۆک ئەو نانانە دەژمێرێت کە لە هەفتەیەکدا دەیفرۆشێت (ژمارەی دروستکراو):

Line with circle markersهێڵ بە نیشانەی بازنەMATLABType in a terminalلە تێرمیناڵدا بینووسە
day = 1:7;
loaves = [120 135 128 150 170 210 190];
plot(day, loaves, 'g-o')
title('Loaves sold'), xlabel('day'), ylabel('loaves'), grid on
Seven circles joined by a line: 120, 135, 128, 150, 170, 210 and 190 loaves on days 1 to 7, with the title 'Loaves sold', axis labels day and loaves, and grid lines.حەوت بازنە کە بە هێڵێک بەستراون: 120، 135، 128، 150، 170، 210 و 190 نان لە ڕۆژەکانی 1 تا 7، بە ناونیشانی «Loaves sold»، ناوی تەوەرەکان day و loaves و هێڵی تۆڕ.
One week of sales with 'g-o' (MATLAB's own green is brighter than the colour used here). Drawn by CêIT from values computed with Python: it shows what the MATLAB code produces (MATLAB's own window looks slightly different).فرۆشی هەفتەیەک بە 'g-o' (سەوزی خودی MATLAB لەو ڕەنگەی لێرە بەکارهاتووە گەشترە). CêIT کێشاویەتی لە بە Python هەژمار کراون: ئەوە پیشان دەدات کە کۆدی MATLAB دروستی دەکات (پەنجەرەی خودی MATLAB کەمێک جیاوازە).CêIT

fplot: plotting a function handlefplot: کێشانی ئاماژەیەکی کردار

When you have the function itself as a handle (anonymous functions), fplot(f, [a b]) draws it on the interval [a, b] and chooses the points itself. Without an interval it uses [−5, 5]. Write the handle with .^, .*, ./ — MathWorks' page asks for element-wise operators to avoid a warning.

کاتێک خودی وەک هەیە (کردارە بێناوەکان)، fplot(f, [a b]) لەسەر ⁦[a, b]⁩ دەیکێشێت و خۆی خاڵەکان هەڵدەبژێرێت. بەبێ ⁦[−5, 5]⁩ بەکار دەهێنێت. بە .^، .*، ./ بنووسە — پەڕەی MathWorks بۆ ئەوەی ئاگاداری نەیەت داوای دەکات.

The guide's cubic, drawn with fplotڕادەدارە سێپلەییەکەی ڕێبەرەکە، بە fplot کێشراوەMATLABType in a terminalلە تێرمیناڵدا بینووسە
f = @(x) x.^3 - 3*x - 1;
fplot(f, [-2 2])
grid on
The curve of x³ − 3x − 1 from −2 to 2: it starts at −3, rises to 1 at x = −1, falls to −3 at x = 1 and rises to 1 at x = 2, crossing zero three times.چەماوەی x³ − 3x − 1 لە −2 تا 2: لە −3ـەوە دەست پێدەکات، لە x = −1 بەرز دەبێتەوە بۆ 1، لە x = 1 دادەبەزێت بۆ −3 و لە x = 2 بەرز دەبێتەوە بۆ 1 و سێ جار سفر دەبڕێت.
fplot(f, [-2 2]) for f(x)=x3−3x−1f(x)=x^3-3x-1; the three zero crossings are found in the root-finding project. Drawn by CêIT from values computed with Python: it shows what the MATLAB code produces (MATLAB's own window looks slightly different).fplot(f, [-2 2]) بۆ f(x)=x3−3x−1f(x)=x^3-3x-1؛ سێ خاڵی بڕینی سفر لە پڕۆژەی دۆزینەوەی ڕەگدا دەدۆزرێنەوە. CêIT کێشاویەتی لە بە Python هەژمار کراون: ئەوە پیشان دەدات کە کۆدی MATLAB دروستی دەکات (پەنجەرەی خودی MATLAB کەمێک جیاوازە).CêIT
Quick checkپشکنینی خێراfplot(@(x) x.^2) with no interval — which x values are drawn?fplot(@(x) x.^2) بەبێ — کام x دەکێشرێن؟

Several graphs in one figure: subplotچەند هێڵکاری لە یەک figureـدا: subplot

subplot(m, n, p) splits the figure into a grid of m rows and n columns of axes (plot areas) and makes number p the current one; the next plot draws there. The places are counted along the rows, left to right, then the next row — both MathWorks' and Octave's manuals say so.

subplot(m, n, p) figure دابەش دەکات بۆ تۆڕێکی m ڕیز و n ستوون لە و ژمارە p دەکات بە ئێستایی؛ plotـی داهاتوو لەوێ دەکێشێت. شوێنەکان بە درێژایی ڕیزەکان دەژمێردرێن، لە چەپەوە بۆ ڕاست، پاشان ڕیزی داهاتوو — ڕێبەری MathWorks و Octave هەردووکیان وا دەڵێن.

A figure window divided into two rows and three columns. The top row is numbered p = 1, 2, 3 and the bottom row p = 4, 5, 6; the box p = 5 is highlighted, and each box shows its call, for example subplot(2, 3, 5).پەنجەرەیەکی figure کە بۆ دوو ڕیز و سێ ستوون دابەش کراوە. ڕیزی سەرەوە p = 1، 2، 3 و ڕیزی خوارەوە p = 4، 5، 6 ژمارە کراون؛ سندووقی p = 5 دیار کراوە و هەر سندووقێک بانگکردنەکەی خۆی پیشان دەدات، بۆ نموونە subplot(2, 3, 5).
subplot(2, 3, p): p counts along the rowssubplot(2, 3, p): p بە درێژایی ڕیزەکان دەژمێردرێتCêIT
Quick checkپشکنینی خێراIn subplot(3, 2, p), which p is the right-hand graph of the second row?لە subplot(3, 2, p)ـدا، کام p لای ڕاستی ڕیزی دووەمە؟

Several graphs in one figure: subplotچەند هێڵکاری لە یەک figureـدا: subplot

Two damped waves side by sideدوو شەپۆلی کپبوو لە تەنیشت یەک

The guide compares two sine waves that die away at different speeds. Here is the same idea with new constants, y=e−xsin⁡6xy=e^{-x}\sin 6x and y=e−2.5xsin⁡6xy=e^{-2.5x}\sin 6x, in one row of two graphs:

ڕێبەرەکە دوو شەپۆلی سینەس بەراورد دەکات کە بە دوو خێرایی جیاواز کز دەبن. ئەمە هەمان بیرۆکەیە بە نەگۆڕی نوێ، y=e−xsin⁡6xy=e^{-x}\sin 6x و y=e−2.5xsin⁡6xy=e^{-2.5x}\sin 6x، لە یەک ڕیزی دوو :

subplot(1, 2, p) with two damped waves⁦subplot(1, 2, p)⁩ بە دوو شەپۆلی کپبووMATLABType in a terminalلە تێرمیناڵدا بینووسە
x = 0:0.01:4;
subplot(1, 2, 1)
plot(x, exp(-x).*sin(6*x)), xlabel('x'), ylabel('exp(-x).*sin(6*x)'), axis([0 4 -1 1])
subplot(1, 2, 2)
plot(x, exp(-2.5*x).*sin(6*x)), xlabel('x'), ylabel('exp(-2.5*x).*sin(6*x)'), axis([0 4 -1 1])
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

.* multiplies the two vectors element by element.

.* دوو بە لێک دەدات.

Lineدێڕی 3,5

The same axis limits make the two graphs easy to compare.

هەمان سنووری axis بەراوردکردنی دوو ئاسان دەکات.

Two graphs side by side on 0 to 4. Left: exp(−x)·sin(6x) swings up to about 0.78 and dies away slowly over several waves. Right: exp(−2.5x)·sin(6x) peaks at about 0.57 and is almost flat after x = 2.دوو هێڵکاری لە تەنیشت یەک لەسەر 0 تا 4. چەپ: exp(−x)·sin(6x) تا نزیکەی 0.78 دەلەرێتەوە و بە چەند شەپۆلێک بە هێواشی کز دەبێت. ڕاست: exp(−2.5x)·sin(6x) لووتکەکەی نزیکەی 0.57ـە و دوای x = 2 نزیکەی تەختە.
Two damped waves with subplot(1, 2, p). Drawn by CêIT from values computed with Python: it shows what the MATLAB code produces (MATLAB's own window looks slightly different).دوو شەپۆلی کپبوو بە subplot(1, 2, p). CêIT کێشاویەتی لە بە Python هەژمار کراون: ئەوە پیشان دەدات کە کۆدی MATLAB دروستی دەکات (پەنجەرەی خودی MATLAB کەمێک جیاوازە).CêIT

Several graphs in one figure: subplotچەند هێڵکاری لە یەک figureـدا: subplot

tiledlayout, and other figure commandstiledlayout و فەرمانەکانی تری figure

MATLAB's documentation now recommends tiledlayout over subplot for new MATLAB code, because the tiles can be spaced and resized more flexibly. Octave's manual documents only subplot, so this track uses subplot when the code must run in both. The MATLAB-only form of the same picture:

بەڵگەنامەکانی MATLAB ئێستا بۆ کۆدی نوێی MATLAB لە جیاتی subplot، tiledlayout پێشنیار دەکەن، چونکە پارچەکان بە نەرمییەکی زیاتر دوور دەخرێنەوە و قەبارەیان دەگۆڕدرێت. ڕێبەری Octave تەنها subplot ڕوون دەکاتەوە، بۆیە ئەم بەشە subplot بەکار دەهێنێت کاتێک کۆدەکە دەبێت لە هەردووکیاندا کار بکات. شێوەی تەنها-MATLABـی هەمان وێنە:

The same two graphs with tiledlayout (MATLAB)هەمان دوو هێڵکاری بە ⁦tiledlayout (MATLAB)⁩MATLABType in a terminalلە تێرمیناڵدا بینووسە
tiledlayout(1, 2)
nexttile
plot(x, exp(-x).*sin(6*x))
nexttile
plot(x, exp(-2.5*x).*sin(6*x))
More figure commands from the commands list زیاتری figure لە لیستی
CommandWhat it doesچی دەکات
figureopens a new, empty figure window (so the next plot does not replace the old one) نوێ و بەتاڵی figure دەکاتەوە (بۆ ئەوەی داهاتوو جێی کۆنەکە نەگرێتەوە)
axesmakes a new plot area (axes) in the current figure; plot and subplot usually make it for youناوچەیەکی کێشانی نوێ ⁦(axes)⁩ لە figureـی ئێستادا دروست دەکات؛ plot و subplot زۆرجار خۆیان دروستی دەکەن
close / close allcloses the current figure / every figurefigureـی ئێستا / هەموو figureـەکان دادەخات
text(x, y, 'note')writes text at the point (x, y) of the axesدەق لە خاڵی ⁦(x, y)⁩ی دەنووسێت
gtext('note')waits for a mouse click and writes the text thereچاوەڕێی کلیکی ماوس دەکات و دەقەکە لەوێ دەنووسێت
hold on / hold offkeep / stop keeping the lines already drawnهێڵە کێشراوەکان بهێڵەوە / مەیانهێڵەوە
set(gca, 'FontSize', 14)changes a property of a graphics object — here the font size of the current axesتایبەتمەندییەکی تەنێکی گرافیکی دەگۆڕێت — لێرەدا قەبارەی فۆنتی axesـی ئێستا
refreshredraws the current figurefigureـی ئێستا دووبارە دەکێشێتەوە

Other charts: bar charts and histogramsهێڵکارییەکانی تر: هێڵکارییە ستوونییەکان و هیستۆگرامەکان

bar(x, y) draws one bar per value instead of a line — right for separate things such as students or months. What a bar chart means, and when to choose it, is in bar and pie charts. Ten students' mid-term marks (new marks, the guide's idea):

bar(x, y) لە جیاتی هێڵ بۆ هەر ستوونێک دەکێشێت — گونجاوە بۆ شتی جیاجیا وەک قوتابی یان مانگ. واتای و کەی هەڵیبژێریت لە هێڵکارییە ستوونییەکان و هێڵکارییە بازنەییەکاندایە. نمرەی ناوەڕاستی وەرزی دە قوتابی (نمرەی نوێ، بیرۆکەی ڕێبەرەکە):

A bar chartهێڵکارییەکی ستوونیMATLABType in a terminalلە تێرمیناڵدا بینووسە
student = 1:10;
marks = [64 81 47 92 73 58 88 69 95 76];
bar(student, marks)
title('Mid-term marks'), xlabel('student'), ylabel('mark')
Ten bars for students 1 to 10 with heights 64, 81, 47, 92, 73, 58, 88, 69, 95 and 76, the title 'Mid-term marks' and the axis labels student and mark.دە ستوون بۆ قوتابیانی 1 تا 10 بە بەرزیی 64، 81، 47، 92، 73، 58، 88، 69، 95 و 76، ناونیشانی «Mid-term marks» و ناوی تەوەرەکان student و mark.
bar(student, marks). Drawn by CêIT from values computed with Python: it shows what the MATLAB code produces (MATLAB's own window looks slightly different).bar(student, marks). CêIT کێشاویەتی لە بە Python هەژمار کراون: ئەوە پیشان دەدات کە کۆدی MATLAB دروستی دەکات (پەنجەرەی خودی MATLAB کەمێک جیاوازە).CêIT

Other charts: bar charts and histogramsهێڵکارییەکانی تر: هێڵکارییە ستوونییەکان و هیستۆگرامەکان

Histograms: histogram in MATLAB, hist in Octaveهیستۆگرامەکان: histogram لە MATLAB، hist لە Octave

A counts how many values fall into each class and draws touching bars (histograms in statistics). For 30 marks in the classes 40–50, 50–60, …, 90–100:

دەژمێرێت چەند دەکەونە ناو هەر و ستوونی بەیەکەوەلکاو دەکێشێت (هیستۆگرامەکان لە ئاماردا). بۆ ⁦30⁩ نمرە لە ⁦40–50⁩، ⁦50–60⁩، …، ⁦90–100⁩:

A histogram of 30 marksهیستۆگرامێکی ⁦30⁩ نمرەMATLABType in a terminalلە تێرمیناڵدا بینووسە
marks = [67 72 55 81 94 63 77 58 86 71 49 66 73 88 91 ...
         62 75 57 84 69 78 53 96 68 74 83 61 79 87 65];
histogram(marks, 40:10:100)          % MATLAB
title('30 marks'), xlabel('mark'), ylabel('number of students')
counts = histcounts(marks, 40:10:100)  % MATLAB: the numbers only
% Octave: hist(marks, 45:10:95) draws the same bars (45, 55, ... are the class centres)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1-2

... continues a long line (long commands).

... دێڕێکی درێژ بەردەوام دەکات (فەرمانەکانی درێژ).

Lineدێڕی 3

The second argument gives the class edges 40, 50, …, 100.

دووەم سنوورەکانی دەدات ⁦40⁩، ⁦50⁩، …، ⁦100⁩.

Six touching bars over 40–50, 50–60, 60–70, 70–80, 80–90 and 90–100 with heights 1, 4, 8, 8, 6 and 3, the title '30 marks' and the axis labels mark and number of students.شەش ستوونی بەیەکەوەلکاو لەسەر 40–50، 50–60، 60–70، 70–80، 80–90 و 90–100 بە بەرزیی 1، 4، 8، 8، 6 و 3، ناونیشانی «30 marks» و ناوی تەوەرەکان mark و number of students.
histogram(marks, 40:10:100). Drawn by CêIT from values computed with Python: it shows what the MATLAB code produces (MATLAB's own window looks slightly different).histogram(marks, 40:10:100). CêIT کێشاویەتی لە بە Python هەژمار کراون: ئەوە پیشان دەدات کە کۆدی MATLAB دروستی دەکات (پەنجەرەی خودی MATLAB کەمێک جیاوازە).CêIT

Other charts: bar charts and histogramsهێڵکارییەکانی تر: هێڵکارییە ستوونییەکان و هیستۆگرامەکان

More chart types from the commands listجۆری زیاتری هێڵکاری لە لیستی فەرمانەکان

Special 2-D plots
CommandDrawsچی دەکێشێتUse it forبۆ چی
stairs(x, y)a staircase: flat steps that jump at each xپەیژەیەک: پلەی تەخت کە لە هەر xـێکدا باز دەدەنa such as the floor function وەک floor
stem(x, y)a vertical line with a circle for each valueهێڵێکی ستوونی بە بازنەیەک بۆ هەر separate samples, for example a signalخوێندنەوە جیاجیاکان، بۆ نموونە سیگناڵێک
loglog(x, y)both axes on a logarithmic scaleهەردوو لەسەر پێوەری لۆگاریتمیvalues that grow over many powers of ten لە یەکەوە تا ملیۆنەها گەشە دەکەن
semilogx / semilogyonly the x-axis / only the y-axis logarithmicتەنها x / تەنها y لۆگاریتمیexponential growth looks straight with semilogy بە semilogy ڕاست دەردەکەوێت
polarplot(theta, r)a curve in polar coordinates (angle and distance)چەماوەیەک بە جەمسەری (گۆشە و دووری)MATLAB: polar is not recommended, use polarplot; Octave: polarMATLAB: polar پێشنیار ناکرێت، polarplot بەکار بهێنە؛ Octave: polar
Quick checkپشکنینی خێراWhich command draws the touching bars of a histogram in current MATLAB?لە MATLABـی ئێستادا کام ستوونە بەیەکەوەلکاوەکانی دەکێشێت؟

3-D surfaces: meshgrid and surfڕووکاری سێ ڕەهەندی: meshgrid و surf

A surface z=f(x,y)z=f(x,y) needs a value of z for every pair (x, y) of a grid. meshgrid builds that grid as two matrices: X repeats the x values in every row and Y repeats the y values in every column.

ڕووکارێکی z=f(x,y)z=f(x,y) پێویستی بە z هەیە بۆ هەموو جووتێکی ⁦(x, y)⁩ی تۆڕێک. meshgrid ئەو تۆڕە وەک دوو دروست دەکات: X x لە هەموو ڕیزێکدا دووبارە دەکاتەوە و Y y لە هەموو ستوونێکدا.

A tiny gridتۆڕێکی بچووکMATLABType in a terminalلە تێرمیناڵدا بینووسە
[X, Y] = meshgrid(1:3, 1:2)

Then compute Z with element-wise operators and draw it with surf — MathWorks notes that the colour follows the height. A saddle (new function; the guide used another one):

پاشان Z بە هەژمار بکە و بە surf بیکێشە — MathWorks دەڵێت ڕەنگەکە شوێنی بەرزی دەکەوێت. زینێک ( نوێ؛ ڕێبەرەکە یەکێکی تری بەکار هێنا):

A saddle surfaceڕووکارێکی زینئاساMATLABType in a terminalلە تێرمیناڵدا بینووسە
[X, Y] = meshgrid(-2:0.25:2);
Z = X.^2 - Y.^2;
surf(X, Y, Z)
xlabel('X'), ylabel('Y'), zlabel('Z')
colorbar
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1

With one input, meshgrid uses the same values for x and y: a 17×1717 \times 17 grid.

بە یەک ، meshgrid هەمان بۆ x و y بەکار دەهێنێت: تۆڕێکی 17×1717 \times 17.

Lineدێڕی 5

colorbar shows which colour stands for which height.

colorbar پیشان دەدات کام ڕەنگ نوێنەری کام بەرزییە.

A saddle-shaped surface over the square from −2 to 2: it rises to 4 at the ends of the X direction and falls to −4 at the ends of the Y direction. The patches are coloured from dark blue (low) through green to yellow (high), with a colour bar from −4 to 4.ڕووکارێکی زینئاسا لەسەر چوارگۆشەی −2 تا 2: لە کۆتاییەکانی ئاڕاستەی Xـدا تا 4 بەرز دەبێتەوە و لە کۆتاییەکانی ئاڕاستەی Yـدا تا −4 دادەبەزێت. پارچەکان لە شینی تۆخ (نزم) بە سەوزدا بۆ زەرد (بەرز) ڕەنگ کراون، لەگەڵ شریتێکی ڕەنگ لە −4 تا 4.
surf(X, Y, Z) for Z=X2−Y2Z=X^2-Y^2. Drawn by CêIT from values computed with Python: it shows what the MATLAB code produces (MATLAB's own window looks slightly different).surf(X, Y, Z) بۆ Z=X2−Y2Z=X^2-Y^2. CêIT کێشاویەتی لە بە Python هەژمار کراون: ئەوە پیشان دەدات کە کۆدی MATLAB دروستی دەکات (پەنجەرەی خودی MATLAB کەمێک جیاوازە).CêIT
The 3-D commands of the commands list سێ ڕەهەندیی لیستی
CommandDrawsچی دەکێشێت
mesh(X, Y, Z)the same surface as a wire net (only the edges)هەمان ڕووکار وەک تۆڕێکی تەل (تەنها لێوارەکان)
surfc / meshcthe surface or net with contour lines underneathڕووکار یان تۆڕەکە لەگەڵ هێڵی هاوبەرزی لە ژێریدا
meshz / waterfalla net with a curtain around it / lines in one direction onlyتۆڕێک بە پەردەیەک بە دەوریدا / هێڵ تەنها بە یەک ئاڕاستە
contour(X, Y, Z)a flat picture of lines of equal heightوێنەیەکی تەخت لە هێڵی بەرزیی یەکسان
plot3(x, y, z)a line through points in spaceهێڵێک بە ناو خاڵەکانی بۆشاییدا
zlabel('…')the label of the z-axisناوی z
Quick checkپشکنینی خێراWhy do you need meshgrid before surf?بۆچی پێش surf پێویستت بە meshgrid هەیە؟

Saving a figure as a pictureپاشەکەوتکردنی figure وەک وێنە

To put a graph in a report, save it as a file. gcf means the current figure and gca the current axes:

بۆ دانانی لە ڕاپۆرتێکدا، وەک پاشەکەوتی بکە. gcf واتە figureـی ئێستا و gca واتە ئێستا:

Three ways to save the current figureسێ ڕێگا بۆ پاشەکەوتکردنی figureـی ئێستاMATLABType in a terminalلە تێرمیناڵدا بینووسە
saveas(gcf, 'saddle.png')            % MATLAB and Octave
print('-dpng', 'saddle.png')         % MATLAB and Octave
exportgraphics(gca, 'saddle.png')    % MATLAB R2020a and later
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1

The file extension chooses the format: .png, .jpg, .pdf …

پاشگری شێوەکە هەڵدەبژێرێت: ⁦.png⁩، ⁦.jpg⁩، ⁦.pdf⁩ …

Lineدێڕی 3

Crops the picture tightly; MathWorks suggests it for most saving jobs. Not available in Octave.

وێنەکە توند دەبڕێت؛ MathWorks بۆ زۆربەی کارەکانی پاشەکەوتکردن پێشنیاری دەکات. لە Octaveـدا نییە.

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

Plotting mistakes and their fixesهەڵەکانی کێشان و چاککردنیان
MistakeهەڵەWhat happensچی ڕوو دەداتFixچاککردن
y = x^2 for a vector xy = x^2 بۆ xan error about matrix power and square matrices دەربارەی توانی و y = x.^2 (also .*, ./)y = x.^2 (هەروەها .*، ./)
Too few points (x = 0:1:6 for a sine)خاڵی زۆر کەم (x = 0:1:6 بۆ سینەس)a jagged curveچەماوەیەکی ددانەدارlinspace(0, 2*pi, 200)linspace(0, 2*pi, 200)
A second plot without hold onplotـی دووەم بەبێ hold onthe first line disappearsهێڵی یەکەم ون دەبێتone plot with several pairs, or hold onیەک plot بە چەند جووتێک، یان hold on
Degrees into sin: sin(0:10:360) بۆ sin: sin(0:10:360)a meaningless jumbleتێکەڵەیەکی بێواتاsind(…), or radians with pisind(…)، یان بە pi
x and y of different lengthsx و y بە درێژیی جیاوازan error that the vectors must be the same length کە دەبێت هەمان درێژییان هەبێتcompute y from the same xy لە هەمان xـەوە هەژمار بکە
hist in new MATLAB code, histogram in Octavehist لە کۆدی نوێی MATLABـدا، histogram لە Octaveـداnot recommended / unknown functionپێشنیار ناکرێت / نەناسراوhistogram in MATLAB, hist in Octavehistogram لە MATLAB، hist لە Octave
ezplot or print -deps from old notesezplot یان print -deps لە تێبینیی کۆنەوەold, not recommended commands کۆن و پێشنیارنەکراوfplot, exportgraphics/saveasfplot، exportgraphics/saveas
Quick checkپشکنینی خێراx = 0:0.1:3; y = x^2; plot(x, y) stops with an error. Why?x = 0:0.1:3; y = x^2; plot(x, y) بە دەوەستێت. بۆچی؟

Practice: draw the calculus graphsڕاهێنان: هێڵکارییەکانی نەخشەی ژمێرکاری بکێشە

The calculus course asks you to sketch graphs by hand (graph exercises). Now check your sketches with MATLAB.

کۆرسی ژمێرکاری داوات لێ دەکات بە دەست بکێشیت (ڕاهێنانەکانی هێڵکاری). ئێستا وێنەکانت بە MATLAB بپشکنە.

Exerciseڕاهێنان1. Three polynomials in one figure1. سێ ڕادەدار لە یەک figureـدا

Draw −2x2−6x−2-2x^2-6x-2 on [−3, 3], x2+x−12x^2+x-12 on [−5, 4] and (x+1)(x−1)(x−2)(x+1)(x-1)(x-2) on [−2, 3] side by side with subplot(1, 3, p), each with a title and grid. Where does each graph cross the x-axis?

−2x2−6x−2-2x^2-6x-2 لەسەر ⁦[−3, 3]⁩، x2+x−12x^2+x-12 لەسەر ⁦[−5, 4]⁩ و (x+1)(x−1)(x−2)(x+1)(x-1)(x-2) لەسەر ⁦[−2, 3]⁩ لە تەنیشت یەک بە subplot(1, 3, p) بکێشە، هەریەکە بە ناونیشان و تۆڕ. هەر لە کوێ x دەبڕێت؟

Show answerوەڵامەکە پیشان بدە
MATLABType in a terminalلە تێرمیناڵدا بینووسە
x = linspace(-3, 3, 200);
subplot(1, 3, 1), plot(x, -2*x.^2 - 6*x - 2), title('-2x^2-6x-2'), grid on
x = linspace(-5, 4, 200);
subplot(1, 3, 2), plot(x, x.^2 + x - 12), title('x^2+x-12'), grid on
x = linspace(-2, 3, 200);
subplot(1, 3, 3), plot(x, (x+1).*(x-1).*(x-2)), title('(x+1)(x-1)(x-2)'), grid on
Three small graphs. Left: a downward parabola −2x²−6x−2 on −3 to 3 with its top 2.5 at x = −1.5. Middle: an upward parabola x²+x−12 on −5 to 4 crossing zero at −4 and 3. Right: a cubic (x+1)(x−1)(x−2) on −2 to 3 crossing zero at −1, 1 and 2.سێ هێڵکاریی بچووک. چەپ: بڕگەیەکی هاوتای بەرەو خوار −2x²−6x−2 لەسەر −3 تا 3 کە لووتکەکەی 2.5ـە لە x = −1.5. ناوەڕاست: بڕگەیەکی هاوتای بەرەو سەر x²+x−12 لەسەر −5 تا 4 کە لە −4 و 3دا سفر دەبڕێت. ڕاست: ڕادەدارێکی سێپلەیی (x+1)(x−1)(x−2) لەسەر −2 تا 3 کە لە −1، 1 و 2دا سفر دەبڕێت.
The three calculus graphs with subplot(1, 3, p). Drawn by CêIT from values computed with Python: it shows what the MATLAB code produces (MATLAB's own window looks slightly different).هەر سێ ژمێرکاری بە subplot(1, 3, p). CêIT کێشاویەتی لە بە Python هەژمار کراون: ئەوە پیشان دەدات کە کۆدی MATLAB دروستی دەکات (پەنجەرەی خودی MATLAB کەمێک جیاوازە).CêIT

Crossings: the first parabola at about −2.62 and −0.38 (its top is 2.5 at x = −1.5); the second at −4 and 3; the cubic at −1, 1 and 2. The second factors: x2+x−12=(x+4)(x−3)x^2+x-12=(x+4)(x-3) (factoring). The first, divided by −2, is x2+3x+1=0x^2+3x+1=0, which does not factor nicely; the quadratic formula (the solver) gives x=−3±52x=\frac{-3\pm\sqrt5}{2}.

خاڵەکانی بڕین: یەکەم لە نزیکەی −2.62-2.62 و −0.38-0.38 (لووتکەکەی ⁦2.5⁩ـە لە x=−1.5x=-1.5)؛ دووەم لە −4-4 و 3؛ لە −1-1، 1 و 2. دووەمیان بە دەپشکنرێت: x2+x−12=(x+4)(x−3)x^2+x-12=(x+4)(x-3) (شیتەڵکردن). یەکەمیان دوای دابەشکردن بە −2-2 دەبێتە x2+3x+1=0x^2+3x+1=0 کە بە ئاسانی شیتەڵ ناکرێت؛ فۆرموولەی چارەسەرکەرەکە x=−3±52x=\frac{-3\pm\sqrt5}{2} دەدات.

Exerciseڕاهێنان2. Sine and cosine together2. سینەس و کۆسینەس پێکەوە

Draw sin(x) and cos(x) on [0,2π][0, 2\pi] in one axes, sine as a solid blue line and cosine as a dashed red line, with a legend (graphs of the trigonometric functions).

sin(x) و cos(x) لەسەر [0,2π][0, 2\pi] لە یەک بکێشە، سینەس وەک هێڵێکی شینی پتەو و کۆسینەس وەک هێڵێکی سووری پچڕپچڕ، بە (هێڵکارییەکانی نەخشەی نەخشە سێگۆشەییەکان).

Show answerوەڵامەکە پیشان بدە
MATLABType in a terminalلە تێرمیناڵدا بینووسە
x = linspace(0, 2*pi, 200);
plot(x, sin(x), 'b-', x, cos(x), 'r--')
legend('sin(x)', 'cos(x)'), grid on
axis([0 2*pi -1.2 1.2])
Exerciseڕاهێنان3. The floor function as stairs3. نەخشەی floor وەک پەیژە

Use stairs to draw ⌊x⌋\lfloor x\rfloor for x from −3 to 3 (the graph of the floor function). What does the picture leave out compared with the calculus drawing?

بە stairs ⌊x⌋\lfloor x\rfloor بۆ x لە ⁦−3⁩ تا 3 بکێشە (هێڵکاریی نەخشەی floor). وێنەکە بە بەراورد لەگەڵ کێشانەکەی ژمێرکاری چی لێ کەم دەکات؟

Show answerوەڵامەکە پیشان بدە
MATLABType in a terminalلە تێرمیناڵدا بینووسە
x = -3:3;
stairs(x, floor(x))
grid on, axis([-3.5 3.5 -3.5 3.5])

stairs draws each value flat until the next x and then jumps — the floor staircase. It also draws the vertical jump lines and shows no filled and empty dots, which the calculus graph uses to say which end belongs to each step.

stairs هەر بە تەختی تا xـی داهاتوو دەکێشێت و پاشان باز دەدات — پەیژەی floor. هەروەها هێڵە ستوونییەکانی بازدان دەکێشێت و هیچ خاڵێکی پڕ و بەتاڵ پیشان نادات، کە کۆرسی ژمێرکاری بەکاریان دەهێنێت بۆ ئەوەی بڵێت کام سەر هی کام پلەیە.

Exerciseڕاهێنان4. Two taxi companies4. دوو کۆمپانیای تاکسی

Company A charges 3000 IQD plus 750 IQD per km; company B 5000 IQD plus 500 IQD per km. Plot both for 0 to 20 km with markers, a legend and labels. From the picture, after how many km is B cheaper?

کۆمپانیای ⁦A 3000⁩ دینار و ⁦750⁩ دینار بۆ هەر کیلۆمەترێک وەردەگرێت؛ کۆمپانیای ⁦B 5000⁩ دینار و ⁦500⁩ دینار بۆ هەر کیلۆمەترێک. هەردووکیان بۆ 0 تا ⁦20⁩ کیلۆمەتر بە نیشانە، و ناوەوە بکێشە. لە وێنەکەوە، دوای چەند کیلۆمەتر B هەرزانترە؟

Show answerوەڵامەکە پیشان بدە
MATLABType in a terminalلە تێرمیناڵدا بینووسە
km = 0:2:20;
plot(km, 3000 + 750*km, 'b-o', km, 5000 + 500*km, 'r-s')
legend('A', 'B'), xlabel('km'), ylabel('fare (IQD)'), grid on

The lines meet where 3000+750k=5000+500k3000 + 750k = 5000 + 500k, so k=8k = 8 km (both 9000 IQD). For longer trips B is cheaper.

هێڵەکان لەو شوێنە یەک دەگرن کە 3000+750k=5000+500k3000 + 750k = 5000 + 500k، کەواتە k=8k = 8 کیلۆمەتر (هەردووکیان ⁦9000⁩ دینار). بۆ گەشتی درێژتر B هەرزانترە.

Exerciseڕاهێنان5. A histogram of your own5. هیستۆگرامێکی خۆت

Make 200 random marks with marks = randi([40 100], 1, 200); and draw their histogram with classes of width 10 (MATLAB: histogram, Octave: hist). Run the two lines a second time. Why does the second picture differ from the first?

بە marks = randi([40 100], 1, 200); ⁦200⁩ نمرەی هەڕەمەکی دروست بکە و بە پانیی ⁦10⁩ بکێشە (MATLAB: histogram، Octave: hist). هەردوو دێڕەکە جارێکی تر جێبەجێ بکە. بۆچی وێنەی دووەم لە وێنەی یەکەم جیاوازە؟

Show answerوەڵامەکە پیشان بدە
MATLABType in a terminalلە تێرمیناڵدا بینووسە
marks = randi([40 100], 1, 200);
histogram(marks, 40:10:100)      % MATLAB
% hist(marks, 45:10:95)          % Octave

Each call to randi takes the next numbers of MATLAB's random sequence, so the second run uses different marks and the bar heights change. The shape stays roughly flat because every whole mark from 40 to 100 is equally likely (the last class also holds 100, so it is a little taller on average). One surprise: a freshly started MATLAB always begins the same sequence (factory setting: seed 0), so two people who start MATLAB and run this once get the same picture; rng('shuffle') gives a new start. Octave starts from a different state each time.

هەر بانگکردنێکی randi ژمارەکانی داهاتووی زنجیرەی هەڕەمەکیی MATLAB وەردەگرێت، بۆیە جێبەجێکردنی دووەم نمرەی جیاواز بەکار دەهێنێت و بەرزیی ستوونەکان دەگۆڕێت. شێوەکە نزیکەی تەخت دەمێنێتەوە چونکە هەموو نمرەیەکی تەواو لە ⁦40⁩ تا ⁦100⁩ هەمان ئەگەری هەیە ( کۆتایی ⁦100⁩ـیش لەخۆ دەگرێت، بۆیە بە ناوەند کەمێک بەرزترە). سەرسوڕمانێک: MATLAB کە تازە دەکرێتەوە هەمیشە هەمان زنجیرە دەست پێدەکات (ڕێکخستنی کارگە: ⁦seed 0⁩)، بۆیە دوو کەس کە MATLAB دەکەنەوە و ئەمە یەک جار جێبەجێ دەکەن هەمان وێنە وەردەگرن؛ rng('shuffle') دەستپێکێکی نوێ دەدات. Octave هەموو جارێک لە دۆخێکی جیاوازەوە دەست پێدەکات.

Summaryپوختە

  • Three steps: x values (linspace), y values with element-wise operators, plot(x, y); more points give a smoother curve.
  • title, xlabel, ylabel, grid on, axis([…]), axis equal label and frame the picture.
  • LineSpec such as 'r--o' sets colour, line and marker; several pairs in one plot or hold on; legend names the lines.
  • fplot(f, [a b]) draws a function handle (ezplot is old); subplot(m, n, p) counts along the rows; MATLAB also offers tiledlayout.
  • bar; histogram/histcounts in MATLAB, hist in Octave; stairs, stem, loglog, semilogx, semilogy, polarplot.
  • meshgrid + surf (also mesh, contour, plot3); save with saveas, print -dpng or exportgraphics (not print -deps).
  • سێ هەنگاو: x (linspace)، y بە ، plot(x, y)؛ خاڵی زیاتر چەماوەیەکی لووستر دەدات.
  • title، xlabel، ylabel، grid on، axis([…])، axis equal ناو و چوارچێوە دەدەنە وێنەکە.
  • LineSpec وەک 'r--o' ڕەنگ، هێڵ و نیشانە دادەنێت؛ چەند جووتێک لە یەک plotـدا یان hold on؛ legend ناو لە هێڵەکان دەنێت.
  • fplot(f, [a b]) دەکێشێت (ezplot کۆنە)؛ subplot(m, n, p) بە درێژایی ڕیزەکان دەژمێرێت؛ MATLAB tiledlayoutـیش پێشکەش دەکات.
  • bar؛ histogram/histcounts لە MATLAB، hist لە Octave؛ stairs، stem، loglog، semilogx، semilogy، polarplot.
  • meshgrid + surf (هەروەها mesh، contour، plot3)؛ بە saveas، print -dpng یان exportgraphics پاشەکەوت بکە (نەک print -deps).
Review questionsپرسیارەکانی پێداچوونەوە
Why does plot(x, x.^2) look like a polygon when x has only 5 values?بۆچی plot(x, x.^2) وەک فرەلا دەردەکەوێت کاتێک x تەنها 5 نرخێکی هەیە؟

plot joins the given points with straight lines; with 5 points there are only 4 straight pieces. Use more points.

plot خاڵە دراوەکان بە هێڵی ڕاست دەبەستێتەوە؛ بە 5 خاڵ تەنها 4 پارچەی ڕاست هەیە. خاڵی زیاتر بەکار بهێنە.

What is the difference between plot and fplot?جیاوازیی نێوان plot و fplot چییە؟

plot draws vectors of points you computed; fplot takes a function handle and an interval and picks the points itself.

plot خاڵ دەکێشێت کە خۆت هەژمارت کردوون؛ fplot و وەردەگرێت و خۆی خاڵەکان هەڵدەبژێرێت.

Which p is the bottom-left graph in subplot(2, 3, p)?لە subplot(2, 3, p)ـدا، کام p هێڵکاریی خوارەوەی لای چەپە؟

4: the first row is 1, 2, 3 and the second row starts at 4.

4: ڕیزی یەکەم 1، 2، 3ـە و ڕیزی دووەم بە 4 دەست پێدەکات.

Videoڤیدیۆ

Watch a videoسەیری ڤیدیۆیەک بکە

A video that viewers liked a lot and praised in their comments, chosen for this lesson.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.

MATLAB Crash Course for BeginnersfreeCodeCamp.org · Englishئینگلیزی · 1.7M viewsبینین · 37K likesلایک · Partبەشی 47:57–1:05:29 (ofلە 1:57:45)

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 2 - Plotting' (47:57-1:05:29): plot, hold on (50:32), xlabel/ylabel/title and grid, colour and marker codes such as 'ms' and 'bv', legend (56:31), axis limits and subplot(2,2,1) (1:02:43).

بەشی Example 2 - Plotting (لە ⁦47:57⁩ تا ⁦1:05:29⁩): plot، hold on ⁦(50:32)⁩، xlabel/ylabel/title و grid، کۆدەکانی ڕەنگ و نیشانە وەک 'ms' و 'bv'، ⁦(56:31)⁩، سنووری و subplot(2,2,1) ⁦(1:02:43)⁩.

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

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

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

Next lessonوانەی داهاتوو

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

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

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