Fitting functions to your data

If we want to get serious about fitting functions to our data, we can turn to gnuplot's sophisticated fit command.

Getting ready

You need the same datafile used in the previous recipe, rs.dat. The following figure shows the same noisy sine wave, but this time the overlaid curve seems to be perfectly smooth:

Getting ready

How to do it…

The following script will produce the previous figure:

f(x) = a*sin(b*x)
fit f(x) 'rs.dat' via a, b
plot 'rs.dat' with lines lw 0.5 notitle, f(x) lw 4 title "Fit by gnuplot"

How it works…

gnuplot's fit command takes a function defined by the user containing several parameters and attempts to find the set ...

Get gnuplot Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.