Ask Your Question

Revision history [back]

You should tell us different approaches, and what was wrong with them. I am not sure whether you ask about defining a function or plotting it. Here would be my direct approach, without more informations about your needs. To define a periodic function, use the .frac() method of real numbers. In your case:

sage: f = lambda x: 1 if RR(x).frac() < 1/2 else 0
sage: plot(f,0,4)

If you do not like the vertical line at the discontinuities, you can look at the options of the plot() function:

sage: plot?

In your case, you can try:

sage: plot(f, 0, 4, plot_points='1000', linestyle='', marker='.')

You should tell us different approaches, and what was wrong with them. I am not sure whether you ask about defining a function or plotting it. Here would be my direct approach, without more informations about your needs. To define a periodic function, use the .frac() method of real numbers. In your case:

sage: f = lambda x: 1 if RR(x).frac() < 1/2 else 0
sage: plot(f,0,4)

If you do not like the vertical line at the discontinuities, you can look at the options of the plot() function:

sage: plot?

In your case, you can try:

sage: plot(f, 0, 4, plot_points='1000', linestyle='', marker='.')

For the integral, since the finction is defined point by points (not a symbolic function), you can do a numerical integration:

sage: numerical_integral(f,0,4)
(2.0, 2.2315482794965646e-14)

You should tell us about your different approaches, and what was wrong with them. I am not sure whether you ask about defining a function or plotting it. Here would be my direct approach, without more informations about your needs. To define a periodic function, use the .frac() method of real numbers. In your case:

sage: f = lambda x: 1 if RR(x).frac() < 1/2 else 0
sage: plot(f,0,4)

If you do not like the vertical line at the discontinuities, you can look at the options of the plot() function:

sage: plot?

In your case, you can try:

sage: plot(f, 0, 4, plot_points='1000', linestyle='', marker='.')

For the integral, since the finction is defined point by points (not a symbolic function), you can do a numerical integration:

sage: numerical_integral(f,0,4)
(2.0, 2.2315482794965646e-14)

You should tell us about your different approaches, and what was wrong with them. I am not sure whether you ask about defining a function or plotting it. Here would be my direct lazy approach, without more informations about your needs. To define a periodic function, use the .frac() method of real numbers. In your case:

sage: f = lambda x: 1 if RR(x).frac() < 1/2 else 0
sage: plot(f,0,4)
plot(f, 0, 4)

If you do not like the vertical line at the discontinuities, you can look at the options of the plot() function:

sage: plot?

In your case, you can try:

sage: plot(f, 0, 4, plot_points='1000', linestyle='', marker='.')

For the integral, since the finction is defined point by points (not a symbolic function), you can do a numerical integration:

sage: numerical_integral(f,0,4)
numerical_integral(f, 0, 4)
(2.0, 2.2315482794965646e-14)