(* Discounts and Over-Declared Shipments *) Clear[demand, setup, holding, price, totalCost]; demand = 7000; setup = 20; holding = 0.36; weight = 3.8; price = PWSFreeze[PWS[ {{{1, IB[750]}, 6.21 &}, {{750, IB[1125]}, 5.94 &}, {{1125, IB[2250]}, 5.88 &}, {{2250, Infinity}, 5.76 &}}]]; breakpoints = {weight, 216.5, 500, 1000, 2000, 5000, 10000, 20000, Infinity}/ weight; rates = {30.30, 24.92, 18.87, 15.39, 10.67, 8.87, 6.58}; rates = (rates weight/100) /. r_?NumberQ -> (r # &); rates = Prepend[rates, 65.60 &]; x = Transpose[{Rest[Reverse[breakpoints]], Reverse[rates]}]; tariff = {{Take[breakpoints, -2], Last[rates]}}; cost[{qty_, rate_}] := rate[qty]; Clear[q]; While[ z = cost[x[[1]]];(* cost to beat *) q0 = IB[x[[1, 1]]]; x = Select[Rest[x], cost[#] < z &]; x != {}, w = First[q /. Solve[x[[1, 2]][q] == z, q]]; tariff = Join[{{{x[[1, 1]], IB[w]}, x[[1, 2]]}, {{w, q0}, Evaluate[z] &}}, tariff]]; If[breakpoints[[1]] < q0, PrependTo[tariff, {{breakpoints[[1]], q0}, Evaluate[z] &}]]; tariff = PWSFreeze[PWS[tariff]]; Plot[tariff[q], {q, 1, 7000}, AxesLabel -> {"Quantity", "Shipping Cost"}]; purchase = demand setup/# & + demand price + (holding #/2 &) price; totalCost = purchase + (demand/# &) tariff; Plot[totalCost[q], {q, 1, 6000}, PlotRange -> {44000, 56000}]; tC = totalCost /. Infinity -> 99999; FindMinimum[tC[q], {q}]