2

Is its possible in gnuplot to plot and fit a function that has two variables? For example, a physical function that depends on hight h and Temperature T where the T dependence should only be calculated but not plotted (for f, h and T experimental data exists):

f(h,T) = a * h * (1 + alpha * T) + f0

where a and f0 are to be determined by the fit, alpha is known. In the end I need a plot with f on the y-axis and h on the x-axis. The whole T dependence should be taken care of in the fit, but I don't need it displayed with splot.

The following is what I tried and failed. I assume because one can't set two dummy variables:

set term png;
set output 'test.png';
set dummy h;
set dummy T;
f(h,T) = a * h * (1 + alpha * T) + f0;
fit f(h,T) 'data.txt' using 2:4:1 via a, f0;
plot f(h,T);

gives undefined variable: h. Any ideas?

Foo Bar
  • 1,764
  • 4
  • 24
  • 43
  • have you tried removing the `set dummy` stuff and just do `fit f(x,y) 'data.txt' using 2:4:1:(1) via a,f0`? – mgilson Feb 06 '13 at 16:49

1 Answers1

3

From examples in the documentation:

Examples:
       f(x) = a*x**2 + b*x + c
       g(x,y) = a*x**2 + b*y**2 + c*x*y
       FIT_LIMIT = 1e-6
       fit f(x) 'measured.dat' via 'start.par'
       fit f(x) 'measured.dat' using 3:($7-5) via 'start.par'
       fit f(x) './data/trash.dat' using 1:2:3 via a, b, c
       fit g(x,y) 'surface.dat' using 1:2:3:(1) via a, b, c

I would expect your script to work if you simply did:

set term png
set output 'test.png'
f(h,T) = a * h * (1 + alpha * T) + f0
fit f(x,y) 'data.txt' using 2:4:1:(1) via a, f0

set view 90,0  #possibly `set view 0,90` or `set view map`?
splot f(x,y)
mgilson
  • 300,191
  • 65
  • 633
  • 696
  • What does the ":(1)" mean in "using"? – Foo Bar Feb 06 '13 at 17:01
  • It's the weight that each point should be given (e.g. for error). see `help fit` in gnuplot. This weights each point the same. – mgilson Feb 06 '13 at 17:04
  • Ah, ok. But nevertheless. If I switch all to x and y and remove the dummy, it get the error `line 5: undefined variable: y` (line 5 is `plot`). Do I have to enable multi variable plots somewhere first? Fitting seems to works just fine (I get reasonable values), but plotting does not. – Foo Bar Feb 06 '13 at 17:07
  • @FooBar -- Sorry, I wasn't thinking. You're plotting a function of 2 variables, so you're going to need `splot` instead of `plot`. Then instead of `x` and `y`, the variables become `u` and `v`. I've updated so hopefully it'll work now. – mgilson Feb 06 '13 at 18:08
  • But `splot` plots a surface plot (3D), but I want to fit `f(h,T)`, but plot only the projection on `f(h)` (2D). So the projection from the 3D vector `(h, T, f(h,T))` on `(h, 0, f(h,T)) -> (h, f(h,T))`. Such that if I'd look at the 3D `f(h,T)` plot from the specific angle that shows only the `f(h)` part. – Foo Bar Feb 06 '13 at 18:25
  • @FooBar -- Do you mean `plot f(x,0)`? – mgilson Feb 06 '13 at 18:27
  • I want the plot of the projection of the 3D plot on the f-h plane. – Foo Bar Feb 06 '13 at 18:30
  • @FooBar -- I'm not sure how to project a 3d surface onto a 2d plane. You can plot the full 3d surface setting the view appropriately ... – mgilson Feb 06 '13 at 19:32
  • @FooBar -- I've edited, adding a couple of `set view ...` options which *might* be what you want ... – mgilson Feb 06 '13 at 19:43