Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I defined and evaluated a function

Max2[t_?NumericQ] := Log[Abs[-1 + First[Maximize[sol1[[2]][x, t], 0 <= x <= L, x]]]];

Now, I expect this to be close to a straight line as a function of $t$, so I would like to get the best slope from a fit.

Looking at the reference website, I don't understand how to linearly fit a function.

share|improve this question
2  
You have provided incomplete information. How come on earth we will know what sol1[[2]][x, t] or the value of x are? Without this info no one can address the problem here. –  PlatoManiac Nov 17 '13 at 15:11
    
One way to find a linear fit to a function is by taking the derivative. –  bill s Nov 17 '13 at 15:19

1 Answer 1

up vote 3 down vote accepted

Let's take a vaguely straight function like you say yours is:

Plot[Log[t], {t, 50, 100}]

logplot

You could fit a line to it by sampling a bunch of points:

data = Table[{t, Log[t]}, {t, 50, 100}];

And fitting a line through those:

fit = Fit[data, {1, t}, t]
(*  3.2732 + 0.0136576 t  *)

Or you could minimise the integral of the square difference (i.e. least squares) directly yourself:

fit2 = a + b t /. Last@NMinimize[Integrate[(a + b t - Log[t])^2, {t, 50, 100}], {a, b}]
(*  3.27497 + 0.0136447 t  *)

They both give similar results:

Plot[{Log[t], fit, fit2}, {t, 50, 100}]

logfit

share|improve this answer
    
Thanks a lot! How can I extract the slope from the output fit, without making the derivative? –  usumdelphini Nov 17 '13 at 23:41
    
You could use a rule fit /. m_ t + c_ :> m or pick out the right part fit[[2, 1]] –  wxffles Nov 18 '13 at 1:47

Your Answer

 
discard

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.