How to make sure that a sinusoid wave with different subsequent frequencies matches up?
You need to make sure that the phase of continuous.  

The easiest way would be something like this.

x(n) = cos(phase);
phase = phase + 2*pi*current_frequency*sample_time;
current_frequency= update_frequency(n);
n = n + 1;


Note, that this is very inefficient code and only for illustrative purposes.