Solving a multivariable linear set of equations I have a question. I believe this question is related to system of equations. This is a finance-related question, but the topics are pretty basic.
First, I pose the question as a problem with two questions. Second, I provide the finance context of the question. Third, I combine the finance context and the problem - such that you (i.e. the reader) understand what the problem is actually trying to solve in real life.
Thanks in advance for any help!
 The Problem 
There are $n$ unknowns, $\{x_1, x_2, ... , x_n\}$.
For each $x_i$ in $\{x_1, x_2, ... , x_n\}$, we know that $0 \le x_i \le 1$
There are 3 equations:

*

*$t=r_1 x_1 + r_2x_2 + ... + r_nx_n$

*$1 = x_1 + x_2 + ... + x_n$

*$l = \frac{x_1+x_2 + ...+ x_k}{x_1+x_2+...+x_k+...+x_n}$ (which can be simplified to $l=x_1+x_2 + ...+ x_k$, using the information in the second bullet point)

Assume that you are given a value for $t$, a value for $l$, and a value for each $r_i$ in $\{r_1,...,r_n\}$ are given.
1. Find whether there exists a set $\{x_1, x_2, ... , x_n\}$ that satisfies the equations given the parameters $t$ and $l$. If so, what is the set $\{x_1, x_2, ... , x_n\}$?
2. If there doesn't exist a set that satisfies the equations given the parameters: what is a change $\Delta = \Delta l = \Delta t$ (i.e. a number that is added / subtracted to / from both $t$ and $l$) such that there will exist a solution $\{x_1, x_2, ... , x_n\}$ . Then, provide the solution $\{x_1, x_2, ... , x_n\}$
 Example of Context of the Problem 
Say we have a portfolio. A portfolio can have different weightings (25% bonds, 50% apple stock, 25% tesla stock). The number $n$ is the number of different types of holdings; that is, in this case $n=3$ because we have bonds, apple stock, and tesla stock (i.e. three things or three 'types of holdings').
Also, in this example we have $x_1=25\%$ (for bonds), $x_2=50\%$ (for apple stock), and $x_3=25\%$ (for tesla stock).
One more piece of information to add to the example: assume bonds have a guaranteed performance of 1.5% (i.e. $r_1 = 1.5\%$),apple stock has a guaranteed performance of 3% (i.e. $r_2 = 3\%$), and tesla stock has a guaranteed performance of 9% (i.e. $r_3=9\%$).
Last piece of information: assume bonds are liquid whereas apple and tesla stock are illiquid.
 Translating the context into the problem 
Now, I will explain how the information above translates into "The Problem":
The first bullet point is the same as someone saying "hey, I want my portfolio to have 5% guaranteed return (i.e. the person lets $t=5\%$)"
The second bullet point is the same as someone saying "the sum of the shares of each type of holding cannot be greater than one i.e. you cannot have 55% bonds and 65% tesla stock!"
The third bullet point is the same as someone saying "I want my portfolio to be 40% liquid (i.e. $l=40%$; that is, we sum the shares of each liquid holding to get a percentage that tells us what percentage of the portfolio is liquid. " So in our example that would be bonds only since we said that only bonds are liquid, whereas tesla and apple stock are illiquid). The $k$ means that we are only choosing liquid types of holdings. That is, in the second bullet point I'm implying that $\{x_1, x_2, ..., x_k\}$ are all liquid.
Question 1 tries to answer "given $t$ and $l$ that a person gave us, is it possible to have a specific 'mix' of bonds, apple, and tesla stock  such that $t$ and $l$ hold". If so, what is this 'mix' (i.e. like 5%, 55%, 40% respectively)?
Question 2 tries to answer "given that there doesn't exist a mix, let's be fair to both the $l$ and $t$ constraints and try to change them both by the same amount such that we get to a middle ground. That is, we will have to sacrifice some liquidity and some performance such that a mix can exist. That is why we say that we want the change to be equal for both $l$ and $t$ (i.e. that is why we said in The Problem that $\Delta = \Delta t = \Delta l$).
 A: This problem seems like a good application for numerical solutions, specifically gradient descent. The idea is that you have a "cost" function for potential solutions which break the rules, and a solution is found by minimizing the cost function. A particular advantage of this approach is that different rules can have different weights.
In your problem, there are essentially three levels of rule: Laws of numbers, priority goals, and secondary goals. The laws of numbers include the constraints that the percentages must sum to 1, and that no individual percentage can be less than 0 or greater than 1. The priority goal is that the deviations $(l-l_0)$ and $(t-t_0)$ must be equal (or otherwise related). The secondary goals are that $l$ and $t$ must equal their given targets, $l_0$ and $t_0$.
Now we work on individual cost functions for each of the rules. Each function takes in a potential solution $\vec x$ and outputs a non-negative number describing how badly the rule is broken. For the rule $\sum_{i=1}^nx_i=1$, we simply take the square of the distance by which this equation is wrong.
$$c_1=\left(1-\sum_{i=1}^nx_i\right)^2$$
For the rule that no element may be negative or greater than unity, we have to use a piecewise function to measure deviation.
$$c_{2,i}=\left\{\begin{array}\\
x_i^2 & x_i<0\\
0 & 0\leq x_i\leq 1\\
(x_i-1)^2 & x_i>1
\end{array}\right.$$
To combine all single-element rules:
$$c_2=\sum_{i=1}^n\left\{\begin{array}\\
x_i^2 & x_i<0\\
0 & 0\leq x_i\leq 1\\
(x_i-1)^2 & x_i>1
\end{array}\right.$$
For the priority goal, we do need to calculate $l$ and $t$ for every potential solution. They are then used in a similar equality cost function to the first rule.
$$c_3=\left((l-l_0)-(t-t_0)\right)^2$$
For the secondary goals, we use the same square-of-error equality cost function.
$$c_4=(l-l_0)^2$$
$$c_5=(t-t_0)^2$$
Finally we put them all together into a single cost function. Since not all rules are the same priority, we can apply different gains to reflect this. For example, perhaps the first two rules should be 100 times as important as the third, which is 10 times as important as the fourth and fifth. Then set $g_1=1000,g_2=10,g_3=1$, and add them all together:
$$c=g_1(c_1+c_2)+g_2c_3+g_3(c_4+c_5)$$
Now any software optimization tools should be able to solve this easily, but gradient descent is probably the best method if you're not using those. The function $c$ is piecewise quadratic and easy to differentiate. Once you have the gradient $\nabla c=\left\langle\frac{dc}{dx_1},\cdots,\frac{dc}{dx_n}\right\rangle$, simply take the unit vector in the opposite direction, and step by a small amount $h$.
$$(\vec x)_{i+1} = (\vec x)_i - h\frac{\nabla c}{\Vert\nabla c\Vert}$$
Now you just need a starting guess (perhaps $x_1=\dots=x_n=\frac{1}{n}$) and a step size (perhaps $0.1$, decreasing with more iterations), and a computer or some patience and you'll have your solution.
