I'm using gnuplot to fit data to a certain function with unknown coefficients. I can get gnuplot to plot the fitted function, but I can't figure out how to have gnuplot give me the actual equation of that function.
The main command is:
f(x) = a/(1+(b*(x-c)*(x-c)))
a=80
b=.001
c=70
fit f(x) "data.dat" using 1:2:3 via a,b,c
I want gnuplot to tell me the values it comes up with for a b and c. Or maybe I am misinterpreting this command and it is actually using the values I plugged in as a,b, and c. Can anyone help me out here?
After fitting the values are stored in the respective variables a, b and c
f(x) = a/(1+(b*(x-c)*(x-c)))
a=80
b=.001
c=70
fit f(x) "data.dat" using 1:2:3 via a,b,c
print a, b, c
plot "data.dat" w p, f(x) title sprintf("a=%e, b=%e, c=%e", a, b, c)
Related
I would like to draw a curve logW= a+b*logP, where a = 12, b = -0.8
R = -0.4 of this curve. I would like to use data and look how this curve fits to data. Can I do it in gnuplot?
log(f(x)) = a+b*log(P)
a = 12, b = -0.8
fit f(x) 'data.txt' u 1:2 w p via a,b
plot f(x) 'data.txt' u 1:2
Does it sounds ok?
You are close, but there are little problems everywhere. Let's try to clean it up.
1) You must define f(x) itself, not log(f(x)). It is not clear from your statement, but I assume 'P' is the independent variable x?
f(P) = exp(a + b*log(P))
2) gnuplot commands are separated by semicolons, not commas
a = 12; b = -0.8
3) 'fit' is not a plotting style, so "with points" makes no sense as part of a fit command
fit f(x) 'data.txt' using 1:2 via a,b
4) The 'plot' command has two separate pieces: the data and the curve fit to it
plot 'data.txt' using 1:2 with points, f(x) with lines
I'm having some troubles fitting a dataset to calculate the Response Time of a Thermometer. So i have my dataset in the form of
Time (s) - Temperature (K)
0.4820 295.0772
0.4840 295.0772
0.4860 295.1651
0.4880 295.1651
0.4900 295.1651
0.4920 295.2531
0.4940 295.2091
0.4960 295.2531
0.4980 295.2972
0.5000 295.3412
0.5020 295.2972
0.5040 295.3853
0.5060 295.3412
and i want to linearize only the second column by doing the operation
y = log($2 - 325.6)
so i've written my .gp file this way
f(x) = a*x+b
fit f(x) 'termom_COST_SCALED.txt' via a, b u 1:(log($2 - 325.6))
p 'termom_COST_SCALED.txt' u 1:(log($2 - 325.6)) title 'T(t)',
f(x) title "Linear fit"
but someway is not working, even if i plot the graph without the fit
p 'termom_COST_SCALED.txt' u 1:(log($2 - 325.6))
the outcome is the graph desired, wich i want to make the fit from. Is the syntax of the fit wrong?
via a, b u 1:(log($2 - 325.6))
Thanks!
I think using should come before via, and you should ignore the header in your data file with every ::1:
f(x) = a*x+b
fit f(x) 'termom_COST_SCALED.txt' u 1:(log($2 - 325.6)) every ::1 via a,b
p 'termom_COST_SCALED.txt' u 1:(log($2 - 325.6)) every ::1 title 'T(t)',\
f(x) title "Linear fit"
pause mouse
The syntax is now correct.
Your 2nd column is close to 295, which means that $2-325.6 is close to -31, and its log will be complex :
gnuplot> print log(-31)
{3.43398720448515, 3.14159265358979}
Is it really what you want?
I have a question regarding plotting a exponential fit in Gnuplot and having the y-axis set in logscale. I am using Gnuplot 4.6.3.
I will present the few ways I have tried setting a range to fit in:
The function I want to fit on the data is f(x)=a+b*x
The first:
set log y
f(x)=a+b*x
fit [4:8] f(x) 'CT2A_OH_R_log.dat' using (log($1)):($2) via a,b
Resutls:
Read 15 points
Skipped 15 points outside range [x=4:8]
No data to fit
error during fit
The second:
fit f(x) 'CT2A_OH_R_log.dat' using (log($1)):($2) via a,b
Results:
Final set of parameters Asymptotic Standard Error
a =16.9858 +/- 6.299 (37.08%)
b =-8.43215 +/- 3.502 (41.53%)
gnuplot> plot 'CT2A_OH_R_log.dat', f(x) via a,b
Obviously the fit is ridiculous but this is the only way I know of that actually gives me a fit. I know the problem most likely lies within the defining of the xrange when fitting but what else can I write there to fix this?
Below I will show image of my plot (f(x) is not showing as it is plotted in the negative area.)
'cause a logscale of the y axis requires the logarithm of the y axis during the fit ;-)
fit [4:8] f(x) 'CT2A_OH_R_log.dat' u 1:(log($2)) via a,b
NOTE
If you want to use the logarithm based on 10, you have to use log($2)/log(10) or log10($2).
#Tom Solid
gnuplot> fit [4:8] [0.1:60] f(x) 'CT2A_OH_R_log.dat' using 1:(log($2))
via a,b
gnuplot> plot 'CT2A_OH_R_log.dat', f(x) via a,b
This time the fit is exponential like.
So, I will post the picture of what I am getting now:
I am trying to fit a linear curve, and then plot the graph with the fitted line as well. However, the following does not work, as gnuplot makes a 0 kbyte .png output.
set t push
set term png
fit f(x) "NET_OUTPUT.dat" u 1:2 via a,b
plot "NET_OUTPUT.dat" using 1:2 with points, f(x)
unset output
set term pop
If the fit line is not included, I can plot without problem.
I suspect that the problem is that the fit.log file is written, and the gnuplot does not finish the writing of the output file.
Does somebody know what is the problem?
Thanks a lot!
For gnuplot when plotting to a png I typically create a script like:
#!/usr/bin/gnuplot
set term png
set output "out.png"
f(x) = a*x + b
fit f(x) "data.dat" u 1:2 via a,b
plot "data.dat" u 1:2, f(x)
and call it with ./plot.gnu or I create a script like:
#!/usr/bin/gnuplot
set term png
f(x) = a*x + b
fit f(x) "data.dat" u 1:2 via a,b
plot "data.dat" u 1:2, f(x)
and call it with ./plot.gnu > out.png.
The first way asks gnuplot to write a png to the specified output file. The second ask gnuplot to generate the png but doesn't set a location (so it gets dumped to standard output), in this case I redirect the standard output (in bash with >) to my png file.
You can also pass the script into gnuplot with gnuplot plot.gnu or gnuplot plot.gnu > out.png on the command line in which case the lines beginning with # are ignored as comments.
I have a function z=f(x,y) and want to plot it using octave, but don't want the plot to be in 3d, as in
octave:1> x=(1:300);
octave:2> y=(1:300);
octave:3> [xx,yy]=meshgrid(x,y);
octave:4> A=sin(xx/100).*yy;
octave:5> mesh(x,y,A)
but rather in 2d using colors for the values of z, like what you get using the gnuplot instruction
gnuplot> plot 'a.txt' matrix w image
if I save the matrix A in the file a.txt. The closest I have found is the command contourf, but the as you can see if you try it,
octave:7> contourf(xx,yy,A)
the result is far from optimal... Any suggestion?
Thanks
imagesc will plot a matrix of your "z" values using colors:
> imagesc(x, y, A)
This will be inverted vertically compared to contourf, but that's easily fixed:
> imagesc(x, flipud(y), flipud(A))
And in your example you don't even need to provide the variables x and y:
> imagesc(A)
> imagesc(flipud(A))