tjzel
tjzel

Reputation: 115

Gnuplot - fitting function to a certain value, problem with the last (first) row of data file

Of late I have been troubling with two issues during some simple plotting.

I'm trying to plot several functions (I suppose the syntax for that can be much simplier), where every one of them fits to just one row of data.

Data file:

11[V] 2.92 4.64 0.04 0.04 0.031[mA]
9[V] 3.32 4.72 0.04 0.04 0.025[mA]
8[V] 3.52 4.76 0.04 0.04 0.022[mA]
7[V] 3.72 4.80 0.04 0.04 0.019[mA]
6[V] 3.92 4.88 0.04 0.04 0.016[mA]
4[V] 4.32 4.92 0.04 0.04 0.010[mA]

Gnuplot commands:

set terminal epscairo color enhanced
set encoding utf8
set xrange[2.8:4.6]
set key left top
set grid
set xlabel 'Napięcie na złączu kolektor-emiter [V]'
set ylabel 'Prąd płynący przez złącze kolektor-emiter [mA]'
set output "dat1_2.eps"
f1(x)=a1*tanh(x)
fit f1(x) "dat1_2.dat" every ::::1 u 2:(($3-0.65)/0.25) via a1
f2(x)=a2*tanh(x)
fit f2(x) "dat1_2.dat" every ::1::2 u 2:(($3-0.65)/0.25) via a2
f3(x)=a3*tanh(x)
fit f3(x) "dat1_2.dat" every ::2::3 u 2:(($3-0.65)/0.25) via a3
f4(x)=a4*tanh(x)
fit f4(x) "dat1_2.dat" every ::3::4 u 2:(($3-0.65)/0.25) via a4
f5(x)=a5*tanh(x)
fit f5(x) "dat1_2.dat" every ::4::5 u 2:(($3-0.65)/0.25) via a5
f6(x)=a6*tanh(x)
fit f6(x) "dat1_2.dat" every ::5 u 2:(($3-0.65)/0.25) via a6
#there is a long plotting line below
plot "dat1_2.dat" using 2:(($3-0.65)/0.25):4:($5/0.25) with xyerrorbar title "Zależność zmierzona", "dat1_2.dat" u ($2+0.07):(($3-0.65)/0.25-0.05):6 with labels notitle font 'Verdana,7.5', f1(x) notitle lt rgb "blue", f2(x) notitle lt rgb "blue", f3(x) notitle lt rgb "blue", f4(x) notitle lt rgb "blue", f5(x) notitle lt rgb "blue", f6(x) notitle lt rgb "blue"

The result of this is:

[...]
variance of residuals (reduced chisquare) = WSSR/ndf   : 0.0116479
Final set of parameters            Asymptotic Standard Error
=======================            ==========================
a5              = 17.0097          +/- 0.07636      (0.4489%)
         Read 1 points
         line 0: No data to fit

         line 0: undefined variable: a6

I have very little idea how to make gnuplot use the last (first?) row. Making a simple trick of doubling the last row of the data file yields expected result but is too nasty for me and I would like to understand what happens here.

The second issue is, how to make gnuplot fit a function to go through a certain point. Let me show what I mean:

https://i.sstatic.net/uXFpH.png

As you can see, fitted functions are not going 'through' the values computed (neglecting errorbars). That's quite surprising for me, as having only one point to fit to I expected gnuplot to do what I mentioned. What command would fit f functions in this desired fashion?

Upvotes: 2

Views: 180

Answers (1)

Christoph
Christoph

Reputation: 48390

With every ::::0, or every ::1::2 you select two points, through which gnuplot can fit, but every ::5 selects only a single point through which gnuplot cannot fit.

However, from your explanation I conclude, that you only need to compute the correct scaling factor given a single point. Given the values in a single row are x0 and y0, your function through this point is

f0(x) = (y0 - 0.65)/(0.25 * tanh(x0)) * tanh(x)

To get the correct point for calculating I first plot the single point and while doing that, I save the coordinates in two variable, which are used subsequently for plotting the respective function:

y(ydat) = (ydat - 0.65)/0.25
plot "dat1_2.dat" using (x0=$2):(y0=y($3)):4:($5/0.25) every ::::0 with xyerrorbars title "first",\
 y0/tanh(x0) * tanh(x) lc rgb "blue" notitle

Using array, which is available since gnuplot 5.2, this can be nicely plotted in loops over all lines:

y(ydat) = (ydat - 0.65)/0.25
set xrange [2.8:4.6]
N = 6
array X[6]
array Y[6]
array Labels[6] = [ "first", "second", "third", "fourth", "fifth", "sixth" ]
plot for [i=1:N] "dat1_2.dat" using (X[i]=$2):(Y[i]=y($3)):4:($5/0.25) every ::(i-1)::(i-1) with xyerrorbars title Labels[i],\
     for [i=1:N] Y[i]/tanh(X[i]) * tanh(x) lc rgb "blue" notitle

enter image description here

Upvotes: 1

Related Questions