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?
Related
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'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)
Is it possible to plot the surface of the Cusp Catastrophe with Gnuplot? Here's a comparable plot.
The catastrophe is mathematically described by: V = x^4 + a*x^2 + b*x (Wikipedia, see above)
The surface is describes by: 0 = a + b*y – y^3
I'm a gnu plot beginner, so I'd be really grateful for an example with source code.
(I study psychology and I need this plot for an essay about a clinical theory: the cusp catastrophe is used to describe changes of mood, that seem to appear more sudden and disruptive for depressive patients, compared to healthy persons.)
I think the best way would be to generate a table of roots of the equation (outside of gnuplot) and then plot the table with splot. But you can get an approximation to what you want in gnuplot by trying
a=b=1
set para
set iso 30
set pm3d at s
set urange [-10:10]
set hidd
set view 47,192
splot a + u*v - b * v**3, u, v
We want to calculate the value of an integral in linear plot.
For a better understanding look at the photo. Let's say the overall area is 1. We want to find what the value in a certain part is. For instance we want to know how much % of the overall 100% lay within the 10th and 11th month if everything refers to months and A as maximum stands for 24.
We can calculate a integral and then should be able to get the searched area by F(x) - F(x-1)
I thoght about the following code:
a <- 24
tab <-matrix(0,a,1)
tab <-cbind(seq(1,a),tab)
tab<-data.frame(tab)
#initialization for first point
tab[1,2] <- (2*tab[1,1] / a - tab[1,1]^2 / a^2)
#for loop for calculation of integral of each point - integral until to the area
for(i in 2:nrow(tab))
{tab[i,2] <- (2*tab[i,1] / a - tab[i,1]^2/ a^2) - sum(tab[1,2]:tab[i-1,2])}
#plotting
plot(tab[,2], type="l")
If you see the plot - it's confusing. Any ideas how to handle this correct?
The base R function integrate() can do this for you:
f <- function(x, A) 2/A - x / A^2
integrate(function(x)f(x, 24), lower=10, upper=11)
0.06510417 with absolute error < 7.2e-16
Using the formulas directly:
a <- 24 # number of divisions
x <- c(seq(1,a)) #
y <- x*2/a - x^2/a^2 # F(x)
z <- (x*2/a - x^2/a^2) - ((x-1)*2/a - (x-1)^2/a^2) # F(x) - F(x-1)
Then do the binding afterward.
> sum(z)
[1] 1
I need to plot a function f(x), where x is discrete set of values (in my case positive integers). I couldn't find a way to specify a step-size when using the range option and samples doesn't seem to be the right solution. Finally, I would like to approximate f(x) with a smooth function.
I don't quite understand why samples is not the solution to your problem.
If I want to plot sin(x) on an interval between 0 and 10 with a point at every integer I use
set xrange [0:10]
set sample 11
plot sin(x) w p
Obviously the number of samples is xmax-xmin+1 (10 - 0 + 1 = 11).
Finally to tackle the approximation problem have a look at this website which discusses linear least squares fitting. For simple linear interpolation use lp instead of p.
Or alternatively, play around with the ceil(x) or floor(x) functions.
Maybe have a look at this example:
http://gnuplot.sourceforge.net/demo/prob2.html
You can do:
plot [1:12] '+' u ($0):(f($0))
Where, $0 will be replaced by 1, 2, ..., 12. You can even do a smooth on this. For instance:
f(x)=sin(2*x)
plot [1:12] f(x) t 'the function'\
, '+' u ($0):(f($0)) t 'the points'\
, '+' u ($0):(f($0)) smooth cspline t 'the smooth'