# How do I define a function in parts for plotting?

I use gnuplot for plotting functions. This is my script at the moment:

``````set terminal png xffffff x222222
set output "Ausgleichszahlungen.png"
set title "Ausgleichszahlungen"
set xlabel 'F_MZ / A_MZ'
set ylabel 'Faktor F'
set xrange [0.5:1]
set yrange [0:1]
set key off
set xzeroaxis linetype -1 linewidth 0.5
set yzeroaxis linetype -1 linewidth 0.5
set xtics 0.1
set ytics 0.1
f(x) =                        0.75   * (1-x) -  317.0 /  20000
g(x) = 5.0/26 * (1-x) ** 2 + 35.0/52 * (1-x) - 2121.0 / 260000.0
h(x) = 13.0/7 * (1-x) ** 2 + 11.0/25 * (1-x)
plot f(x), g(x), h(x)
``````

f(x), g(x) and h(x) are one function:

-infinity < x < 0.8 : f(x)

0.8 <= x < 0.93 : g(x)

0.93 <= x < +infinity: h(x)

How can I do this?

-

Change the last line to

``````plot f(x)*(x<0.8) +  g(x) * (x>=0.8)*(x<0.93) + h(x)*(x>=0.93)
``````

I find that easy to read, but it has the disadvantage that all f(x), g(x) and h(x) will always be evaluated. You can also use the ternary conditional operator:

``````condition ? case1 : case2
``````

will evaluate to `case1` if `condition` is true and to `case2` if `condition` is false. You can nest those, so

``````plot x < 0.8 ? f(x) : x < 0.93 ? g(x) : h(x)
``````

will do the job for you.

-
Hi Jan, it works fine, thanks. gnuplot.info/docs_4.2/gnuplot.html is quite interesting if someone else is searching for basic problems in gnuplot. – Martin Thoma Jan 26 '11 at 20:11