I’m programming web application which has some tricky calculations and I would need some help with mathematical calculations.

What I'm trying to calculate:

I have number of items. Lets call items a1, a2, ..., an. Each item consists of three components (let's call them x, y and z). Components have a ratio which is percentage. Sum of that ratio is always 1 (or 100%).

There is individual amount of each item and each item has unique tolerance/range +- % (min - max).

Then I have target ratio (x, y and z). I need to figure out what amount of each item must be to find combination which forms target ratio. Items amount must be between their minimum and maximum values. Combined items forms overall x, y and z values with should match target x, y and z

Here is an example how I started to solve the problem:

Item 1:
Amount: 100
Tolerance: +-30%
x = 0.25, y = 0.50 and z = 0.25
Minimum amount: 100% - 30% = 70% -> 100 x 0.7 = 70
Maximum amount: 100% + 30% = 130% -> 100 x 1.3 = 130

(Here 70% and 130% are coming from tolerances and after that min and max amounts can be calculated

Item 2:
Amount: 250
Tolerance: +-25%
x = 0.15, y = 0.30 and z = 0.55
Minimum amount: 100% - 25% = 75% -> 250 x 0.75 = 187.5
Maximum amount: 100% + 25% = 125% -> 250 x 1.25 = 390.6

Item 3:
Amount: 180
Tolerance: +-40%
x = 0.40, y = 0.40 and z = 0.20
Minimum amount: 100% - 40% = 60% -> 180 x 0.6 = 108
Maximum amount: 100% + 40% = 140% -> 180 x 1.4= 252

I have a target ratio. In this example let the target ratio be:

x = 0.25, y = 0.40 and z = 0.35

I need to find what the amount of each items should be in order to form the target ratio and the items must be in their tolerance ranges (between min and max).

Because of each items has 3 components, without min/max amount the combination can be easily calculated (multiplier for each item) with 3 equations below by using linear equation. Item 1 = a1, item 2 = a2 and item 3 = a3:

0.25 a1 + 0.50 a2 + 0.25 a3 = 0.25 <- target x = 0.25
0.15 a1 + 0.30 a2 + 0.55 a3 = 0.40 <- target y = 0.40
0.40 a1 + 0.40 a2 + 0.20 a3 = 0.35 <- target z = 0.35

After calculating linear equation I get ratio of items: a1 = 0.375, a2 = 0.375 and a3 = 0.25 meaning that a1 = 37.5%, a2 = 37.5% and a3 = 25.0% of overall amount of items. Now x, y and z match with target ratio. At this point I haven't used the minimum or maximum values.

Below is the link to online calculator which I now used to calculate factors using linear equation for test purposes: (Also note that I used a, b and c for item/variable names instead of a1, a2 and a3 in online calculator). Link to online calculator

In this example there are 3 variables/items and 3 equations. Two problems: min/max is not included and there can be for example 10 items/variables in equation. Then the linear equation doesn't work anymore because there are much more items/variables than equations. I'm not sure how but these min and max amount's must be added to the same equation (and is it possible to form a matrix):

a1 >= 70 <- item 1 minimum amount
a1 <= 130 <- item 1 maximum amount
a2 >= 187.5 <- item 2 minimum amount
a2 <= 390.6 <- item 2 maximum amount
a3 >= 108 <- item 3 minimum amount
a3 <= 252 <- item 3 maximum amount

And this is my problem. I have no idea how to combine these together (inequalities and linear equations or am I going totally wrong direction?). I need to be able to get values for a1, a2, a3, …, an. And I need at least one suitable solution (combination) if there is any solution also when there are multiple or infinite amount of solutions.

If someone can help I would be very grateful! Thanks for your time!

  • 1
    $\begingroup$ Welcome to CS.SE! I'm having a hard time understanding the question. This question is very long; can you find any way to make it more concise? Usually if you can state the problem in general mathematical terms it can be much more concise than if you give us detailed examples. Coding questions are off-topic here so the programming language is not relevant. $\endgroup$
    – D.W.
    Commented Sep 28, 2020 at 3:34
  • $\begingroup$ Hi, thanks. I made this question shorter and removed programming part to make this question easier to understand. I wrote the example because I'm not sure how to form this question so that it would be understandable to viewers. $\endgroup$ Commented Sep 28, 2020 at 6:38
  • $\begingroup$ In linear programming, every inequality defines a half-space (delimited by a plane), and the admissible domain is the convex polyhedron defined by the intersection of these half-spaces. Then if you want to maximize some linear objective function (i.e. the equi-objective surfaces are parallel planes), the solution occurs at one of the vertices of the polyhedron. The most common optimization method constructs vertices one after the other by following the edges, making sure to increase the objective each time. $\endgroup$
    – user16034
    Commented Jun 20, 2022 at 14:42

1 Answer 1


I find it hard to tell from your question, but your problem sounds like it might be an instance of linear programming, in which case there existing solvers you can use.

  • $\begingroup$ I reformed my question to make it more readable. I'm now studying the linear programming but I have hard time to understand where should I start. $\endgroup$ Commented Sep 28, 2020 at 7:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.