Updated code attached...

```
function [f] = Inventory_ma2rafiq(C_Cost)
Demand = [1,3,2,4];
%C_Cost = [
% 20 16 31/2 15 27/2
% 16 15 14 12 21/2
% 12 10 7 13/2 6
% 7 6 5 4 0];
Setup_cost = 3;
variable_cost = 1;
holding_cost = 0.5;
Prod_Cap = 5;
WareHouse_Cap = 4;
Period_Begining = 1;
Period_End = 4;
Inv_Min = 0;
Inv_Max = 4;
%Prod_Q_Min = 0;
%Prod_Q_Max = 5;
n=size(C_Cost,1);
Min_Cost=zeros(1,n); %added matrix to keep argmin(Min_Cost)
Opt_Prod_Quantity=zeros(1,n); %added matrix to keep argmin(Min_Cost)
for Period = Period_End:-1:Period_Begining; % Period Loop for 4 - 1
for Inv_Level = Inv_Min:Inv_Max; % Inventory Level Loop from 0 - 4
for Prod_Quantity = max(0,Demand(Period)-Inv_Level):min(Prod_Cap,WareHouse_Cap-Inv_Level+Demand(Period)); % Loop only with Current Month Demand met constraint and Ending Inventory cannot exceed Capacity Constraint
if Period == 4 % Cost for Period 4
if Prod_Quantity == 0
cost = 0; % No Production = 0 Cost
else
cost = Setup_cost + Prod_Quantity*variable_cost + holding_cost*(Inv_Level + Prod_Quantity - Demand(Period)); % Holding Cost + ProdCost
end
end
if Period ~= 4 % Cost for Periods other than 4
if Prod_Quantity == 0
cost = Setup_cost + Prod_Quantity*variable_cost; % Calculate only cost from Cost Matrix
else
cost = Setup_cost + Prod_Quantity*variable_cost + holding_cost*(Inv_Level + Prod_Quantity - Demand(Period)) + Setup_cost + Prod_Quantity*variable_cost; % Complete Cost Formula
end
end
%Inv_Level + Prod_Quantity - demand(Period) <=4; % Current Month Demand Constraint
%Inv_Level + Prod_Quantity - demand(Period) >=0; % Ending Inventory cannot exceed Capacity Constraint
if C_Cost(Period,Inv_Level+1) == cost;% Check to see if this is the minimum cost
X = (cost+0.5*(Inv_Level + Prod_Quantity - Demand(Period))); % Store the Minimum cost
Min_Cost(1,Inv_Level+1) = (X); % If we are at the minimum cost then store min_cost
Opt_Prod_Quantity(1,Inv_Level+1) = (Prod_Quantity); % If we are at the minimum cost then store min_cost associated Prod_Quantity
end
end
end
end
Min_Cost % Output Min_Cost matrix
Opt_Prod_Quantity % Output Production Quantity matrix
end
```