pemrograman komputer - 03 matlab
Post on 08-Apr-2018
256 Views
Preview:
TRANSCRIPT
-
8/6/2019 Pemrograman Komputer - 03 Matlab
1/42
Matlab
-
8/6/2019 Pemrograman Komputer - 03 Matlab
2/42
Matlab
1. the command window,
2. the command-history window,
3. the current directory window or (hidden in this
view) the workspace(variable window),
4. the file information window,
5. the icon toolbar with the choice menu for the
current directory,
6. the shortcut toolbar, and7. the start button.
-
8/6/2019 Pemrograman Komputer - 03 Matlab
3/42
1. the command window,
2. the command-history window,
3. the current directory window or (hidden in this view) the
workspace(variable window), 4. the file information window,
5. the icon toolbar with the choice menu for the current
directory,
6. the shortcut toolbar, and
7. the start button.
-
8/6/2019 Pemrograman Komputer - 03 Matlab
4/42
Arithmetic
Penambahan +
Pengurangan -
Perkalian * Pembagian /
Pangkat
-
8/6/2019 Pemrograman Komputer - 03 Matlab
5/42
Operation Algebraic form Matlab Example
Addition a + b a + b 5 +3
Subtraction a b a - b 23-12
Multiplication a b a * b 3.14*0.85
Right division a b a / b 56/8
Left division b a a \ b 8\56
Exponentiation ab a b 5
-
8/6/2019 Pemrograman Komputer - 03 Matlab
6/42
Arithmetic
>> 32 - (5 + 4)/2 + 6*3
ans =
22.5000
>> ans2 + sqrt(ans)
ans =
510.9934
>> u = cos(10)
u =
-0.8391
>> v = sin(10)
v =
-0.5440
>> u2 + v2
ans =
1
-
8/6/2019 Pemrograman Komputer - 03 Matlab
7/42
Aljabar
Perhitungan dgn simbol gunakansyms untuk variabel
>> syms x y
>> (x - y)*(x - y)2
ans =
(x-y)^3
>> expand(ans)
ans =
x^3-3*x^2*y+3*x*y^2-y^3
>> factor(ans)
ans =
(x-y)^3
Penyederhanaan
>> simplify((x3 - y3)/(x - y))
ans =
x^2+x*y+y^2
The command expand
told MATLAB to multiply out theexpression, and
factor forced MATLAB
to restore it to factored form.
-
8/6/2019 Pemrograman Komputer - 03 Matlab
8/42
Aljabar
>> cos(pi/2)
ans =
6.1232e-17
>> cos(sym(pi/2))ans =
0
>> sym(1/2) + sym(1/3)
ans =
5/6
variable-precisionarithmetic vpa.
50 digits 2
>> vpa(sqrt(2), 50)
ans =
1.41421356237309504
88016887242096980785696718753769
-
8/6/2019 Pemrograman Komputer - 03 Matlab
9/42
Variables and Assignments
>> x = 7
x =
7
variable x akan bernilai 7 kapanpun MATLABmelihat huruf x
Misal y adalah symbolic:
>> x2 - 2*x*y + yans =
49-13*y
-
8/6/2019 Pemrograman Komputer - 03 Matlab
10/42
To clear the value of the variable x, type clear x.
You can make very general assignments for symbolicvariables and then manipulate them. For example,
>> clear x; syms x y>> z = x2 - 2*x*y + y
z =
x^2-2*x*y+y
>> 5*y*zans =
5*y*(x^2-2*x*y+y)
-
8/6/2019 Pemrograman Komputer - 03 Matlab
11/42
Penyelesaian Persamaan
Gunakan solve or fzero.
Contoh: x2 2x 4 = 0
>> solve(x2 - 2*x - 4 = 0)ans =
[ 5^(1/2)+1]
[ 1-5^(1/2)]
-
8/6/2019 Pemrograman Komputer - 03 Matlab
12/42
>> [x, y] = solve(x2 - y = 2, y - 2*x = 5)
x =
[ 1+2*2^(1/2)]
[ 1-2*2^(1/2)]
y =
[ 7+4*2^(1/2)]
[ 7-4*2^(1/2)]
>> x(1)
ans =
1+2*2^(1/2)
>> y(1)
ans =
7+4*2^(1/2)
The second solution can be extracted with x(2) and y(2).
-
8/6/2019 Pemrograman Komputer - 03 Matlab
13/42
Coba
>> x= 1.23
x=1.23
>> vector = [1 2 -3]
vector =
1 2 -3
>> thematrix = [3 1+2*i 2; 8 0 -5]
thematrix =3.0000 1.0000 + 2.0000i 2.0000
8.0000 0 -5.0000
-
8/6/2019 Pemrograman Komputer - 03 Matlab
14/42
>> colvector = [2; 4; 3; -1; 1-4*j]
colvector =
2.00004.0000
3.0000
-1.00001.0000 - 4.0000i
-
8/6/2019 Pemrograman Komputer - 03 Matlab
15/42
>> [2,3,4; 3,-1,0]
ans =
2 3 43 -1 0
-
8/6/2019 Pemrograman Komputer - 03 Matlab
16/42
>> who
Your variables are:
ans colvector thematrix vector x
-
8/6/2019 Pemrograman Komputer - 03 Matlab
17/42
>> whos
Name Size Bytes Class
ans 2x3 48 double array
colvector 5x1 80 double array (complex)
thematrix 2x3 96 double array (complex)
vector 1x3 24 double array
x 1x1 8 double array
Grand total is 21 elements using 256 bytes
-
8/6/2019 Pemrograman Komputer - 03 Matlab
18/42
The workspace browser
-
8/6/2019 Pemrograman Komputer - 03 Matlab
19/42
Representation of a matrix in the
array editor
-
8/6/2019 Pemrograman Komputer - 03 Matlab
20/42
-
8/6/2019 Pemrograman Komputer - 03 Matlab
21/42
>> thematrix = [thematrix; 1 2 3]
thematrix =
3.0000 1.0000 + 2.0000i 2.00004.0000 0 -5.0000
1.0000 2.0000 3.0000
-
8/6/2019 Pemrograman Komputer - 03 Matlab
22/42
>> thematrix = [thematrix, [1;2;3]]
thematrix =
3.0000 1.0000 + 2.0000i 2.0000 1.00004.0000 0 -5.0000 2.0000
1.0000 2.0000 3.0000 3.0000
-
8/6/2019 Pemrograman Komputer - 03 Matlab
23/42
>> v = [1;2;3]
v =
1
2
3
>> thematrix = [thematrix, v]
thematrix =
3.0000 1.0000 + 2.0000i 2.0000 1.0000
4.0000 0 -5.0000 2.0000
1.0000 2.0000 3.0000 3.0000
-
8/6/2019 Pemrograman Komputer - 03 Matlab
24/42
>> thematrix(:,2) = [] %menghapus kolom 2
thematrix =
3 2 1
4 -5 2
1 3 3
>> thematrix(1,:) =[] %menghapus baris 1
thematrix =
4 -5 2
1 3 3
-
8/6/2019 Pemrograman Komputer - 03 Matlab
25/42
Vector Step 2
>> largevector = (0:2:5000)
largevector =
Columns 1 through 12
0 2 4 6 8 10 12 14 16 ...Columns 13 through 24
24 26 28 30 etc.
-
8/6/2019 Pemrograman Komputer - 03 Matlab
26/42
Memilih baris tertentu
>> firstrow = thematrix(1,:)
firstrow =
4 -5 2
-
8/6/2019 Pemrograman Komputer - 03 Matlab
27/42
The Colon Operator
The expression
1:10
is a row vector containing the integers from 1 to 10
1 2 3 4 5 6 7 8 9 10
To obtain non unit spacing, specify an increment. For example,
100:-7:50
is
100 93 86 79 72 65 58 51
and
0:pi/4:piis
0 0.7854 1.5708 2.3562 3.1416
-
8/6/2019 Pemrograman Komputer - 03 Matlab
28/42
>> t=(0:1:5) % (0= start, 1 = sela, 5=end)
t =
0 1 2 3 4 5
>> s=sin(t)
s =
0 0.8415 0.9093 0.1411 -0.7568 -0.9589
The sine of the specified vector t is again a vector,specifically, the vector
s = ( sin (0), sin (1), sin (2), , sin (5)) .
-
8/6/2019 Pemrograman Komputer - 03 Matlab
29/42
plot
>> t = (0:1:5);
>> s = sin(t);
>> plot(t,s)
-
8/6/2019 Pemrograman Komputer - 03 Matlab
30/42
Creating a Plot
The plot function has different forms, depending on the input arguments.
If y is a vector, plot(y) produces a piecewise linear graph of the elements of y
versus the index of the elements of y.
If you specify two vectors as arguments, plot(x,y) produces a graph of y versusx.
For example, these statements use the colon operator to create a vector of x
values ranging from zero to , compute the sine of these values, and plot the
result.
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)
-
8/6/2019 Pemrograman Komputer - 03 Matlab
31/42
Creating a Plot
Now label the axes and add a title.
The characters \pi create the symbol .
xlabel('x = 0:2\pi')
ylabel('Sine of x')
title('Plot of the Sine Function','FontSize',12)
-
8/6/2019 Pemrograman Komputer - 03 Matlab
32/42
-
8/6/2019 Pemrograman Komputer - 03 Matlab
33/42
Multiple Data Sets in One Graph
Multiple x-y pair arguments create multiple graphswith a single call to plot.
MATLAB automatically cycles through a predefined(but user settable) list of colors to allow discrimination
between each set of data. For example, these statements plot three related
functions of x, each curve in a separate distinguishingcolor.
y2 = sin(x-.25);
y3 = sin(x-.5);
plot(x,y,x,y2,x,y3)
-
8/6/2019 Pemrograman Komputer - 03 Matlab
34/42
Multiple Data Sets in One Graph
Legend
legend('sin(x)','sin(x-.25)','sin(x-.5)')
-
8/6/2019 Pemrograman Komputer - 03 Matlab
35/42
Specifying Line Styles and Colors
It is possible to specify color, line styles, and
markers (such as plus signs or circles) when
you plot your data using the plot command.
plot(x,y,'color_style_marker')
Color strings:
'c', 'm', 'y', 'r', 'g', 'b', 'w', and 'k'.
cyan, magenta, yellow, red, green, blue, white, and
black.
-
8/6/2019 Pemrograman Komputer - 03 Matlab
36/42
Line & Marker
Linestyle strings:
'-' for solid,
'--' for dashed,
':' for dotted,'-.' for dash-dot,
'none' for no line.
The marker types:'+', 'o', '*', and 'x'
the filled marker types
's for square,
'd' for diamond,
'^' for up triangle,'v' for down triangle,
'> for right triangle,
'
-
8/6/2019 Pemrograman Komputer - 03 Matlab
37/42
Plotting Lines and Markers
>>plot(x,y,'ks') plots black squares at each data point, but does not connect the
markers with a line.
>>plot(x,y,'r:+')
plots a red dotted line and places plus sign markers at each datapoint.
This example plots the data twice using a different number ofpoints for the dotted line and marker plots.
x1 = 0:pi/100:2*pi;x2 = 0:pi/10:2*pi;
plot(x1,sin(x1),'r:',x2,sin(x2),'r+')
-
8/6/2019 Pemrograman Komputer - 03 Matlab
38/42
-
8/6/2019 Pemrograman Komputer - 03 Matlab
39/42
Axis Labels and Titles
The xlabel, ylabel, and zlabel commands add
x-, y-, and z-axis labels.t = -pi:pi/100:pi;
y = sin(t);plot(t,y)
axis([-pi pi -1 1])
xlabel('-\pi \leq {\itt} \leq \pi')
ylabel('sin(t)')title('Graph of the sine function')
text(1,-1/3,'{\itNote the odd symmetry.}')
-
8/6/2019 Pemrograman Komputer - 03 Matlab
40/42
-
8/6/2019 Pemrograman Komputer - 03 Matlab
41/42
-
8/6/2019 Pemrograman Komputer - 03 Matlab
42/42
top related