Professional Documents
Culture Documents
Scalars
>> a=4
a=
4
>> A=6
A=
6
>> A=6;
>> a=4, A=6; x=1;
a=
4
>> a
a=
4
>> A
A=
6
>> x=2+i*4
x=
2.0000 + 4.0000i
>> pi
ans =
3.1416
>> format long
>> pi
ans =
3.14159265358979
>> format short
>> pi
ans =
3.1416
Assignment (cont.)
Vectors
>> a=[1 2 3 4 5]
a=
1 2 3 4
>> b=[2;4;6;8;10]
b=
2
4
6
8
10
>> b=[2;4;6;8;10]'
b=
2 4 6 8 10
transpose
Matrices
Assignment (cont.)
>> A=[1 2 3; 4 5 6; 7 8 9]
A=
1 2 3
4 5 6
7 8 9
>> whos
Name
Size
Bytes Class
A
3x3
72 double array
a
1x5
40 double array
ans
1x1
8 double array
b
5x1
40 double array
x
1x1
16 double array (complex)
Grand total is 21 elements using 176 bytes
>> b
b=
2
4
6
8
10
>> b(4)
ans =
8
>> A(2,3)
ans =
6
>> E=zeros(2,3)
E=
0 0 0
0 0 0
>> u=ones(1,3)
u=
1 1 1
Assignment (cont.)
Colon Operator
>> t=1:5
t=
1 2
>> t=1:0.5:5
t=
Columns 1 through 6
1.0000 1.5000 2.0000 2.5000 3.0000 3.5000
Columns 7 through 9
4.0000 4.5000 5.0000
>> t=1:0.5:3
t=
1.0000 1.5000
>> t=10:-1:5
t=
10 9 8
>> A
A=
1 2
4 5
7 8
3
6
9
>> A(2,:)
ans =
4 5 6
>> t(2:4)
ans =
9 8 7
Assignment (cont.)
linspace and logspace Function
>> linspace(0,1,6)
ans =
0 0.2000 0.4000
>> linspace(0,1)
(default is 100 elements)
>> logspace(-1,2,4)
ans =
0.1000 1.0000 10.0000 100.0000
Mathematical Operation
>> 2*pi
ans =
6.2832
>> y=pi/4
y=
0.7854
>> y^2.45
ans =
0.5533
>> y=-4^2
y=
-16
>> y=(-4)^2
y=
16
>> x
x=
2.0000 + 4.0000i
>> 3*x
ans =
6.0000 +12.0000i
>> 1/x
ans =
0.1000 - 0.2000i
>> x^2
ans =
-12.0000 +16.0000i
>> x+y
ans =
18.0000 + 4.0000i
>> b*a
ans =
2 4
4 8
6 12
8 16
10 20
6
12
18
24
30
8 10
16 20
24 30
32 40
40 50
3
6
9
>> a*A
ans =
30 36 42
>> A*b
ans =
32
77
122
>> A*A
ans =
30 36 42
66 81 96
102 126 150
>> A/pi
ans =
0.3183 0.6366 0.9549
1.2732 1.5915 1.9099
2.2282 2.5465 2.8648
>> A^2 (^ matrix
ans =
30 36 42
66 81 96
102 126 150
operation)
operation)
Built-In Function
>> A=[1 2 3; 4 5 6; 7 8 9]
A=
1 2 3
4 5 6
7 8 9
>> log(A) (natural log)
ans =
0 0.6931 1.0986
1.3863 1.6094 1.7918
1.9459 2.0794 2.1972
>> sqrt(A)
ans =
1.0000
2.0000
2.6458
>> abs(A)
ans =
1 2 3
4 5 6
7 8 9
>> sin(A)
ans =
0.8415 0.9093 0.1411
-0.7568 -0.9589 -0.2794
0.6570 0.9894 0.4121
(element-by-element operation)
1.4142 1.7321
2.2361 2.4495
2.8284 3.0000
0 - 1.3170i
0 - 2.2924i
0 - 2.7687i
0 - 1.7627i
0 - 2.4779i
0 - 2.8873i
>> tanh(A)
ans =
0.7616 0.9640 0.9951
0.9993 0.9999 1.0000
1.0000 1.0000 1.0000
>> sqrtm(A) (matrix operation)
ans =
0.4498 + 0.7623i 0.5526 + 0.2068i 0.6555 - 0.3487i
1.0185 + 0.0842i 1.2515 + 0.0228i 1.4844 - 0.0385i
1.5873 - 0.5940i 1.9503 - 0.1611i 2.3134 + 0.2717i
User-Defined Function
>> t=[0:2:20]'
t=
0
2
4
6
8
10
12
14
16
18
20
gc d
)
m
Graphics
Plot
Plotofofv vversus
versust t
50
50
50
40
40
40
>> grid
>> plot(t,v,'o')
>> plot(t,v,t,v,'o')
Values of v
Values of v
>> plot(t,v)
60
60
60
30
30
30
20
20
20
10
10
10
00 0
00 0
55 5
10
10
10
Values
Valuesofoft t
15
15
15
20
20
20
Try again:
freefallvel(100, 0.25, 8)
Also try:
help freefallvel
function name
i =1
( xi x ) 2
( n 1)
Structured Programming
If command:
function gout=grader(grade)
if grade >= 60
conditional statement
disp('passing grade:')
disp(grade)
end
If command with error messages:
function f=errortest(x)
if x == 0
conditional statement
error('zero value encountered')
end
f=1/x
fout = n!
Get Organized!
function quad=quadroots(a,b,c)
if a == 0
if b ~= 0
r1=-c/b
else
error('Trivial Solution')
end
else
d=sqrt(b^2-4*a*c);
r1=(-b+d)/(2*a)
r2=(-b-d)/(2*a)
end
f ( x ) = ax 2 + bx + c
x1, 2
b b 2 4ac
=
2a
Function in a Function
Execute an M file (sub program) within an M file (sub program)
funcavg M file
function favg = funcavg(a,b,n)
x=linspace(a,b,n)
y=feval('func',x)
favg=mean(y)
func M file
function f=func(x)
f=0.125*x.^3-1.125*x.^2+2.75*x+1;