Now I have a serrate function f(x) with a minimal positive period $T=2$ (slope $k=1$ when $nT <t \le nT+T/2$ ; $k=-1$ when $nT+T/2<t \le nT+T$)
Given a data x, try to determine $t_0 \in [ 0, T]$, so that in the interval $[t_0, x+t_0]$, the area of the $f ( t )$ image above and below the line $y = f ( t_0 )$ is equal ( s.t.integral is 0 ), and the total length of the interval with the slope of 1 ( k > 0 ) is equal to the total length of the interval with the slope of -1 ( k < 0 ).
This may be a basic problem, but I am a beginner in matlab, and I do not know how to achieve this. My only idea is to cut the area into small triangles and trapezoids, but this is cumbersome and cannot be extended to general periodic functions. Can someone provide more appropriate code ? ( I hope to be able to output the expression of $t_0$ about x, but as far as I know, matlab may only be convenient to provide $t_0-x$ image, I do not know whether it can output the expression of piecewise function )

In fact, you don't need a program because there is a direct formula giving the final bound $x_f$ in the interval $[x,x+t_0]=[x_i,x_f]$ which is plainly
$$x_f=2[x+1]-x \ \iff \ t_0=2[x+1]-(x+1)$$
where $[a]$ denotes the integer part of $a$.
Anyway, as you want to "practice" Matlab, I have written the following program checking the exactness of the formula above for a random initial value $x$ covering all cases. Please note that I don't use initial function $f$, but its derivative.
Check that $az$ is always equal to $\approx 0$, whatever the value of $x$.