Search code examples
gnuplotlimitintercept

Limit the intercept for fit with gnuplot


I have a simple linear fitting function as:

f(x) = m*x + b

however, the fitting end up having a negative intercept (b<0) which does not have any meaning in my use case. I need to restrict the intercept to be only positive numbers. The code that I found

fit [b=0:] f(x) "name_of_the_file" u 1:2 via m, b

only works for x variable restriction, but not for any other parameter. How can I limit the intercept to positive numbers?


Solution

  • You can try to modify your fitting function and replace b by c**2:

    f(x) = m*x + c**2
    fit f(x) "name_of_the_file" u 1:2 via m, c
    

    Then you have a nonnegative b = c**2.