# Find the values of 3 variables that best fit 6 equations [duplicate]

I have 6 equations

0.344027==0.5 (a+b)-0.5 (a-b) Cos[2 (-1.3439-0.0174533 c)]
0.679511==0.5 (a+b)-0.5 (a-b) Cos[2 (0.20944 -0.0174533 c)]
0.436543==0.5 (a+b)-0.5 (a-b) Cos[2 (-0.733038-0.0174533 c)]
0.324024==0.5 (a+b)-0.5 (a-b) Cos[2 (1.18682 -0.0174533 c)]
0.304968==0.5 (a+b)-0.5 (a-b) Cos[2 (-1.5708-0.0174533 c)]
0.676049==0.5 (a+b)-0.5 (a-b) Cos[2 (-0.174533-0.0174533 c)]


I could pick 3 equations from the list and got exact answers. However, I want my variables (a,b,c) to have values such that the right hand sides of all 6 equations approach the left hand side as much as possible. I'm not really sure what I can use in this case to resolve this.

Edit: I used Kuba's method to solve my problem. However, using FindFit (per belisarius' helpful suggestion) also gave me the same result. This works because all 6 equations have a pair of x and y that could be fitted through multivariable FindFit-- though if I were to be given random equations with a,b,c, NMinimize might be the only method to find them.

FindFit example in MMA's documentation

Using FindFit to my example

Original equations:

0.344027==0.5 (a+b)-0.5 (a-b) Cos[2 (-1.3439-0.0174533 c)]
0.679511==0.5 (a+b)-0.5 (a-b) Cos[2 (0.20944 -0.0174533 c)]
0.436543==0.5 (a+b)-0.5 (a-b) Cos[2 (-0.733038-0.0174533 c)]
0.324024==0.5 (a+b)-0.5 (a-b) Cos[2 (1.18682 -0.0174533 c)]
0.304968==0.5 (a+b)-0.5 (a-b) Cos[2 (-1.5708-0.0174533 c)]
0.676049==0.5 (a+b)-0.5 (a-b) Cos[2 (-0.174533-0.0174533 c)]


yData = {0.344027, 0.679511, 0.436543, 0.324024, 0.304968, 0.676049};
xData = {-1.3439, 0.20944, -0.733038, 1.18682, -1.5708, -0.174533};
data = {xData, yData, Table[0, {6}]} // Transpose;
model = y - (0.5 (a + b) - 0.5 (a - b) Cos[2 (x - 0.0174533 c)]);
fit = FindFit[data, model, {a, b, c}, {x, y}]
model /. fit /. {y -> yData, x -> xData}
Show[Plot3D[model /. fit, {x, -2, 2}, {y, 0, 1},
AxesLabel -> Automatic,
MeshShading -> {{None, None}, {None, None}}],
ListPointPlot3D[data, PlotStyle -> Directive[PointSize[Medium], Red]],
ListPlot3D[data, VertexColors -> Hue]]


ClearAll[a, b, c]
NMinimize[
#.# &[Subtract @@@ {
0.344027 == 0.5 (a + b) - 0.5 (a - b) Cos[2 (-1.3439 - 0.0174533 c)],
0.679511 == 0.5 (a + b) - 0.5 (a - b) Cos[2 (0.20944 - 0.0174533 c)],
0.436543 == 0.5 (a + b) - 0.5 (a - b) Cos[2 (-0.733038 - 0.0174533 c)],
0.324024 == 0.5 (a + b) - 0.5 (a - b) Cos[2 (1.18682 - 0.0174533 c)],
0.304968 == 0.5 (a + b) - 0.5 (a - b) Cos[2 (-1.5708 - 0.0174533 c)],
0.676049 == 0.5 (a + b) - 0.5 (a - b) Cos[2 (-0.174533 - 0.0174533 c)]
}], {a, b, c}]

{0.0059057, {a -> 0.291325, b -> 0.677473, c -> 3.2186}}

• Thank you so much for your help Kuba. I have a question: it seems like you were trying to find the dot product of the list of the (subtraction) equations and minimize it to find the best roots. Is this a standard math practice, and if so does it have a name or is there anywhere I could learn more about it? I'm just a college student so my math skills are not very strong. – seismatica Jun 21 '14 at 8:06
• @seismatica Hi, I'm glad I could help. Before dot product there is subtraction so at the end we are minimizing sum of squared differences, like in standard lsq method but via black box NMinimize :) – Kuba Jun 21 '14 at 14:23

You may use FindFit

l = {0.344027 == 0.5 (a + b) - 0.5 (a - b) Cos[2 (-1.3439 - 0.0174533 c)],
0.679511 == 0.5 (a + b) - 0.5 (a - b) Cos[2 (0.20944 - 0.0174533 c)],
0.436543 == 0.5 (a + b) - 0.5 (a - b) Cos[2 (-0.733038 - 0.0174533 c)],
0.324024 == 0.5 (a + b) - 0.5 (a - b) Cos[2 (1.18682 - 0.0174533 c)],
0.304968 == 0.5 (a + b) - 0.5 (a - b) Cos[2 (-1.5708 - 0.0174533 c)],
0.676049 == 0.5 (a + b) - 0.5 (a - b) Cos[2 (-0.174533 - 0.0174533 c)]};
l1 = (List@@@l) /. {x1_,x2_} :>Flatten@{Cases[x2,Cos[2 x__] :> (List @@ x /.c :> 1), 2], x1};
ff = FindFit[l1, .5 u - .5 v Cos[2 (x + y c)], {u, v, c}, {x, y}]
Solve[{a + b == u, a - b == v} /. ff, {a, b}]

(*
{u -> 0.968798, v -> -0.386148, c -> 3.2186}
{{a -> 0.291325, b -> 0.677473}}
*)

• Thank you so much for your help belisarius. I've edited my question with the FindFit method you suggested in a way that feels more intuitive to me. – seismatica Jun 21 '14 at 12:40