Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

stuck on a mathematica assignment and could really use some help.

A) Fit a function with the shape log(Y) = a0 + a1log(U) to the datapoints (log(Ui), log(Yi) with an least square fitting. Show this in a plot ...

B) Use your answer in A) to find a function Y = kU^n that fit to the datapoints (Ui, Yi)

The U datapoints is: (600000, 200000, 60000, 10000, 2500)

The Y datapoints is: (250, 60, 25, 12, 5)

I've done this:

data2 = {{Log[2500], Log[5]}, {Log[10000], Log[12]}, {Log[60000], 
   Log[25]}, {Log[200000], Log[60]}, {Log[600000], Log[250]}}

fitExample = Fit[data2, {1, x}, x]

Am I right to be using the Fit function this way? How can I use this answer in the B question?

share|improve this question
Using Solve is an easy way: Solve[Log[y]==(*your answer from A*)/.x->Log[u],y] – Z-Y.L May 7 '14 at 2:23
@Z-Y.L Why do not you write this as an answer? – Alexei Boulbitch May 7 '14 at 7:25
@AlexeiBoulbitch I usually write an answer in the comment if it can be expressed as one sentence. Anyway, thanks for your suggestion! – Z-Y.L May 9 '14 at 2:11

You can also use LinearModelFit:

lm = LinearModelFit[data2, {1, x}, x]

You can 'normalize' output:


This yields:

-3.72168 + 0.663665 x

You can look at underlying properties:



{"AdjustedRSquared", "AIC", "AICc", "ANOVATable", \ "ANOVATableDegreesOfFreedom", "ANOVATableEntries", \ "ANOVATableFStatistics", "ANOVATableMeanSquares", \ "ANOVATablePValues", "ANOVATableSumsOfSquares", "BasisFunctions", \ "BetaDifferences", "BestFit", "BestFitParameters", "BIC", \ "CatcherMatrix", "CoefficientOfVariation", "CookDistances", \ "CorrelationMatrix", "CovarianceMatrix", "CovarianceRatios", "Data", \ "DesignMatrix", "DurbinWatsonD", "EigenstructureTable", \ "EigenstructureTableEigenvalues", "EigenstructureTableEntries", \ "EigenstructureTableIndexes", "EigenstructureTablePartitions", \ "EstimatedVariance", "FitDifferences", "FitResiduals", "Function", \ "FVarianceRatios", "HatDiagonal", "MeanPredictionBands", \ "MeanPredictionConfidenceIntervals", \ "MeanPredictionConfidenceIntervalTable", \ "MeanPredictionConfidenceIntervalTableEntries", \ "MeanPredictionErrors", "ParameterConfidenceIntervals", \ "ParameterConfidenceIntervalTable", \ "ParameterConfidenceIntervalTableEntries", \ "ParameterConfidenceRegion", "ParameterErrors", "ParameterPValues", \ "ParameterTable", "ParameterTableEntries", "ParameterTStatistics", \ "PartialSumOfSquares", "PredictedResponse", "Properties", "Response", \ "RSquared", "SequentialSumOfSquares", "SingleDeletionVariances", \ "SinglePredictionBands", "SinglePredictionConfidenceIntervals", \ "SinglePredictionConfidenceIntervalTable", \ "SinglePredictionConfidenceIntervalTableEntries", \ "SinglePredictionErrors", "StandardizedResiduals", \ "StudentizedResiduals", "VarianceInflationFactors"}

and use for example:

  lm["ANOVATable"]}, Frame -> All]

enter image description here

or 95% confidence bands for mean prediction:

p[x_] := lm["MeanPredictionBands", ConfidenceLevel -> 0.95]
Show[ListPlot[data2], Plot[Evaluate@{lm[x], p[x]}, {x, 1, 14}]]

enter image description here

Finally, for 'fun' you can confirm least squares result with some linear algebra:

mat = {ConstantArray[1, 5], data2[[All, 1]]};
N@Inverse[mat.Transpose@mat].mat.data2[[All, 2]]

which yields: {-3.72168, 0.663665} per found fit.

If you wish to back transform and ultimately show power relation:

tf[u_] := Exp[(Normal@lm /. x -> Log[u])]
lin = Show[ListPlot[Exp[data2]], 
  Plot[Evaluate@tf[x], {x, 2500, 600000}]]
lglg = Show[ListLogLogPlot[Exp[data2]], 
  LogLogPlot[Evaluate@tf[x], {x, 2500, 600000}]]

enter image description here

enter image description here

and the power relation:

0.0241932 x^0.663665

share|improve this answer

You may do like you started to, and then follow like in the comment of Z-Y.L. You might also go ahead directly using the FindFit function as follows. This is your initial data (without logarithms in it):

data={{2500, 5}, {10000, 12}, {60000, 25}, {200000, 60}, {600000, 250}};

here one fits it:

 ff = FindFit[data, a*x^b + c, {a, b, c}, x]

(*  {a -> 2.37535*10^-6, b -> 1.3853, c -> 9.75617}  *)

Done. Let us have a look at the data and the fit:

  LogLogPlot[a*x^b + c /. ff, {x, 2500, 600000}, PlotStyle -> Red]

It should look as follows:

enter image description here

Note that without the parameter c the coincidence will be still worth. Have success.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.