Matlab
>> 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.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
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.