The problem is the term "a solution". There are *many* different solutions to the question "how much of which salts/acids can be used to hit the target water profile and pH".
Of course. We are trying to match perhaps 7 ion's concentrations and have perhaps 10 salts, acids and bases we can use. The problem is underdetermined and there are, therefore, an infinite number of solutions.
Perhaps one could artificially limit the output by having the user select which salts/acids they have,
I think it's pretty clear that a user would want to limit the salts the program can choose from to the ones he has in hand. We also find from use of the Solver that broadening the spectrum of input salts/acids/bases given to the program may improve the quality of the match. As an example if the guy wants extra sulfate but doesn't want any more calcium or magnesium he can allow the use of sulfuric acid but that will, of course, change the pH so that the program will also call for some sodium hdroxide, potassium hydroxide or sodium carbonate to compensate for that. He could, of course, just give the program sodium hydroxide or carbonate to work with and get the same result but we want to give him the option to make those choices.
but even then there would be multiple solutions.
If he restricts the number of salts to less than or equal to the number of ions he is trying to match then there is one solution only. But in the general case he should be allowed to select how many he wants to use and there will then be cases where there will be an infinite number of solutions.
The output should be a list of solutions that the user can select from.
Why? I'm missing something here. All he really cares about is getting a solution that gives him the best match to his desired profile given the salts/acids/bases he has available.
Newton-Raphson, Moore-Penrose, Built-in Excel Solver don't live up to this expectation.
The Solver certainly lets him choose which ions he wants to match and which salts/acids/bases he wants to use with any restrictions he wants to place on the amount, for example, of any given salt. It also allows him to weight the ions under consideration in any relative importance he chooses, to chose and arithmetic or geometric error criterion etc. What more could he want? One of the challenges will be making all those choices available to the user more conveniently than the Solver does. My focus right now is on the algorithm with the intention of making a function or subroutine available that will accept an desired water properties list, a list of weights to be assigneded to each of those, a list of chosen additions, a geometric/algebraic flag and a desired pH and return a list of addition quantities. It them becomes the task of a real programmer to interface that to a pretty, functional spreadsheet presentation.
Newto-Raphson and Moore Penrose are parts of the same algorithm - not distinct algorithms. In Newton's method we find how far off we are from a target, find the slope of the target function with respect to the parameter being optimized over, divide the error by the slope and correct the value of the parameter being optimized over by that ratio. That works fine where the error function is Q and the parameter is being estimated is the single parameter pH. Here we have multiple parameters being optimized and thus the slope isn't a simple derivative - it is a matrix of derivatives. You don't divide by a matrix, you multiply by its inverse and only square matrices have inverses. So we have to use a 'pseudo inverse'. MPP (Moore-Penrose Psuedo Inverse) is one of a class having the property that when the problem is overdetermined and the equations inconsistent it picks the best solution. When the problem is undetermined as it is here, it picks the best solution with the minimum norm. Thus it does exactly what we need here.
I was able to use MPP to solve the malt coefficient parameter estimation problem because while the relevant matrix A doesn't have an inverse (it is tall - i.e. not square) A_transpose*A does. Thus Excels built in matrix inversion routine can be used to find MPP. In the ion matching problem A_transpose*A, while square, doesn't have an inverse and so MPP requires SVD to compute. Excel doesn't have SVD capability. It is available, free, as a plug in but I don't realistically expect that even the more advanced brewers would be willing to accept my add in and the Real Statistics add in. The alternative would be for me to haul out Numerical Recipes and port the FORTRAN to VBA. Doable but it's a fairly lengthy piece of code.