Anda di halaman 1dari 42

Matlab

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, and
7. the start button.
• 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.
Arithmetic
• Penambahan +
• Pengurangan -
• Perkalian *
• Pembagian /
• Pangkat ˆ
• 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^
Arithmetic
>> 3ˆ2 - (5 + 4)/2 + 6*3 >> v = sin(10)
ans = v=
22.5000 -0.5440

>> ansˆ2 + sqrt(ans) >> uˆ2 + vˆ2


ans = ans =
510.9934 1

>> u = cos(10)
u=
-0.8391
Aljabar
• Perhitungan dgn simbol gunakan Penyederhanaan
syms untuk variabel
>> simplify((xˆ3 - yˆ3)/(x - y))
>> syms x y ans =
>> (x - y)*(x - y)ˆ2 x^2+x*y+y^2
ans =
(x-y)^3 • The command expand
told MATLAB to multiply out the
>> expand(ans) expression, and
ans = • factor forced MATLAB
x^3-3*x^2*y+3*x*y^2-y^3 to restore it to factored form.

>> factor(ans)
ans =
(x-y)^3
Aljabar
>> cos(pi/2) • variable-precision
ans = arithmetic vpa.
6.1232e-17 • 50 digits √2
>> cos(sym(’pi/2’))
ans = >> vpa(’sqrt(2)’, 50)
0 ans =
>> sym(’1/2’) + sym(’1/3’) 1.41421356237309504
ans = 8801688724209698078
5696718753769
5/6
Variables and Assignments
>> x = 7
x=
7
variable x akan bernilai 7 kapanpun MATLAB
melihat huruf x
Misal y adalah symbolic:
>> xˆ2 - 2*x*y + y
ans =
49-13*y
• ➱ To clear the value of the variable x, type clear x.
• You can make very general assignments for symbolic
variables and then manipulate them. For example,
>> clear x; syms x y
>> z = xˆ2 - 2*x*y + y
z=
x^2-2*x*y+y
>> 5*y*z
ans =
5*y*(x^2-2*x*y+y)
Penyelesaian Persamaan
• Gunakan solve or fzero.
• Contoh: x2 − 2x − 4 = 0
>> solve(’xˆ2 - 2*x - 4 = 0’)
ans =
[ 5^(1/2)+1]
[ 1-5^(1/2)]
>> [x, y] = solve(’xˆ2 - 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).
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
>> colvector = [2; 4; 3; -1; 1-4*j]
colvector =
2.0000
4.0000
3.0000
-1.0000
1.0000 - 4.0000i
>> [2,3,4; 3,-1,0]
ans =
2 3 4
3 -1 0
>> who
Your variables are:
ans colvector thematrix vector x
>> 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


The workspace browser
Representation of a matrix in the
array editor
Erase variabel
>> clear thematrix
>> who
Your variables are:
ans colvector vector x
>> thematrix = [thematrix; 1 2 3]
thematrix =
3.0000 1.0000 + 2.0000i 2.0000
4.0000 0 -5.0000
1.0000 2.0000 3.0000
>> thematrix = [thematrix, [1;2;3]]
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
>> 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
>> 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
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.
Memilih baris tertentu
>> firstrow = thematrix(1,:)
firstrow =
4 -5 2
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:pi
is
0 0.7854 1.5708 2.3562 3.1416
>> 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)) .


plot
>> t = (0:1:5);
>> s = sin(t);
>> plot(t,s)
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 versus
x.

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)
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)
Multiple Data Sets in One Graph
• Multiple x-y pair arguments create multiple graphs
with 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 distinguishing
color.
y2 = sin(x-.25);
y3 = sin(x-.5);
plot(x,y,x,y2,x,y3)
Multiple Data Sets in One Graph
• Legend
legend('sin(x)','sin(x-.25)','sin(x-.5)')
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.
Line & Marker
•Linestyle strings: the filled marker types
'-' for solid, 's’ for square,
'--' for dashed, 'd' for diamond,
':' for dotted, '^' for up triangle,
'-.' for dash-dot, 'v' for down triangle,
'none' for no line. '>’ for right triangle,
'<' for left triangle,
•The marker types: 'p' for pentagram,
'+', 'o', '*', and 'x' 'h' for hexagram,
none for no marker.
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 data
point.

• This example plots the data twice using a different number of


points 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+')
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.}')

Anda mungkin juga menyukai