Anda di halaman 1dari 25

Assignment

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

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

2.0000 2.5000 3.0000

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

0.6000 0.8000 1.0000

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

Mathematical Operation - (cont.)


>> a
a=
1 2
>> b
b=
2
4
6
8
10
>> a*b
ans =
110

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

Mathematical Operation - (cont.)


>> a=[1 2 3]
a=
1 2 3
>> b=[4 5 6]'
b=
4
5
6
>> A
A=
1 2
4 5
7 8

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)

>> A.^2 (.^ element


ans =
1 4 9
16 25 36
49 64 81

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

Built-In Function - (cont.)


>> acos(A)
ans =
0
0 - 2.0634i
0 - 2.6339i

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

>> g=9.81, m=68.1, cd=0.25


mg
v(t ) =
tanh(t
g=
cd
9.8100
m=
68.1000
>> v=sqrt(g*m/cd)*tanh(t*sqrt(g*cd/m))
cd =
v=
0.2500
0
18.7292
33.1118
42.0762
46.9575
49.4214
50.6175
51.1871
51.4560
51.5823
51.6416

gc d
)
m

Graphics
Plot
Plotofofv vversus
versust t

>> title('Plot of v versus t')

50
50
50

>> xlabel('Values of t')


>> ylabel('Values of v')

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

Script File (Main Program)


File, New -> Command Editor Window
Enter Commands:
t=[0:2:20]
g=9.81
m=68
m=68.1
cd=0.25
v=sqrt(g*m/cd)*tanh(sqrt(g*cd/m)*t)
Save the File: Give a filename
The default file extension: .m

Script File (Main Program) cont.


Two Ways to Execute the Script file:
(1) from the Command Editor Window
Debug, Run (Save and Run)
(2) from Command Window
Enter the filename of the Script file

Function File (Sub-Program)


Open a new file (Command Editor Window pop-up)
Enter:
function velocity = freefallvel(m, cd, t)

% freefallvel(m, cd, t) computes the free-fall velocity (m/s)


%
of an object with second-order drag.
% input:
% m = mass (kg)
% cd = second-order drag coefficient (kg/m)
% t = time (s)
% output:
% velocity = downward velocity (m/s)
%
g=9.81; % acceleration of gravity
velocity = sqrt(g*m/cd)*tanh(sqrt(g*cd/m)*t);

Function File (Sub-Program) cont.


A function file must start with the word function
function name (any name other
Output Variable
than the name of built-in
functions in MatLab)
function velocity = freefallvel(m, cd, t)
Input arguments
% freefallvel(m, cd, t) computes the free-fall velocity (m/s)
Comments %
of an object with second-order drag.
(will be
% input:
displayed
% m = mass (kg)
when help
% cd = second-order drag coefficient (kg/m)
function
name
% t = time (s)
(velocity)
% output:
command is
% velocity = downward velocity (m/s)
entered)
(comment can be appended
%
to the command)
g=9.81 % acceleration of gravity
Commands
velocity = sqrt(g*m/cd)*tanh(sqrt(g*cd/m)*t)

Function File (Sub-Program) cont.


To execute the function file (M file)
At the Command Window enter:
freefallvel(68.1, 0.25, 12)

Try again:
freefallvel(100, 0.25, 8)

Also try:
help freefallvel

Function File with More Than One


Output Variables
2 Output Variables

function name

function [mean, stdev] = stats(x)


Input argument
n=length(x);
mean=sum(x)/n;
stdev=sqrt(sum((x-mean).^2/(n-1)));
n

i =1

( xi x ) 2
( n 1)

Function File with More Than One


Output Variables cont.
To execute the function file stats (M file):
y=[8 5 10 12 6 7.5 4]
stats(y) (only mean is output)
[m,s]=stats(y) (both mean and std deviation are output)

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

Structured Programming cont.


If-Else command:
function sgn = mysign (x)
if x>0
sgn=1
else
sgn=-1
end
If-ElseIF-Else command:
function sgn = mysign (x)
if x>0
sgn=1
elseif x == 0
sgn=0
else
sgn=-1
end

Structured Programming cont.


For loop command:
function fout=factor(n)
x=1
for i=1:n
x=x*i
end
fout=x

fout = n!

Structured Programming cont.


while command:
function fout=whiledemo2
x=8
while x>0
if x<5
break
end
x=x-3;
disp(x)
end

Structured Programming cont.


Arrange your if, for, while in a nesting fashion:

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;

All the M files must be in the


(same) working directory

Anda mungkin juga menyukai