Menu

Fitting a linear function doesn't work

Help
Jannis B.
2016-04-18
2016-04-25
  • Jannis B.

    Jannis B. - 2016-04-18

    Hey!

    I'm trying to fit a function in the form of f(x)=a*x+b to these measurements:

    5.19e+14 7.91e-14
    5.49e+14 8.56e-14
    6.88e+14 1.67e-13
    7.40e+14 1.96e-13
    8.21e+14 2.40e-13
    

    However, after fit f(x) measured.csv using 1:2 via a,b, gnuplot returns

    a6              = -1.46398e-15
    b6              = 1   
    

    ..., which is a line thats rising far, far to fast.

    Any idea what's wrong?

    Thanks in advance!

     
  • Hans-Bernhard Broeker

    What's wrong is that your fitting problem is in bad shape, and reading the documentation about the fit command, particularly "help fit tips", would have told you what's bad about it.

     
  • Bastian Märkisch

    # inline data
    $data << EOD
    5.19e+14 7.91e-14
    5.49e+14 8.56e-14
    6.88e+14 1.67e-13
    7.40e+14 1.96e-13
    8.21e+14 2.40e-13
    EOD
    
    f(x) = a*x + b
    
    # best option: do not use a non-linear fitting tool for a linear fit,
    # use 'stats' instead:
    stats $data using 1:2
    a = STATS_slope
    b = STATS_intercept
    
    # or, if you insist on 'fit':
    # Do set start-up values! The order of magnitude is important.
    # Otherwise gnuplot will assume start-up values of 1, which is way off.
    a = 1e-28
    b = -1e-13
    fit f(x) $data using 1:2 via a,b
    
    set key bottom
    set format y "%h"
    plot \
      $data t "measured", \
      STATS_slope*x + STATS_intercept t "stats result", \
      f(x) t "fit result"
    
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.