# Optimize/fit data for 2 variables described by three functions

I have three equations that describe how parameters change: $$N_{1}(T,f)=f \ e^{-T (\sigma^{1}_c +\sigma^{1}_f)}$$ $$N_{2}(T,f)=-\frac{f \sigma^{1}_c \ (e^{-T (\sigma^{1}_c +\sigma^{1}_f)}-e^{-T \sigma^{2}_c})}{\sigma^{1}_c-\sigma^{2}_c+\sigma^{1}_f}$$ $$N_{3}(T,f)=(1-f) \ e^{-T \sigma^{3}_c }$$

N1[T_,f_]:=f*Exp[-T*(97 + 584)]
N2[T_,f_]:=-((f*97*(Exp[-T*(97 + 584)] - Exp[-T*(24)]))/(97 - 24 + 584))
N3[T_,f_]:=(1 - f)*Exp[-T*(2.683)]


All of the $$\sigma$$ values are known. I have the relative values for each $$N_i$$ and would like to solve for $$T$$ and $$f$$.

There is no exact solution, so I'd like to optimize/approximate a solution that finds some $$T$$ and $$f$$ which would result in values close to what I have, something like a least squares fit but for three functions rather than data points.

The $$N_i$$ are concentrations of components, so at some point $$F$$ and $$T$$, I know that component $$N_1$$ is 74% of the solution, $$N_2$$ is 13% and $$N_3$$ is 11%. There are others $$N_i$$s that make the percentages add to unity, but I don't have equations to describe those, so they're not relevant.

It seems like a pretty simple problem, but I haven't been able to figure out a way to fit the data to multiple functions. Any help would be greatly appreciated!

Edit: Yes, for any of the two expression, there is a numerical solution (no free parameters). But I need to fit the data to all three equations and solve for an f and T that approximate the real data as well as possible. This would be done with a least squares fit for the parameters f and T, but I'm not sure how to account for equations, rather than data in Mathmatica's least squares function.

• This sounds to me more l like a mathematics(Mathematics)/statistics question than a Mathematica question. Once you know the method by which you want to fit, then you can start to figure out how to implement it in Mathematica, and then come here if you run into any trouble in that implementation. Feb 20, 2019 at 18:50
• I'd like to fit by least squares, sorry if that wasn't clear from the post. Feb 20, 2019 at 19:01
• If you could write out what specific objective function you want to optimize around, NMinimize would likely be the most straightforward way to solve this problem. Just reading the problem however, I'm not entirely sure what you're going for here. I'd guess it'd be minimizing the sum of square differences between the known $N_i$ values and the corresponding derived values from the $T$ and $f$ expressions. Feb 20, 2019 at 20:29
• Yes, that is the least squares method, I'd like to optimize by minimizing the sum of squared residuals between the real data points and the calculated points (for some f, T). I was hoping to that someone would know a method to input my equations or some variation of them into Mathmaticas LeastSquares regression analysis. Feb 20, 2019 at 20:44
• @Mecury-197 I'm a bit confused about what you want. What are you trying to fit? You have the $N_i$ values and you have expressions for them and you have the $\sigma$ values so what is there left to fit? Like there are no free parameters in this problem as you've written it out. Feb 20, 2019 at 21:22

I think I understand what you want, now. You just want a solution that more or less gives you the right ratios for a single point, right? I.e. you don't need a fit. You just need to minimize the residuals from what you expect for a single point.

So we can try this:

systemEquations =
With[
{n1 = N1[T, f], n2 = N2[T, f], n3 = N3[T, f],
conc = {.74, .13, .11}},
# - (#2/Total[conc]) (n1 + n2 + n3) &,
{
{n1, n2, n3},
conc
}
]
];

sol = NMinimize[
Norm@Simplify[systemEquations],
{ T, f} ∈ Rectangle[{0., 0.}, {.003, 1.0}]
]

{9.26348*10^-8, {T -> 0.00119307, f -> 0.937932}}


Then substituting back in:

systemEquations /. sol[[2]]

{7.55255*10^-8, -4.13023*10^-8, -3.42232*10^-8}

{N1[T, f], N2[T, f], N3[T, f]} /. sol[[2]]

{0.416212, 0.0731183, 0.0618693}


Seem reasonable?

• Yes, that looks correct! It seems I came at this from the wrong angle, I was really hoping when I posted this that it would be a trivial problem and I had overlooked something in wolfram documentation about reducing residuals about a point in functions. So thank you for trying to understand my poor initial explanation and helping me! Feb 20, 2019 at 22:14
• @Mecury-197 I'm glad I could be useful. Keep an eye on this page, though, maybe someone else will come up with an even better answer. Feb 20, 2019 at 22:15
• Alternative NMinimize[(.74 - N1 [T, f])^2 + (.13 - N2 [T, f])^2 + (.11 - N3 [T, f])^2, {T, f}] gives a much better result {N1->0.725246, N2->0.0254578, N3->0.0952181} ! Feb 21, 2019 at 8:04
• @UlrichNeumann do we know that, e.g., N1[T, f] should be .74? I thought it was 74% of the total... Feb 21, 2019 at 8:06
• @ b3m2a1 In your question I read "N1 is 74%,...". Perhaps you have to add a constraint N1[T, f] + N2[T, f] + N3[T, f] == 0.98 Feb 21, 2019 at 8:21

Sorry I'm a little bit late. Here I give a direct solution which I proposed in my comments:

opt = NMinimize[{(.74 - N1[T, f])^2 + (.13 - N2[T, f])^2 + (.11 -
N3[T, f])^2, N1[T, f] + N2[T, f] + N3[T, f] == 0.98}, {T, f}]
(*{0.024073, {T -> 0.000041979, f -> 0.826641}}*)


The optimum fits N1&N3 quite well, N2 only poor:

{N1[T, f], N2[T, f], N3[T, f]} /. opt[[2]]
{0.803344, 0.00331672, 0.173339}


The approbiate weighting of the funcional might chnage the optimum slightly.

If

sumN = N1[T, f] + N2[T, f] + N3[T, f] + \[CapitalDelta]N


is the total number of molecules (unknown \[CapitalDelta]N), the percentage part of each sort of molecules is given by Ni/sumN.

Now let's minimize for T,f **and** \[CapitalDelta]N (!)

J = (.74 - N1[T, f]/sumN)^2 + (.13 - N2[T, f]/sumN)^2 + (.11 -N3[T, f]/sumN)^2
opt = NMinimize[{J , \[CapitalDelta]N > 0 }, {T, f, \[CapitalDelta]N}]
(*{5.79983*10^-12, {T -> 0.00119306,f -> 0.937932, \[CapitalDelta]N ->0.0112501}}*)

{N1[T, f], N2[T, f], N3[T, f]} /. opt[[2]]
(*{0.416216, 0.0731177, 0.0618699}*)

• The fundamental problem is that only the relative proportions are known. It is not known that N1[T, f] should be .74, etc. It is just known that N1[T, f]/Total[Table[Ni[T, f], {Ni, {N1, N2, ...}]] should be .74. Feb 22, 2019 at 9:05
• ... Total[Ni]=N1+N2+N3=?=.98`? Feb 22, 2019 at 9:15
• Not quite. The sum of them is unknown as far as I can tell. The percentages they comprise are known, though. Feb 22, 2019 at 9:21
• Sorry , I'm out. What about your comment "...updated version where I take N1 + N2 + N3 == .98 ..." Feb 22, 2019 at 9:54
• Laziness in writing on my part. I took them to be .98 of the total. Feb 22, 2019 at 9:55