outline matlab

30
MATLAB

Upload: yahya-anggoro-efriza

Post on 27-Oct-2014

121 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Outline Matlab

MATLAB

Page 2: Outline Matlab

2

Outline Matlab

Bilangan & Operator

Window pada Matlab

Pendahuluan

Menyimpan & Memanggil Data

Fungsi Matematika Dasar

Komentar & Tanda Baca

Sumber Matlab di Internet

Operator Logika & Relasional

Page 3: Outline Matlab

3

Window pada Matlab (6)Window pada Matlab (6)

• Help Windows (lanjutan)– Bantuan informasi yang tersedia

Page 4: Outline Matlab

4

Bilangan dan Operator (1)Bilangan dan Operator (1)

• Tipe bilangan dalam Matlab– Bilangan bulat– Bilangan real– Bilangan kompleks

Page 5: Outline Matlab

5

Bilangan dan Operator (2)Bilangan dan Operator (2)

• Operator Aritmetika Dasar dalam MatlabOperasi Simbol

Penambahan, a+b +

Pengurangan, a-b -

Perkalian, axb *

Pembagian, a:b / atau \

Pemangkatan, ab ^

Page 6: Outline Matlab

6

Komentar dan Tanda BacaKomentar dan Tanda Baca

• Komentar menggunakan tanda persen (%)• Untuk menjelaskan apa yang dikerjakan• Tanda titik koma (;) digunakan agar hasilnya tidak ditampilkan

langsung

Page 7: Outline Matlab

7

Fungsi Matematika DasarFungsi Matematika Dasar

• MATLAB menyediakan fungsi yang biasa digunakan dalam matematika

• Contoh: nilai ∏, sinus, arc sin

Page 8: Outline Matlab

8

Menyimpan Data (1)Menyimpan Data (1)

• File Save Workspace As• Ketik nama file, misalnya: contoh1• Klik OK

Page 9: Outline Matlab

9

Menyimpan Data (2)Menyimpan Data (2)

• Ketik save namefile.mat

Page 10: Outline Matlab

10

Memanggil DataMemanggil Data

• Ketik load namefile.mat

Page 11: Outline Matlab

11

Operator Logika dan RelasionalOperator Logika dan Relasional

• Digunakan untuk menjawab pertanyaan dengan jawaban benar atau salah

• Untuk mengontrol urutan eksekusi

• Hasilnya memuat 1 (benar) atau 0 (salah)

Relasi Deskripsi

< Kurang dari

> Lebih dari

<= Kurang dari atau sama dengan

>= Lebih dari atau sama dengan

== Sama dengan

~= Tidak sama denganRelasi Deskripsi

& AND (dan)

| OR (atau)

~ NOT (negasi)

Page 12: Outline Matlab

12

Command Window

ctrl-p Recall previous line ctrl-n Recall next line ctrl-b Move back one character

ctrl-f Move forward one character

• ctrl - ctrl-r Move right one word• ctrl - ctrl-l Move left one word• home ctrl-a Move to beginning of line

• end ctrl-e Move to end of line• esc ctrl-u Clear line• del ctrl-d Delete character at cursor

• backspace ctrl-h Delete character before cursor• ctrl-k Delete to end of line

ctrl-p Recall previous line ctrl-n Recall next line ctrl-b Move back one character

ctrl-f Move forward one character

• ctrl - ctrl-r Move right one word• ctrl - ctrl-l Move left one word• home ctrl-a Move to beginning of line

• end ctrl-e Move to end of line• esc ctrl-u Clear line• del ctrl-d Delete character at cursor

• backspace ctrl-h Delete character before cursor• ctrl-k Delete to end of line

Page 13: Outline Matlab

13

MATLAB Graphics(1) - Creating a Plot

» t = 0:pi/100:2*pi;» y = sin(t);» plot(t,y)» grid» axis([0 2*pi -1 1])» xlabel('0 \leq \itangle \leq \pi')» ylabel('sin(t)')» title('Graph of the sine function')» text(1,-1/3,'\it{Demonstration of plotting}')

» t = 0:pi/100:2*pi;» y = sin(t);» plot(t,y)» grid» axis([0 2*pi -1 1])» xlabel('0 \leq \itangle \leq \pi')» ylabel('sin(t)')» title('Graph of the sine function')» text(1,-1/3,'\it{Demonstration of plotting}')

Page 14: Outline Matlab

14

Matlab Graphics

x = 0:pi/100:2*pi;

y = sin(x);

plot(x,y)

xlabel('x = 0:2\pi')

ylabel('Sine of x')

title('Plot of the Sine Function')

Page 15: Outline Matlab

15

Multiple Graphs

t = 0:pi/100:2*pi;

y1=sin(t);

y2=sin(t+pi/2);

plot(t,y1,t,y2)

grid on

Page 16: Outline Matlab

16

Multiple Plots

t = 0:pi/100:2*pi;

y1=sin(t);

y2=sin(t+pi/2);

subplot(2,2,1)

plot(t,y1)

subplot(2,2,2)

plot(t,y2)

Page 17: Outline Matlab

17

MATLAB Graphics(2) - Mesh & surface plots

» [X,Y] = meshgrid(-8:.5:8);» R = sqrt(X.^2 + Y.^2) + eps;» Z = sin(R)./R;» mesh(X,Y,Z)» text(15,10,'sin(r)/r')» title('Demo of 2-D plot');

» [X,Y] = meshgrid(-8:.5:8);» R = sqrt(X.^2 + Y.^2) + eps;» Z = sin(R)./R;» mesh(X,Y,Z)» text(15,10,'sin(r)/r')» title('Demo of 2-D plot');

Page 18: Outline Matlab

18

MATLAB Graphics(3) - Subplots» t = 0:pi/10:2*pi;» [X,Y,Z] = cylinder(4*cos(t));» subplot(2,2,1); mesh(X)» subplot(2,2,2); mesh(Y)» subplot(2,2,3); mesh(Z)» subplot(2,2,4); mesh(X,Y,Z)

» t = 0:pi/10:2*pi;» [X,Y,Z] = cylinder(4*cos(t));» subplot(2,2,1); mesh(X)» subplot(2,2,2); mesh(Y)» subplot(2,2,3); mesh(Z)» subplot(2,2,4); mesh(X,Y,Z)

Page 19: Outline Matlab

19

Graph Functions (summary)

• Plot : linear plot• Stem : discrete plot• Grid : add grid lines• Xlabel : add X-axis label• Ylabel : add Y-axis label• title : add graph title• Subplot : divide figure window • Figure : create new figure window• Pause : wait for user response

Page 20: Outline Matlab

20

Flow Control

if A > B

'greater'

elseif A < B

'less'

else

'equal'

end

for x = 1:10

r(x) = x;

end

• if statement• switch statement

• for loops• while loops

• continue statement• break statement

Page 21: Outline Matlab

21

close all;clear;clc;format compact; alpha = [0 45 90 135 180 225 270 315 360];x = alpha * pi/180;y = sin(x); disp('x = ');disp(x);disp('y = ');disp(y); plot(x, y);xlabel('x axis label');ylabel('y axis label');title('plot title');grid on;

Create a Matlab m-file with the lines shown:•initialization (close all previously open plot windows, clear workspace, clear command console, use compact output formatting)•create the row vector alpha containing a sequence of angles from 0 to 360 degrees on increments of 45 degrees •create a second row vector x with the angles converted from degrees to radians (note: this is a scalar multiplication of each element in the alpha vector to create the x vector)•calculate the “sine” of each angle and store the result in a third vector , y (note: the sin() function is applied to each element of the x vector to create the y vector)•display the values stored in x and y•plot y vs. x•add labels for the x and y axes•add a title for the plot•add grid lines

Page 22: Outline Matlab

22

• Save the m-file in your “current directory” as plot1.m

• Execute the m-file by typing prog1 in the command console

• You should see a new plot window open with the x-y plot shown at left

• Note the presence of the axis labels, plot title, and grid lines

• The command console should contain the output shown

• Note that since we divided the total 360 degree range of angles into 8 intervals of 45 degrees, there are a total of 8+1 or 9 values of alpha, x, and y

• With only 9 plot points and straight line segments connecting the points, the sine function plot has a “jagged” appearance – more plot points would help

0 1 2 3 4 5 6 7-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

x axis label

y ax

is la

bel

plot title

x = Columns 1 through 5 0 0.7854 1.5708 2.3562 3.1416 Columns 6 through 9 3.9270 4.7124 5.4978 6.2832y = Columns 1 through 5 0 0.7071 1.0000 0.7071 0.0000 Columns 6 through 9 -0.7071 -1.0000 -0.7071 -0.0000

Page 23: Outline Matlab

23

close all;clear;clc;format compact; alpha = [0:2:360];x = alpha * pi/180;y = sin(x); disp('x = ');disp(x);disp('y = ');disp(y); plot(x, y);xlabel('x axis label');ylabel('y axis label');title('plot title');grid on;

• Modify plot1.m as shown and save the new version as plot2.m

• The vector alpha is created using the “colon” operator to specify the sequence of angle values

• The syntax for a sequence specified by the colon operator is

alpha = [first:increment:last]• In this case, the sequence starts with 0, ends at 360,

and each value is separated by 2

Note: The sequence of values is generated by the

colon operator. Placing square braces [] around the operator

causes the creation of a vector to store the values.

The equal sign “assigns” the new vector to the variable alpha. The equal sign is an assignment operator, not a statement of equality.

Page 24: Outline Matlab

24

0 1 2 3 4 5 6 7-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

x axis label

y a

xis

label

plot title

x = Columns 1 through 6 0 0.0349 0.0698 0.1047 0.1396 0.1745 Columns 7 through 12 0.2094 0.2443 0.2793 0.3142 0.3491 0.3840

• Execute plot2.m from the console window and verify that you can get the results shown

Page 25: Outline Matlab

25

Bagaimana Melihat Perilaku Dinamik Proses?

PROSES(Dinamik)

PersamaanDifferensial

FungsiTransformasiLAPLACE F(s)

SolusiNUMERIK

Pemodelan Teorema TL

EulerRK, dll

FUNGSIWAKTU f(t)

Ekspansidan TLB

Input:Sinyal uji(step, ramp, dll)

RESPONDINAMIK

MATLAB

Linearisasi

FUNGSI ALIH

Page 26: Outline Matlab

26

Sumber MATLAB di InternetSumber MATLAB di Internet

• Website MathWorks– http://www.mathworks.com

• Newsgroup MATLAB– news://salukinews.siu.edu/comp.soft-sys.MATLAB/

• Link untuk MATLAB– http://www.cse.uiuc.edu/cse301/matlab.html

• Mastering MATLAB Website– http://www.eece.maine.edu/mm

Page 27: Outline Matlab

27

Matlab homepage (news & more):http://www.mathworks.com/

online tutorials:http://www.engin.umich.edu/group/ctm/http://www.math.mtu.edu/~msgocken/intro/intro.html

you can find all this at:http://www.soton.ac.uk/~jowa/teaching.html

Page 28: Outline Matlab

28

SOAL-SOAL dan PENGAYAAN

Page 29: Outline Matlab

29

1. Buatlah program untuk menghitung keliling lingkran. Data input dibaca dari keyboard, output dituliskan pada layar.Objektif : Menghitung keliling lingkaranInput : Jari-jari lingkaran (R)Output : Keliling lingkaranRumus : Keliling = 2*phi*R

dimana phi = 3,14Contoh : Jika R = 10 satuan, maka

Keliling = 62,83 satuan

2. Buatlah program untuk mengkonversi nilai suhu dari satuan Celcius ke satuan Fahrenheit. Data input dibaca dari keyboard, hasilnya dituliskan di layar.Objektif : Konversi nilai suhuInput : : Suhu dalam satuan Celcius (C)Output : Suhu dalam satuan Fahrenheit (F)Rumus : F = 9/5 C + 32

dimana phi = 3,14Contoh : Jika C = 10 derajat Celcius, maka

maka F = 50 derajat Fahrenheit3. Buatlah program untuk mengkonversi jumlah Jam, Menit, dan Detik yang diberikan menjadi

Detik. Data input dibaca dari keyboard, output dituliskan di layar.Objektif : Konversi jumlah waktuInput : : Durasi waktu dalam Jam, Menit, DetikOutput : Durasi waktu dalam detikRumus : Contoh : Jika diberikan 1 jam, 1 menit, 8 detik, maka

maka nilainya sama dengan 3668 detik.

Page 30: Outline Matlab

30

DAFTAR PUSTAKA

• Arhami M dan Anita Desiani. 2005. Pemrograman MATLAB. Penerbit Andi Yogyakarta

• Joghiyanto. 1997. Turbo Pascal 5. Edisi 4. Penerbit Andi Yogyakarta