





























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






























^ Previous Lecture:
^ 2-d array—matrix ^ Today’s Lecture:
^ More examples on matrices ^ Contour plot (Read 7.2, 7.3) ^ Announcements:
^ Project 3 due tonight at 11pm ^ Prelim 2 on Thurs Mar 12. Email [email protected] if you have a prelim conflict ^ Review session next week
March 5, 2009
Lecture 14
Initialize vectors/matrices if dimensions are known…instead of “building” the array one component ata time
% Build y on the flyx=linspace(a,b,n);for k=1:n
y(k)=myF(x); end
% Initialize yx=linspace(a,b,n);y=zeros(1,n);for k=1:n
y(k)=myF(x); end Much faster for large n!
March 5, 2009
Lecture 14
function A = RandomLinks(n)% A is n-by-n matrix of 1s and 0s% representing n webpagesA = zeros(n,n);for i=1:n
for j=1:n r = rand(1);if i~=j && r<= 1/(1 + abs(i-j));
A(i,j) = 1; end end end
March 5, 2009
Lecture 14 0 1 1 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 01 0 0 0 1 0 0 0 1 1 1 0 0 0 0 0 0 1 0 00 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 00 1 1 1 1 1 0 0 0 1 0 1 1 0 0 0 0 0 0 00 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 1 10 1 0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 0 0 00 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 0 0 10 0 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 0 0 00 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 10 0 0 1 0 0 0 0 1 1 0 1 0 1 1 0 0 0 0 00 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 00 0 0 0 0 1 0 1 0 0 0 0 1 0 0 1 0 0 0 10 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 1 00 1 0 0 0 0 0 0 1 0 0 0 0 1 0 1 0 1 1 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 10 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0
Random web^ N
March 5, 2009
Lecture 14
Line black as it leaves page j, red when it
arrives at page i
Represent the web pages graphically…
March 5, 2009
Lecture 14
ShowRandomLinks.m
March 5, 2009
Lecture 14
ProblemsA customer submits a purchase order thatis to be filled by a single factory.1. How much would it cost a factory to fill the order?2. Does a factory have enough inventory to fillthe order?3. Among the factories that can fill the order,who can do it most cheaply?
March 5, 2009
Lecture 14
Cost Array
10
36
22
15
12
35
20
12
13
37
21
16
62 66 59
The value of
C(i,j)
is what it costs
factory i to make product j.
March 5, 2009
Lecture 14
Purchase Order
The value of
PO(j)
is the number of
product j’s that the customer wants
March 5, 2009
Lecture 14
10
36
22
15
12
35
20
12
13
37
21
16
62 66 59
Cost forfactory 1:
March 5, 2009
Lecture 14
10
36
22
15
12
35
20
12
13
37
21
16
62 66 59
Cost forfactory 2:
s^
%Sum
of
cost
for
j=1:5s =
s
C(2,j)*PO(j)
end
March 5, 2009
Lecture 14
10
36
22
15
12
35
20
12
13
37
21
16
62 66 59
Cost forfactory i:
s^
%Sum
of
cost
for
j=1:5s =
s
C(i,j)*PO(j)
end
March 5, 2009
Lecture 14
10
36
22
15
12
35
20
12
13
37
21
16
62 66 59
Finding the Cheapest
1019930 1040 As computedby
iCost
March 5, 2009
Lecture 14
iBest = 0;
minBill = inf;
for i=1:nFact
iBill = iCost(i,C,PO);if iBill < minBill
% Found an ImprovementiBest = i; minBill = iBill; end end Finding the Cheapest