# Solving system of equations with R

Suppose I have the following function :

$f(x,y,z,a)= \cos(ax) + 12y^2 - 9az$

and I want to solve the following syste of equations.

$f(x,y,z,1)= 10$,

$f(x,y,z,5)= 7$, and

$f(x,y,z,-3)= 17$.

These are equivalent to

$\cos(x) + 12 y^2 - 9 z(1) = 10$, $\cos(5x) + 12y^2 - 9 z(5) = 7$, and $\cos(-3x) + 12y^2 +27z = 17$.

How do I solve these equations for $x$, $y$, and $z$? I would like to solve these equations, if possible, using R or any other computer tools.

-

## migrated from stats.stackexchange.comOct 10 '13 at 23:31

This question came from our site for people interested in statistics, machine learning, data analysis, data mining, and data visualization.

While this is an absolutely fascinating question, I don't think it's on topic here. It might perhaps be on topic on say math.SE or perhaps stackoverflow depending on how it's framed. If you want to try it at one of those places, don't repost; flag it and ask for it to be moved. –  Glen_b Oct 10 '13 at 22:52
This question appears to be off-topic because it is not about statistics within the scope as defined by the help center. –  gung Oct 10 '13 at 23:26

This is a partial answer; I can't give actual code, since I'm not familiar with R.

The standard way to solve this type of problem is to reformulate it as a nonlinear root-finding (or nonlinear optimization) problem and then use existing software or packages.

1. First, write your equations as follows: \begin{align} \cos x + 12 y^2 - 9 z - 10 &= 0 \\ \cos 5 x + 12 y^2 - 45 z - 7 &= 0 \\ \cos 3 x + 12 y^2 + 27 z - 17 &= 0 \end{align} (in the last equation we have used the even symmetry of $\cos$: $\cos(-x) = \cos x$).
2. Next, observe that this the above equations are equivalent to the following problem: Find the root of the nonlinear function $\textbf{F} : \mathbb{R}^3 \to \mathbb{R}^3$: $$\textbf{F}(\textbf{x}) = \begin{bmatrix} \cos x + 12 y^2 - 9 z - 10 = 0 \\ \cos 5 x + 12 y^2 - 45 z - 7 = 0 \\ \cos 3 x + 12 y^2 + 27 z - 17 = 0 \end{bmatrix},$$ where $$\textbf{x} = \begin{bmatrix} x \\ y \\ z \end{bmatrix};$$ that is, solve the equation $$\textbf{F}(\textbf{x}) = \textbf{0} \tag{1}$$ for $\textbf{x} \in \mathbb{R}^3$.

Unfortunately it is not immediately clear if this problem has a solution. Moreover, if it does, it is not clear precisely how many solutions it has. Nonetheless, we can proceed using a couple of packages for R that are available on CRAN: rootSolve and ucminf.

I would suggest first using rootSolve: it is a nonlinear root-finding package, and will try to solve Equation (1) assuming that a solution exists. It will require an initial guess; ideally it would be close to what you think is a solution, but to get started, feel free to try $x = 0, y = 0, z = 0$ or $x = 1, y = 1, z = 1$ or even $x, y, z =$ random numbers. When your script is running and returning output, you should test that your solution is correct: put the output back into the function $\textbf{F}(\textbf{x})$ and test that it is $\approx \textbf{0}$.

If it fails this test, then you might need to use a different initial guess, or just cut your losses and use ucminf: this package is a nonlinear optimization package. It does not assume that your function has an exact solution; instead, it tries to find $\textbf{x} \in \mathbb{R}^3$ such that $\left\| \textbf{F}(\textbf{x}) \right\|$ is minimized. If it happens that the problem has a solution, but for some reason rootSolve was not able to find it, then this nonlinear optimization problem is equivalent; otherwise it will simply be an optimal "solution".

Again, let me stress that nonlinear problems may not even have a solution, or may have several solutions, even though the output of the functions will indicate only one solution. Moreover, the output should always checked. Solving nonlinear systems of equations is not easy. Hopefully someone with more experience in R can comment or make a post on how to actually implement this.

-

$cos(x) + 12 y^2 - 9 az = 10$

$cos(5x) + 12 y^2 - 45 az = 7$

$cos(-3x) + 12 y^2 + 27 az = 17 = cos(3x) + …$

$12y^2$ is the easiest to eliminate, so this becomes

$12 y^2 = 10 + 9az - cos(x) = 7 + 45az - cos(5x) = 17 - 27az - cos(3x)$

Since each of these three functions now equal each other, we can subtract them from each other to get 3 pairs of $0$

$10 + 9az - cos(x) - 7 - 45az + cos(5x) = 3 - 36az - cos(x) + cos(5x) = 0$

$17 - 27az - cos(3x) - 10 - 9az + cos(x) = 7 - 36az + cos(x) - cos(3x) = 0$

$17 - 27az - cos(3x) - 7 - 45az + cos(5x) = 10 - 72az - cos(3x) + cos(5x) = 0$

We can now eliminate az by saying

$36az = 3 - cos(x) + cos(5x) = 7 + cos(x) - cos(3x)$

and then

$7 + cos(x) - cos(3x) - 3 + cos(x) - cos(5x) = 4 + 2cos(x) - cos(3x) = 0$

The cleanest identity for $cos(3x)$ is $4cos^3(x) - 3cos(x)$, which then gives us

$4 + 5cos(x) - 4cos^3(x) = 0$

You can then use cubic roots to calculate the possible values of cosx, which can then be plugged into the original formulae using the identities $cos(3x) = 4cos^3(x) - 3cos(x)$ and $cos(5x) = 16cos^5(x) - 20cos^3(x) + 5cos(x)$. After that, "y" and "z" can be calculated by subtracting the pairs of equations created by these new constraints.

-
–  Shaun Oct 20 '14 at 15:14
Thanks! I'll take a look. –  Simpson17866 Oct 20 '14 at 15:27
You're welcome. Note that $\cos$ looks better than $cos$ using $\cos$ :) –  Shaun Oct 20 '14 at 19:31