Introduction To MATLAB
Introduction To MATLAB
Editor Window
Command
History
Command
Window
Example:
Compute sin 45𝑜 2
+ cos 45𝑜 2
plot(x,y) 3.5
2.5
1.5
0.5
0
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
plot(x,y)
3.5
2.5
1.5
0.5
0
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
x = linspace(0,2*pi,100); 30
20
y = (x.^2).*cos(x); 10
g = x.*cos(x); 0
-10
f = (2.^x).*sin(x); -20
plot(x,y,x,g,x,f) -30
-40
0 1 2 3 4 5 6 7
30
plot(x,y,’k:p’,x,g,’r-o’,x,f,’b-.x) 20
10
-10
-20
-30
-40
0 1 2 3 4 5 6 7
0.8
0.6
0.4
x=linspace(0,2*pi,150); 0.2
-0.2
plot(x,cos(x));
-0.4
-0.6
1
-0.8
0.8
-1
hold on
0 1 2 3 4 5 6 7 0.6
0.4
0.2
plot(x,sin(x)); -0.2
-0.4
-0.6
100
hold off 80
60
40
-0.8
-1
0 1 2 3 4 5 6 7
plot(x,tan(x))
20
-20
-40
-60
-80
-100
0 1 2 3 4 5 6 7
0.8
0.6
0.4
Example: 0.2
x=linspace(0,2*pi,150); -0.2
-0.4
plot(x,cos(x),'r*',x,sin(x),'k') -0.6
-0.8
1
0.8
-1
0 1 2 3 4 5 6 7
0.6
0.4
0.2
grid
0
-0.2
xlabel('x-axis')
0.2
y-axis
0
-0.2
ylabel('y-axis') -0.4
-0.6
-1
0 1 2 3 4 5 6 7
x-axis
Example: 7
n=-3:3;
6
f=n.^2; 4
stem(n,f) 2
0
-3 -2 -1 0 1 2 3
if condition1
Statements
elseif condition2
Statements
else
Statements
end
syms t y 0.35
0.3
y=t*exp(-t); 0.25
0.15
y1=subs(y,t,t1); 0.1
plot(t1,y1) 0.05
0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5