Curve fitting is built into gnuplot as the fit command: define a model function with unknown parameters, give the parameters starting values, and run fit f(x) "growth.dat" using 1:2 via a, b to adjust them by least squares. The script below fits an exponential to growth data.
The file growth.dat holds x in column 1 and a measured y in column 2 that grows roughly exponentially:
0 1.1
1 1.7
2 2.6
3 4.2
4 6.6
5 10.4
f(x) = a * exp(b * x) + c
a = 1.0
b = 0.1
c = 0.0
fit f(x) "growth.dat" using 1:2 via a, b, c
set title "Exponential Fit"
set xlabel "x"
set ylabel "y"
plot "growth.dat" using 1:2 with points pointtype 7 pointsize 1.3 title "Data", \
f(x) with lines linewidth 2 title "Fit"
print sprintf("Fitted: a = %.4f, b = %.4f, c = %.4f", a, b, c)
What each part does
The first line defines the model: f(x) = a * exp(b * x) + c. The names a, b and c are parameters — variables whose values the fit will choose. The next three lines are not decoration: fit uses an iterative algorithm (Levenberg–Marquardt), so every parameter needs a starting value reasonably close to the truth, or the iteration can stall or wander off. For exponential growth, a small positive b such as 0.1 is a sane start.
The fit command itself reads like a plot command: the function, the file, the using columns for x and y, and finally via a, b, c listing exactly which variables may vary. When it finishes, gnuplot prints a summary — the final sum of squared residuals and, for each parameter, the fitted value plus an asymptotic standard error (a rough ± uncertainty). The parameters keep their fitted values afterwards, so the following plot draws f(x) as the fitted curve next to the raw points, and the print sprintf(...) line echoes the numbers for use in a caption or report. A full session log is also written to fit.log in the working directory.
Variants and going further
If the data file has an uncertainty column, weight the fit with it by adding the error column to the using spec: fit f(x) "growth.dat" using 1:2:3 via a, b, c treats column 3 as the y error (sigma), so precise points pull the curve harder. Fixing a parameter is just as easy: leave it out of the via list and it stays at its assigned value. For straight-line work the model is simply f(x) = a * x + b — the same workflow, and a good first fit to try.
Common mistakes
- Fit stalls, diverges, or ends with huge errors: the starting values are too far from the solution, or two parameters trade off against each other (a classic with exponentials plus offsets). Plot the model with your starting values first — it should already pass near the data — then fit again.
- “Undefined value during function evaluation”: the trial parameters pushed the model outside its domain mid-fit, for example
logof a negative number or overflow inexp. Constrain the start values, simplify the model, or fit a transformed form (such as fitting a line to log(y)) first to get good starting values.
Related tutorials: Error Bars in Gnuplot, Plot CSV Data in Gnuplot. See also the full Gnuplot Tutorials index.
Try it online: Fitting works in the online terminal with a pasted datablock, and the printed parameter summary appears in its console output. Paste the script into the Gnuplot Online terminal and run it in your browser — no install needed. To run gnuplot locally instead, see the installation guide and the download page.
Go deeper: For a systematic walk through gnuplot from first plots to publication-ready figures, see Gnuplot in Action by Philipp K. Janert — Janert is a gnuplot developer, and the book is the most complete tutorial reference for the program. Affiliate link: see our Affiliate Disclosure.
