Different number of samples for different functions

浪尽此生 提交于 2019-12-23 15:27:40

问题


plot x+3 , x**2+5*x+12 

Is it possible to set x+3 to have only 2 samples and x**2+5*x+12 to have say 1000 samples in the same plot?


回答1:


It can be done, but not out-of-the-box.

The first variant uses a temporary file to save one function with a low sampling rate and plotting it later together with the high-resolution function:

set samples 2
set table 'tmp.dat'
plot x+3
unset table
set samples 1000
plot 'tmp.dat' w lp t 'x+3', x**2 + 5*x + 12

This has the advantage, that you can use any sampling rates for both functions.

For you special case of 2 samples for one function, it can be done without an external file, but it involves quite some tricking:

set xrange [-10:10]
s = 1000
set samples s
f1(x) = x + 3

set style func linespoints
set style data linespoints
plot '+' using (x0 = (($0 == 0 || $0 == (s-1) )? $1 : x0), \
                ($0 < (s-2) ? 1/0 : x0)):(f1(x0)) t 'x+3',\
     x**2 + 5*x + 12

What I did here is:

  1. Use the special filename + to generate a set of coordinates in the current xrange. This must be set, no autoscaling is possible.
  2. Skipping all points but the first and the last by giving them the value 1/0 doesn't work, because the two remaining points aren't connected.
  3. So I store the first x-value (when $0, or column(0) equals 0) and use it when I encountered the second last points. For the last points, the usual values are used.

That works for your special case of 2 samples.

You must keep in mind, that the first function is treated as data, so you must use both set style data and set style func (just to show it).

The result with 4.6.4 is:



来源:https://stackoverflow.com/questions/19524582/different-number-of-samples-for-different-functions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!