Message Boards Message Boards

2 Replies
3 Total Likes
View groups...
Share this post:

Finding a specific curve fitting a set of points

Posted 10 years ago
I haven’t used Mathematica earlier, so I apologise for my blatant lack of knowledge.

I’ve been trying to find a solution to a problem (duh!). I need to find a function that comes very close to these points: {{0.1,30},{0.5,1},{0.95,0.1}}, but which also resembles in its shape the left-and-a-little-bit-of-the-right slice of a cotangent plot (in that it first drops in a steep way, almost levels itself and then starts dropping again; but not in a symmetrical way – the first drop is steeper than the second one). The domain of the function would be (0,1].

First I tried to tackle this by solving a system of equations. I decided that the (0,0.8*Pi] slice of the cotangent plot should be a good start, and so I went with this general idea for the form of the function:
C* (cot[A * 0.8 * Pi * x] + B)

where C would be the vertical scale, B  for shifting vertically and A would widen/tighten the horizontal shape of the function.

Thus, I go to this:

Then NSolve and FindRoot, as the attempt was fruitless.

So then I tried NonlinearModelFit with the general form above. But even with heavily upped MaxIterations, the results weren’t even remotely useful (in fact, the more iterations it took, the more wrong the numbers were).

So… I’m kinda stuck. Any ideas for further action?
2 Replies
data = {{0.1, 30}, {0.5, 1}, {0.95, 0.1}};
model = a Cot[c + b x];
fit = FindFit[data, model, {a, b, c}, x];
sol = Function[{x}, Evaluate[model /. fit]];

Plot[sol[x], {x, 0.1, 1}, Epilog -> {PointSize[Large], Red, Map[Point, data]}, PlotRange -> All]

POSTED BY: Sam Carrettie
Posted 10 years ago
In some cases, the initial parameter values for the non-linear solver can be a strong factor.
I used a genetic algorithm to "guess" initial parameter estimates and here is my final fitting result.
Try these coefficients:

  A =  2.5767189410200002E-04
    B = -2.0812198398869805E+03
    C =  2.2355243129572131E-03
Send me an email if you have any questions on the details

James Phillips
POSTED BY: James Phillips
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract