How to draw graph of Gauss function?

风流意气都作罢 提交于 2019-12-12 15:08:28

问题


Gauss function has an infinite number of jump discontinuities at x = 1/n, for positive integers.

I want to draw diagram of Gauss function.

Using Maxima cas I can draw it with simple command :

  f(x):= 1/x - floor(1/x);  plot2d(f(x),[x,0,1]);

but the result is not good ( near x=0 it should be like here)

Also Maxima claims:

plot2d: expression evaluates to non-numeric value somewhere in plotting range.

I can define picewise function ( jump discontinuities at x = 1/n, for positive integers )

so I tried :

define( g(x), for i:2 thru 20 step 1 do if (x=i) then x else (1/x) - floor(1/x));

but it don't works.

I can also use chebyshew polynomials to aproximate function ( like in : A Graduate Introduction to Numerical Methods From the Viewpoint of Backward Error Analysis by Corless, Robert, Fillion, Nicolas)

How to do it properly ?


回答1:


For plot2d you can set the adapt_depth and nticks parameters. The default values are 5 and 29, respectively. set_plot_option() (i.e. with no argument) returns the current list of option values. If you increase adapt_depth and/or nticks, then plot2d will use more points for plotting. Perhaps that makes the figure look good enough.

Another way is to use the draw2d function (in the draw package) and explicitly tell it to plot each segment. We know that there are discontinuities at 1/k, for k = 1, 2, 3, .... We have to decide how many segments to plot. Let's say 20.

(%i6) load (draw) $
(%i7) f(x):= 1/x - floor(1/x) $
(%i8) makelist (explicit (f, x, 1/(k + 1), 1/k), k, 1, 20);
(%o8) [explicit(f,x,1/2,1),explicit(f,x,1/3,1/2),
       explicit(f,x,1/4,1/3),explicit(f,x,1/5,1/4),
       explicit(f,x,1/6,1/5),explicit(f,x,1/7,1/6),
       explicit(f,x,1/8,1/7),explicit(f,x,1/9,1/8),
       explicit(f,x,1/10,1/9),explicit(f,x,1/11,1/10),
       explicit(f,x,1/12,1/11),explicit(f,x,1/13,1/12),
       explicit(f,x,1/14,1/13),explicit(f,x,1/15,1/14),
       explicit(f,x,1/16,1/15),explicit(f,x,1/17,1/16),
       explicit(f,x,1/18,1/17),explicit(f,x,1/19,1/18),
       explicit(f,x,1/20,1/19),explicit(f,x,1/21,1/20)]
(%i9) apply (draw2d, %);



回答2:


I have made a list of segments with ending points. The result is :

and full code is here

Edit: smaller size with shorter lists in case of almost straight lines,

if (n>20) then iMax:10 else iMax : 250,

in the GivePart function



来源:https://stackoverflow.com/questions/49587741/how-to-draw-graph-of-gauss-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!