# Solving numerically system with Mathematica

I'm trying to solve the following system with Mathematica:

Poblacion = {
{1900, 1.65},
{1910, 1.75},
{1920, 1.86},
{1930, 2.07},
{1940, 2.30},
{1950, 2.53},
{1960, 3.02},
{1970, 3.70},
{1980, 4.45},
{1990, 5.30},
{2000, 7.29}
}

P[t_] := 1 / (-q (K t + C))^(1/q)

NSolve[{P[1900] == 1.65, P[1910] == 1.75, P[1920] == 1.86}, {K, q, C}]


But I can't get it working. Could you please tell me what I'm doing wrong?

-
For this type of system FindRoot is more appropriate; however, why only using 3 points rather than making a fit using all the data ? –  b.gatessucks May 29 '13 at 9:35
@b.gatessucks, I'd go further and suggest FindFit[], even on the edited problem where only three points are taken. –  Guess who it is. May 29 '13 at 9:36
How can i make a fit using all data? :S –  José D. May 29 '13 at 9:49
Do you have any idea about what the values of the parameters should be? –  Oleksandr R. May 29 '13 at 14:52
1) C is a protected symbol; change to CC or something. 2) "NSolve deals primarily with linear and polynomial equations" which you don't have. 3) The initial points are important; these yield a solution with FindRoot: {{K, 0.001}, {q, 1.}, {CC, -2.1}}; they are important for FindFit, too. 4) With FindFit put constraints on parameters so that the base of your exponential expression does not become negative. –  Michael E2 May 29 '13 at 18:44

Maybe you should consider a different model. If you look at your data and try to make a guess the most obvious choice is an exponential; a double exponential looks even better :

ListLinePlot[{Log[#[[1]]], Log@Log[#[[2]]]} & /@ Poblacion]


If this looks fine then the fitting is straighforward :

nlm = NonlinearModelFit[{Log[#[[1]]], Log@Log[#[[2]]]} & /@ Poblacion,a + b t, {a, b}, t]
(* -206.05 + 27.1955 t *)


and in terms of the original data :

Show[ListPlot[Poblacion, PlotStyle -> Red],
Plot[Exp[Exp[nlm[Log[t]]]], {t, Poblacion[[1, 1]], Poblacion[[-1, 1]]}]]


-

FWIW I get a resonable fit by adding a constant to your expression, and using Abs to ensure a real value returned..

P[t_] := 1 /(Abs[ -q  (K   t + CC)])^(1/q) + p0
ff = FindFit[
Poblacion, {P[t]}, {{q, -.28}, {CC, -100}, {K, .05}, {p0, 2}}, t]


(* {q -> -0.0614064, CC -> -55.0774, K -> 0.0365885, p0 -> 1.56496} *)

Show[Plot[P[t] /. ff, {t, 1900, 2000}, PlotRange -> {0, 8}],
ListPlot[Poblacion, PlotRange -> {0, 8}]]


This only works by getting darn close with the initial guesses.

-
Slightly different starting values work equally well to give a different solution: I tried {{q, 0.0308}, {CC, -98.4}, {K, 0.0338}, {p0, Pi/2}}, albeit still with convergence warnings. (These can be suppressed using Method -> "Gradient" or NMinimize, but it appears that this just hides the symptoms rather than solving the problem.) Clearly this fit is quite ill-conditioned and it does not appear possible to exactly reproduce the given points, so it's debatable to what extent this can be considered a well-posed problem. –  Oleksandr R. May 30 '13 at 12:43
exactly. As often the case, if you set about trying to manually fit your model to the data by choosing sensable parameters you will discover that the model equation is not a good form. –  george2079 May 30 '13 at 18:27
How can i fit the model without the initial point? –  José D. May 31 '13 at 13:24
@Trollkemada the model does not fit properly even with initial points given--note that george2079 had to change it to reproduce the data somewhat reasonably, and the parameters are still not well determined. I recommend that you revise your model or otherwise reconsider what you are doing (and then update your question), because not much progress is going to be made if people have to guess what you really want. –  Oleksandr R. May 31 '13 at 15:57
I have to find the best fit of that model to that data. I cannot change the model. –  José D. May 31 '13 at 19:51