You are on page 1of 7

[MATLAB BASIC] October 11, 2012

HOW TO MAKE CIRCLE IN MATLAB

Dalam artikel ini akan dibahas tentang membuat Lingkaran atau Circle menggunakan matlab. Sebenarnya tidak terlalu rumit, kita hanya perlu memahami saja bahwa circle merupakan bangun 2 dimensi yang terdiri dari jari-jari (r) dan titik pusat (x0, y0). Dengan demikian ada tiga input yang digunakan dalam program. Agar tidak terlalu memperpanjang artikel ini saya langsung hadirkan program untuk membuat circle menggunakan Matlab.
clear all; close all; clc; NOP = 200; pusat = [0 0]; r = 2; THETA = linspace(0,2*pi,NOP); RHO = ones(1, NOP) * r; [X,Y] = pol2cart(THETA, RHO); X = X + pusat(1); Y = Y + pusat(2); plot(X, Y, 'r-'); hold on; plot(pusat(1), pusat(2), 'ko'); hold on; line([pusat(1) X(100)], [pusat(2) Y(100)], 'color', 'k'); hold off; title('CIRCLE'); axis square; axis off;

hasil eksekusi dari program di atas adalah

[janshendry@gmail.com]

Page 1

[MATLAB BASIC] October 11, 2012

Program di atas bisa dikembangkan lagi misalnya untuk membuat sederatan lingkaran dalam bentuk matriks. Program utama
clear all; close all; clc; r = 1; xlim = 20; ylim = 20; y = r; t = 0; x = 0; cond = true; while y + r <= ylim if cond; x = 0; else x = r; end while x + 2 * r <= xlim circle([x + r, y], r, 360); hold on; x = x + r * 2; end y = y + r * sqrt(3); t = t + 2 * r; cond = ~cond; end hold off;

program utama di atas akan memanggil fungsi CIRCLE berikut ini:


function circle(pusat, r, NOP) THETA = linspace(0, 2*pi, NOP); RHO = ones(1, NOP) * r; [X,Y] = pol2cart(THETA, RHO); X = X + pusat(1); Y = Y + pusat(2); plot(X,Y, 'r-'); axis square; axis off;

[janshendry@gmail.com]

Page 2

[MATLAB BASIC] October 11, 2012

hasil eksekusi dari program di atas adalah

Bisa juga kita membentuk segitiga pascal yang tersusun dari banyak lingkaran. Code berikut ini saya dapat dari sebuah website yang beralamat di http://m2matlabdb.ma.tum.de/download.jsp?MC_ID=5&SC_ID=13&M_ID=280 program ini terdiri atas 2 m file yakni main program dan fungsi untuk menampilkan lingkaran dalam konstruksi segitiga pascal atau pascal triangle. Perhatikan hasil eksekusi di bawah ini:

[janshendry@gmail.com]

Page 3

[MATLAB BASIC] October 11, 2012

Dengan menggunakan program untuk pascal triangle di atas kita juga bisa membuat fractal Sierpinski Triangle. Perhatikan hasil berikut ini:

Program utama
function Pascal(n,s) % % % % % % PASCAL(N,S) draws Pascal's triangle with coloured hexagons. The colour of each hexagon is determined by the parameters n and s; n is the number of rows of the triangle, and s is a natural number. If s divides the number of the current hexagon, then the hexagon is red, otherwise green. For n <= 12 the numbers are written within each hexagon.

% PASCAL uses n = 10, s = 4. % This file was generated by students as a partial fulfillment % for the requirements of the course "Fractals", Winter term % 2004/2005, Stuttgart University. % Author : Sylvia Frey % Date : Nov 2004 % Version: 1.0 % Modify : Jans Hendry % Gadjah Mada University % items : modification to display circle instead of hexagon

[janshendry@gmail.com]

Page 4

[MATLAB BASIC] October 11, 2012

% default settings if nargin ~=2 n = 10; s = 4; end % more than n = 55 is not possible if n < 55 a = n/10; x = 0; y = 0; x2 = 0; w = 1; % draw the top hexagon Sechseck(n,x,y,w,a,s); hold on; % loop on the rows of the triangle for m = 1:n-1 x = x2-a/2; y = y-a/2*sqrt(2); w = 1; % draw the first hexagon of each row Sechseck(n,x,y,w,a,s); x2 = x; % draw the whole row for l = 1:m x = x+a; w = nchoosek(m,l); Sechseck(n,x,y,w,a,s); end end else clf; text(-0.2,0.5,'Na, wir wollen mal nicht bertreiben!',... 'FontSize',23); end; title('Pascal Triangle'); hold off; axis square; axis equal; axis off;

[janshendry@gmail.com]

Page 5

[MATLAB BASIC] October 11, 2012

program untuk menampilkan lingkaran dalam struktur fractal.


function Sechseck(n,x,y,w,a,s) % % % % % % % SECHSECK(N,X,Y,W,A,S) draws a hexagon with the following parameters: n is the total size of Pascal's triangle. (x,y) is the middle point of the hexagon. w gives the number of this hexagon within Pascal's triangle. a is the length of one side of the hexagon. s is the divisor; if s|w, the colour of the hexagon is red, otherwise green.

% SECHSECK is used in the file Pascal.m. % This file was generated by students as a partial fulfillment % for the requirements of the course "Fractals", Winter term % 2004/2005, Stuttgart University. % Author : Sylvia Frey % Date : Nov 2004 % Version: 1.0 % Modify : Jans Hendry % Gadjah Mada University % items : modification to display circle instead of hexagon % % % % % calculate the co-ordinates of the nodes of the hexagon r = a/4*sqrt(2); phi = linspace(0,2*pi,7); d = r.*sin(phi)+x; e = r.*cos(phi)+y;

%% r = a/4*sqrt(2); NOP = 360; THETA = linspace(0,2*pi,NOP); X = x + r.*cos(THETA); Y = y + r.*sin(THETA); %% % plot the edges plot(X,Y,'k-'); % determine the colour if mod(w,s) == 0 patch(X,Y,'r') else patch(X,Y,'g') end; x = x-a/30; % center the text within the hexagon if w>9 x = x-a/10;

[janshendry@gmail.com]

Page 6

[MATLAB BASIC] October 11, 2012

end; if w>99 x = x-a/8; end; % write a number or not? if n<13 text(x,y,num2str(w), 'color', 'k'); end;

sekedar iseng untuk membuat sarang lebah menggunakan Matlab.

Untuk mendapatkan gambar di atas, anda tinggal mengubah fungsi pada contoh ke dua di atas menjadi rumus hexagonal pada Matlab.

@ thanks

[janshendry@gmail.com]

Page 7

You might also like