l20. more on 2d arrays
DESCRIPTION
L20. More on 2D Arrays. Operations Subscripting Functions* & 2D Arrays. *Will see two new things. Boolean-valued functions Functions that have a function as a parameter. Two Applications. A commercial setting that involves cost arrays, inventory arrays, and purchase orders. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/1.jpg)
L20. More on 2D Arrays
OperationsSubscriptingFunctions* & 2D Arrays
*Will see two new things. Boolean-valued functionsFunctions that have a function as a parameter.
![Page 2: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/2.jpg)
Two Applications
A commercial setting that involves cost arrays, inventory arrays, and purchase orders.
A setting that requires the visualizationof a function of two variables f(x,y) viacontour plotting.
![Page 3: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/3.jpg)
A Cost/Inventory Setting
A company has 3 factories that make 5 different products.
The cost of making a product varies from
factory to factory.
The inventory varies from factory to factory.
![Page 4: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/4.jpg)
ProblemsA customer submits a purchase order
thatis to be filled by a single factory.
1. How much would it cost a factoryto fill the order?
2. Does a factory have enough inventory to fill the order?3. Among the factories that can fill the order, who can do it most cheaply?
![Page 5: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/5.jpg)
Cost Array
C:
10 36 22 15
12 35 20 12
13 37 21 16
66
62
59
The value of C(i,j) is what it costs factory i to make product j.
![Page 6: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/6.jpg)
Inventory Array
38 5 99 34
82 19 83 12Inv:
51 29 21 56
42
42
87
The value of Inv(i,j) is the inventory in factory i of product j.
![Page 7: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/7.jpg)
Purchase Order
The value of PO(j) is the numberproduct j’s that the customer wants
1 0 12 529PO:
![Page 8: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/8.jpg)
How Much Does it Cost for Each Factory to Process
a Purchase order?
![Page 9: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/9.jpg)
1 0 12 529PO:
C:
10 36 22 15
12 35 20 12
13 37 21 16
66
62
59
1*10 + 0*36 + 12*22 + 29* 15 + 5*62
For factory 1:
![Page 10: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/10.jpg)
1 0 12 529PO:
C:
10 36 22 15
12 35 20 12
13 37 21 16
66
62
59
s = 0;for j=1:5 s = s + C(1,j) * PO(j)end
For factory 1:
j = 1
![Page 11: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/11.jpg)
1 0 12 529PO:
C:
10 36 22 15
12 35 20 12
13 37 21 16
66
62
59
s = 0;for j=1:5 s = s + C(1,j) * PO(j)end
For factory 1:
j = 2
![Page 12: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/12.jpg)
1 0 12 529PO:
C:
10 36 22 15
12 35 20 12
13 37 21 16
66
62
59
s = 0;for j=1:5 s = s + C(1,j) * PO(j)end
For factory 1:
j = 3
![Page 13: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/13.jpg)
1 0 12 529PO:
C:
10 36 22 15
12 35 20 12
13 37 21 16
66
62
59
s = 0;for j=1:5 s = s + C(1,j) * PO(j)end
For factory 1:
j = 4
![Page 14: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/14.jpg)
1 0 12 529PO:
C:
10 36 22 15
12 35 20 12
13 37 21 16
66
62
59
s = 0;for j=1:5 s = s + C(1,j) * PO(j)end
For factory 1:
j = 5
![Page 15: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/15.jpg)
1 0 12 529PO:
C:
10 36 22 15
12 35 20 12
13 37 21 16
66
62
59
s = 0;for j=1:5 s = s + C(2,j)*PO(j)end
For factory 2:
![Page 16: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/16.jpg)
1 0 12 529PO:
C:
10 36 22 15
12 35 20 12
13 37 21 16
66
62
59
s = 0;for j=1:5 s = s + C(i,j)*PO(j)end
For factory i:
![Page 17: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/17.jpg)
function TheBill = iCost(i,C,PO)
% The cost when factory i fills
% the purchase order
nProd = length(PO)
TheBill = 0;
for j=1:nProd
TheBill = TheBill + C(i,j)*PO(j);
end
Encapsulate…
![Page 18: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/18.jpg)
1 0 12 529PO:
C:
10 36 22 15
12 35 20 12
13 37 21 16
66
62
59
Finding the Cheapest
1019
930
1040
As computed by iCost
![Page 19: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/19.jpg)
1 0 12 529PO:
C:
10 36 22 15
12 35 20 12
13 37 21 16
66
62
59
Finding Cheapest: Initialization
1019
930
1040
iBest: minBill: 0 inf
Canwe dobetter?
![Page 20: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/20.jpg)
A Note on “inf’’
A special value that can be regarded as+ infinity.
x = 10/0 assigns inf to xy = 1+x assigns inf to yz = 1/x assigns zero to zw < inf is always true if w is
numeric
![Page 21: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/21.jpg)
1 0 12 529PO:
C:
10 36 22 15
12 35 20 12
13 37 21 16
66
62
59
Improvement at i = 1
1019
930
1040
iBest: minBill: 1 1019
![Page 22: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/22.jpg)
1 0 12 529PO:
C:
10 36 22 15
12 35 20 12
13 37 21 16
66
62
59
Improvement at i = 2
1019
930
1040
iBest: minBill: 2 930
![Page 23: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/23.jpg)
1 0 12 529PO:
C:
10 36 22 15
12 35 20 12
13 37 21 16
66
62
59
No Improvement at i = 3
1019
930
1040
iBest: minBill: 2 930
![Page 24: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/24.jpg)
iBest = 0; minBill = inf; for i=1:nFact
iBill = iCost(i,C,PO);
if iBill < minBill
% Found an Improvement
iBest = i; minBill = iBill;
end
end
Finding the Cheapest
![Page 25: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/25.jpg)
Inventory Considerations
What if a factory lacks the inventory tofill the purchase order?
Such a factory should be excluded fromthe find-the-cheapest computation.
![Page 26: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/26.jpg)
Who Can Fill the Order?
38 5 99 34
82 19 83 12Inv:
51 29 21 56
42
42
87
1 0 12 529PO:
Yes
No
Yes
Because 12 < 29
![Page 27: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/27.jpg)
Wanted: A True/False Function
iCanDoInv
PO
i
B
B is “true” if factory i can fill the order.
B is “false” if factory i cannot fill the order.
![Page 28: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/28.jpg)
Boolean Operationsin Matlab
SO FAR we have indicated thatexpressions like
a <= x && x <= b
abs(y) > 10
are either TRUE or FALSE.
![Page 29: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/29.jpg)
The 0-1 Secret
In reality, expressions like
a <= x && x <= b
abs(y) > 10
render the value “1” if they are TRUE and
“0” if they are FALSE.
![Page 30: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/30.jpg)
Example
>> x = 8; y = 7;
>> B = x<yB = 0
>> B = x>yB = 1
![Page 31: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/31.jpg)
A Boolean-Valued Function function B = Overlap(a,b,c,d)
% B is true if intervals [a,b]
% and [c,d] intersect.
% Otherwise B is false.
% Assume a<b and c<d.
abToLeft = b < c;
abToRight = d < a;
B = ~(abToLeft || abToRight);
![Page 32: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/32.jpg)
Using Overlap
S = 0;for k=1:100 a = rand; b = a + rand; c = rand; d = c + rand; if Overlap(a,b,c,d) s = s+1; endendprobOverlap = s/100
![Page 33: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/33.jpg)
Back to Inventory Problem
38 5 99 34
82 19 83 12Inv:
51 29 21 56
42
42
87
1 0 12 529PO:
![Page 34: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/34.jpg)
Initialization
38 5 99 34
82 19 83 12Inv:
51 29 21 56
42
42
87
1 0 12 529PO:
B: 1
![Page 35: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/35.jpg)
Still True…
38 5 99 34
82 19 83 12Inv:
51 29 21 56
42
42
87
1 0 12 529PO:
B: 1
B = B && ( Inv(2,1) >= PO(1) )
![Page 36: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/36.jpg)
Still True…
38 5 99 34
82 19 83 12Inv:
51 29 21 56
42
42
87
1 0 12 529PO:
B: 1
B = B && ( Inv(2,2) >= PO(2) )
![Page 37: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/37.jpg)
Still True…
38 5 99 34
82 19 83 12Inv:
51 29 21 56
42
42
87
1 0 12 529PO:
B: 1
B = B && ( Inv(2,3) >= PO(3) )
![Page 38: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/38.jpg)
No Longer True…
38 5 99 34
82 19 83 12Inv:
51 29 21 56
42
42
87
1 0 12 529PO:
B: 0
B = B && ( Inv(2,4) >= PO(4) )
![Page 39: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/39.jpg)
function B = iCanDo(i,Inv,PO)% B is true if factory i can fill% the purchase order. Otherwise, false nProd = length(PO); j = 1; B = 1; while j<=nProd && B B = B && ( Inv(i,j) >= PO(j) ); j = j+1; end
Encapsulate…
![Page 40: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/40.jpg)
iBest = 0; minBill = inf; for i=1:nFact
iBill = iCost(i,C,PO);
if iBill < minBill
% Found an Improvement
iBest = i; minBill = iBill;
end
end
Back To Finding the Cheapest
Don’t bother with this unless sufficient inventory.
![Page 41: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/41.jpg)
iBest = 0; minBill = inf; for i=1:nFact if iCanDo(i,Inv,PO) iBill = iCost(i,C,PO); if iBill < minBill% Found an Improvement iBest = i; minBill = iBill; end end end
Back To Finding the Cheapest
![Page 42: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/42.jpg)
function [iBest,minBill] = … Cheapest(C,Inv,PO)
[nFact,nProd] = size(C);
iBest = 0; minBill = inf;
for i=1:nFact
if iCanDo(i,Inv,PO)
iBill = iCost(i,C,PO);
if iBill < minBill
iBest = i; minBill = iBill;
end
end
end
![Page 43: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/43.jpg)
1 0 12 529PO:
C:
10 36 22 15
12 35 20 12
13 37 21 16
66
62
59
Finding the Cheapest
1019
930
1040
As computed by iCost
Yes
No
Yes
As computed by iCanDo
![Page 44: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/44.jpg)
New Problem
Visualizing a function of the form z = f(x,y).
Think of z as an elevation which dependson the coordinates x and y of thelocation.
![Page 45: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/45.jpg)
Sample Elevation Function
function z = Elev(x,y) r1 = (x-1)^2 + 3*(y-1.5)^2;r2 = 2*(x+2)^2 + (y-.5)^2;r3 = (x-.5)^2 + 7*y^2;z = 100*exp(-.5*r1) + … 90*exp(-.3*r2) + … 80*exp(-.4*r3);
Three Hills at (1,1.5),(-2,.5), (.5,0)
![Page 46: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/46.jpg)
Its Contour Plot
![Page 47: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/47.jpg)
Making a Contour Plotx = linspace(-5,4,200);y = linspace(-2.5,6.5,200);A = zeros(200,200);for i=1:200 for j=1:200 A(i,j) = Elev(x(j),y(i)); endend
contour(x,y,A,15)
Set up a matrix of function evals
![Page 48: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/48.jpg)
General Set-Up
function A = SetUp(f,xVals,yVals)Nx = length(xVals);Ny = length(yVals);A = zeros(Ny,Nx);for i=1:Ny for j=1:Nx A(i,j) = f(xVals(j),yVals(i)); endend
![Page 49: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/49.jpg)
Calling SetUp
x = linspace(-5,4,200);
y = linspace(-2.5,6.5,200);
F = SetUp(@Elev,x,y);
Not just ‘Elev’The @ is required for function parameters.
![Page 50: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/50.jpg)
Generating a Cross Section
Enter endpoints via ginputSample Elev(x,y) along the line segment
![Page 51: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/51.jpg)
Mouse Input Via ginput
To draw a line segment connecting(a(1),b(1)) and (a(2),b(2)):
[a,b] = ginput(2);
plot(a,b)
[a,b] = ginput(n) puts the mouseclick coords in length-n arrays a and b.
![Page 52: L20. More on 2D Arrays](https://reader035.vdocument.in/reader035/viewer/2022062803/56814749550346895db486ce/html5/thumbnails/52.jpg)
n = 100;t = linspace(0,1,n);x = linspace(a(1),a(2),n);y = linspace(b(1),b(2),n);for i=1:n% At "time" t(i) we are at (x(i),y(i)).% Compute elevation at time t(i). f(i) = Elev(x(i),y(i));endfigureplot(t,f)