Let me explain the situation: I took a new job which requires me to move to a new town for the duration of 6 months. However, since my friends and partner are living in my old hometown, I’ll probably travel there most of the weekends. And since I don’t have a car, I will be traveling by train.
The Deutsche Bahn offers several discounts for people who travel a lot. For a fixed price, you can get either 25% or 50% off for every ticket you buy for the duration of one year. So the question we asked here is: Which offer saves me the most money?
When I was facing this problem, I thought the best solution would be a visual one, that is, make a plot and directly see how often I would have to travel for each discount to be the cheapest. As a tool to do this plots I decided to use GnuPlot, which I’m familiar with from my studies. Any other plotting tool works equally well, though.
A ticket for one trip costs 23 euros, given that I have to take the return as well, that is 46 euros per weekend without discount. The “Bahncard 25” which yields a 25% discount for one year costs 62 Euros, the “Bahncard 50”, which yields 50% discount for one year costs 255 Euros.
For each situation, we want to plot a function gives the total cost over the number of travels. In the undiscounted case, calling our function f, that would be as simple as
f(x) = 46*x.
Where x is the number of travels. For the Bahncard 25, we obtain
g(x) = 62 + (46-(0.25*46))*x = 62 + 0.75*46*x.
62 is the flat price for the Bahncard, which we only pay once, for each trip, we pay 46 Euros minus 25%, that is, 46 – 0.25*46. But subtracting 25% is the same as just paying 75% of the original price, hence we just use the final factor of 0.75*46.
Analogously, we obtain the function for the Bahncard 50 to be given by
h(x) = 255 + 0.50*46*x
All we have to do now is to put this data into GnuPlot. Starting up GnuPlot for the first time you will see a screen like this
This might look intimidating at first, but trust me, it is not that hard to use. In fact, we can enter our functions the same way as we did here on the blog:
Now GnuPlot knows what f, g, and h are. So all we have to do is to tell it to plot them over a reasonable range. Given that I am in the new town for 6 months, which is roughly 24 weekends (assuming 4 weekends per month), 0 to 30 might be a reasonable range.
We can do this by simply typing
plot [0:30] f(x),g(x),h(x)
plot just tells GnuPlot to plot something in 2D, in the squared brackets we give the range of our plot—in our case 0 to 30—and then we just give a list of the functions we want to plot, separated by commata. Hitting enter, you should receive the following graph:
Hovering over the intersections, in the bottom left corner you can see the exact coordinates of the mouse cursor. Hence we can evaluate that:
- If I do less than 6 trips, it is cheaper to take no Bahncard at all,
- if I do between 6 and 16 trips, the Bahncard 25 is cheapest, and
- if I do more than 16 trips, the Bahncard 50 will be the cheapest solution for me.
Neatening things up
Having the graphs called f(x), g(x), h(x) isn’t really helpful when just looking at the plot. Especially if you have more than three cases. We can fix this by changing the plot function to
plot [0:30] f(x) title "No Bahncard",g(x) title "Bahncard 25",h(x) title "Bahncard 50"
Then our graph looks like this:
Also, we can put some labels on the axis, using the commands “set xlabel” and “set ylabel”. For example, if we write
set xlabel "Number of trips" set ylabel "total cost"
and then the usual plot command, our graph will look like this:
To enhance readability, we can activate a grid behind the graphs by using the “set grid” command. By default, this will show gridlines at the labels, in our case that is vertical lines at 5, 10, 15, 20 and 25 on the x-axis and horizontal lines at 200, 400, 600, 800, 1000 and 1200 on the y-axis. We can further customize this by using the “set xtics” and “set ytics” to change the positions of the labels on the axis. In our case having a vertical line at every number and no vertical lines might be most helpful, so we write
set xtics 1 unset ytics set grid
before our plot command to receive the following graph:
The Full Code
f(x) = 46*x g(x) = 62 + 0.75*46*x h(x) = 255 + 0.50*46*x set xlabel "Number of trips" set ylabel "total cost" set xtics 1 unset ytics set grid plot [0:30] f(x) title "No Bahncard",g(x) title "Bahncard 25",h(x) title "Bahncard 50"