MATLAB Chapter 1
MATLAB Chapter 1
MATLAB Chapter 1
An Overview of MATLAB
MATLAB
1
Spring 2015
2
Spring 2015
MATLAB in University/Industry
This window
shows the current
directory or the This is the
workspace command
This window window
shows the
command
history
3
Spring 2015
4
Spring 2015
1.1 Variable
ans =
4
>>
9
1.1 Variable
5
Spring 2015
11
Appendix A
12
6
Spring 2015
- 5 - 4 = 1
See Table 1.1-2 for the
order of precedence. * 5 * 4 = 20
/ 5 / 4 = 1.25
13
14
7
Spring 2015
Example 1.1-1
Volume of a circular
>> r=8;
cylinder:
>> h=15;
Given radius r and >> V=pi*r^2*h;
height h and volume >> V=V+0.2*V;
V=pi*r^2*h of a cylinder, >> r=sqrt(V/(pi*h))
find radius of another
cylinder with the same r =
height and a volume
8.7636
20% greater.
Notice: Using a semicolon >>
“;” after a command
removes screen printing. Notice that new values
of r and V replace old values
15
16
8
Spring 2015
Instead of retyping the entire line, press the up-arrow () and the
down-arrow () to recall previous commands you typed in your
session.
17
9
Spring 2015
19
ans =
-0.8276 - 1.0690i
>> (-3+7i)*(-3-7i)
ans =
58
20 >>
10
Spring 2015
21
22
11
Spring 2015
>> x=[0 1 2 3]
Matlab can handle collections of
numbers, called arrays, as if they x =
were a single variable.
0 1 2 3
>> x=[0,1,2,3]
Possible way to define an array
elements must be separated by a x =
comma or space (or both). 0 1 2 3
>>
Order of elements is important.
23
1.3 Arrays
>> x=2:8
It is not necessary to x =
24
12
Spring 2015
y =
0 0.8415 0.9093 0.1411 Note: Matlab
>> z=cos(x) trigonometric
functions use
z =
radian measure.
Using degrees can
1.0000 0.5403 -0.4161 -0.9900 be done by hand:
>> y+z cosine 60° is
cos(60*pi/180)
ans =
>>
25
1.3 Arrays
26
13
Spring 2015
1.3 Arrays
>> x=0:3;
>> y=sin(x)
y =
>> z=cos(x)
z =
>> y.^2+z.^2
ans =
>> y^2+z^2
??? Error using ==> mpower
Matrix must be square.
>>
27
28
14
Spring 2015
29
1 2 4
30 2 4 5
15
Spring 2015
ans =
To find the polynomial from its roots, use
the function poly([root1,root2,…]) 1 -7 40 -34
>>
31
32
16
Spring 2015
1.3 MAT-files
33
1.3 MAT-files
You can define your own MAT-file by using
save filename and load filename
The variables are then stored in the file filename.mat
34
17
Spring 2015
1.3 M-files
35
36
18
Spring 2015
37
38
19
Spring 2015
>> x=0:0.1:10;
>> y=sin(x);
>> plot(x,y)
39
40
20
Spring 2015
This Figure1.fig
file can be saved
under a different
name and format
for usage within
other programs
like MS Word.
41
Example:
plot(x,y),xlabel(‘time(s)’),ylabel(‘voltage(V)’),title(‘V vs. t’)
42
21
Spring 2015
You can create multiple plots by including another set of values in the plot:
plot(x,y1,x,y2,x,y3)
Typing plot(x,y,’+’) will return a scatter plot without a line, where the
data points are represented by a “+” sign.
Solution =
3
5
44 -2
22
Spring 2015
1.3 Statistics
ans = ans =
0 5
ans = ans =
10 3.3166
45
% is used to write
comments (will not be
compiled). Text color in M-
file editor after % is green.
Semicolons “;” can be
used as usual.
Filenames can be typed
directly in the command
window, if their directory is
in the search path.
Writing type filename
in the command editor
displays the file in the See page 31-32 in TB on what to
command window. remember when using script files
46
23
Spring 2015
If due to a certain mistake, you would like to stop the program from
running, press
Ctrl+c
47
24
Spring 2015
49
25
Spring 2015
ans =
>> x='m';
>> y='m';
>> x==y
ans =
>>
51
z = ans =
1 0 0 1 1 1
0 1 1
>> z=(x==y)
>> C=A~=B
z =
C =
0 0 1
0 0 0
>> z=x==y %another way to write it 1 0 0
z = >>
0 0 1
52
26
Spring 2015
>> x=[2,3,1];
>> y=[1,2,5];
>> x(x>y)
ans =
2 3
>>
53
>> x=[1,2,9,0,3,0,3];
>> y=find(x)
y =
Only
1 2 3 5 7 indices are
returned.
54
27
Spring 2015
56
28
Spring 2015
57
1.6 Loops
58
29
Spring 2015
1.6 Loops
Syntax:
59
60
30
Spring 2015
1.6 Loops
for-loop example:
%first method %second method
m=0; m=1;
x(1)=10; x(m)=10;
for k=[2:3:11] for k=2:3:11
m=m+1; x(m+1)=x(m)+k^2;
x(m+1)=x(m)+k^2; m=m+1;
end end
x x
>> x
x =
10 14 39 103 224
61
1.6 Loops
while-loop example:
62
31
Spring 2015
15 4x 10 if x 9
y(x) 10x 10 if 0 x 9
10
if x 0
63
k=k+1;
end
64 plot(x,y),xlabel('x'),ylabel('y')
32
Spring 2015
65
%Example 1.6-3: using for loop %Example 1.6-3: using while loop
clear clear
total1=0; total3=0;
k=1:15; k=1:15;
y=5*(k.^2)-2*k; y=5*(k.^2)-2*k;
33
Spring 2015
67
68
34
Spring 2015
69
%Example 6.1-5
clear
>> Example_1_6_5
amount=500;
years=0;
amount =
while amount<1e4
1.0789e+004
amount=amount*1.05+500;
%or amount=amount+500+amount*0.05
years=years+1;
end years =
amount
years 14
70
35