# Fitting with roots of transcendental equation

I'm new to this community and my first question probably reflect my lack of confidence with Mathematica.

My problem is the following: I find the roots of a transcendental equation dependent on a parameter, let's say $x = \cos(\alpha x)$, And compare these roots with a set of assigned values via an error function (e.g. sum of the absolute value of the difference between each root and the corresponding fixed value). I want to minimize the outcome of the error function varying the parameter $\alpha$, in order to get a value for $\alpha$ such that the transcendental roots lie the closest possible to the assigned values.

I'm basically trying to fit data (the assigned values) with the roots of an equation depending on a parameter. I think that it should work in this way:

1. Find roots of transcendental equation
2. Error function outputs an error given by confronting the calculated roots with data
3. Some minimization, varying the parameter until you get to a value for it that minimize the error function.

The minimization function should than be able to repeat the process from point number 1 at every change of the parameter, but I couldn't find any simple function doing that. I'm not sure if you can do that with the fitting function I've already seen, like FindFit, and I'm not sure I'm looking at the problem the right way. Is there an easy way to do that with Mathematica's function?

• Thanks for a well-formatted first question, and welcome. – Mr.Wizard Jan 15 '15 at 1:25

## 1 Answer

I believe I understand your question.

Here is a table of values of {alpha, f[alpha]}

mybasedata = Table[{α, x /. NSolve[{x == Cos[α x] && 0 < x < 2 π/α}, x][]}, {α, 0.1, 2, .1}] // Quiet


Here is the functional fit:

myfit[α_] = Fit[mybasedata, {1, α, α^2, α^3}, α]


(* 1.02718 - 0.2184 α - 0.112612 α^2 + 0.0475883 α^3 *)

Here is the full training data:

mytrainingdata = Table[{α, myfit[α]}, {α, .1, 2, .1}];


Here is some fake test data:

mytestdata = Table[{α, RandomReal[]}, {α, .1, 2, .1}];


Here is an error score: the variance of the values of mytrainingdata and mytestdata:

myError = Variance[mytrainingdata[[All, 2]] - mytestdata[[All, 2]]]


(* 0.108539 *)

This overall code can be streamlined significantly (since we needn't use the values of alpha in several steps) but let us know if this is what you're seeking.