Professional Documents
Culture Documents
MATLAB Chapter 4: 4.1 Program Design and Development
MATLAB Chapter 4: 4.1 Program Design and Development
MATLAB Chapter 4: 4.1 Program Design and Development
MATLAB Chapter 4
Programming with Matlab
1
Spring 2015
2
Spring 2015
z = u =
1 0 0 1 0 0
u =
1 0 0
>> x(u)
ans =
6
6
6
3
Spring 2015
4
Spring 2015
0 1 1 ans = ans =
>> x|y 0 1 1 3 9
0 1 0
5
Spring 2015
ans =
0
The exclusive OR function
>> xor(1,0)
xor(A,B) returns zeros
where A and B are either ans =
ans =
0
11
>>
6
Spring 2015
>> x=[1,1,0,0]';
>> y=[1,0,1,0]';
>> Truth_Table=[x,y,~x,x|y,x&y,xor(x,y)]
Truth_Table =
1 1 0 1 1 0
1 0 0 1 0 1
0 1 1 1 0 1
0 0 1 0 0 0
>>
13
7
Spring 2015
Functions 1 2
0 5
>> [u,v,w]=find(A)
u =
1
1
2
v =
1
[u,v,w]=find(A) 2
2
u=nonzero row indices
v=nonzero column indices
w =
w=nonzero values
1
2
15
5
etc…
16
8
Spring 2015
Example 4.3-1
The projectile will strike the ground when h(t)=0, which gives the time
to hit t hit 2(v 0 / g)sinA .
Suppose A = 40°, v0= 20 m/s, and g = 9.8 m/s2. Find the times when
the height is no less than 6 m (i.e. h(t)6 m) and the speed is
simultaneously no greater than 16 m/s (i.e. v(t)16 m/s).
17
Example 4.3-1
18
9
Spring 2015
20
10
Spring 2015
Do you want to continue? Y/N [Y]: y Do you want to continue? Y/N [Y]: n
response = response =
Y N
>> >>
21
Example 4.4-1
Write a script file to compute the sum of the first 15 terms in the series 5k2 - 2k,
k=1, 2, 3, . . . , 15.
Solution:
22
11
Spring 2015
Example 4.4-1
Write a script file to compute the sum of the first 15 terms in the series 5k2 - 2k,
k=1, 2, 3, . . . , 15.
Solution:
23
Example 4.4-2
24
12
Spring 2015
Example 4.4-2
25
Example
Typing specmat(5) produces
the following matrix:
26
13
Spring 2015
Example 4.4-4
Write a script file to determine how many terms are required for the sum of the series
5k2 - 2k, k = 1, 2, 3, . . . to exceed 10,000. What is the sum for this many terms?
Solution:
27
Example 4.4-4
Write a script file to determine how many terms are required for the sum of the series
5k2 - 2k, k = 1, 2, 3, . . . to exceed 10,000. What is the sum for this many terms?
Solution:
28
14
Spring 2015
Example 4.4-5
Determine how long it will take to accumulate at least $10,000 in a bank account if you
deposit $500 initially and $500 at the end of each year, if the account pays 5 percent
annual interest.
Solution:
29
Example 4.4-5
Determine how long it will take to accumulate at least $10,000 in a bank account if you
deposit $500 initially and $500 at the end of each year, if the account pays 5 percent
annual interest.
Solution:
30
15
Spring 2015
4.5 Loops
32
20
16
Spring 2015
4.5 Loops
The breakstatement
It is permissible to use an if statement to “jump” out of the loop
before the loop variable reaches its terminating value. The break
command terminates the loop but does not stop the entire program.
33
4.5 Loops
The continue statement
34
17
Spring 2015
4.5 Loops
The continue statement
35
4.5 Loops
Using an Array as a loop Index
clear 1
A=[1,2;4,5]; 4
for v=A
v
end v =
2
5
36 >>
18