【Canvas】勾画调和级数Harmonic series 曲线 y=1+1/2+1/3+1/4+1/5+1/6+1/7+1/8+....

相关资料:https://baike.baidu.com/item/%E8%B0%83%E5%92%8C%E7%BA%A7%E6%95%B0/8019971?fr=aladdin

调和级数(英语:Harmonic series)是一个发散的无穷级数。
调和级数是由调和数列各元素相加所得的和。中世纪后期的数学家Oresme证明了所有调和级数都是发散于无穷的。但是调和级数的拉马努金和存在,且为欧拉常数。

 

代码:

<!DOCTYPE html>
<html lang="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
     <title>调和级数曲线 作者:逆火狂飙 https://www.cnblogs.com/xiandedanteng/category/1133377.html</title>
    </head>

     <body onload="draw()">
        <canvas id="myCanvus" width="1330px" height="640px" style="border:1px dashed black;">
            出现文字表示你的浏览器不支持HTML5 作者:逆火狂飙 https://www.cnblogs.com/xiandedanteng/category/1133377.html
        </canvas>
     </body>
</html>
<script type="text/javascript">
<!--
    function draw(){
        // 注意:曲线和刻度的坐标和位置是不同的,坐标是数学中的实际值,位置则是Canvas画布上的实际点,两者需要通过步长进行转换
        // 步长即XY轴的比例,计算曲线、刻度时需要要位置除以步长,在实际描绘时需要再乘回来
        var X_STEP=6.25;// 定义X轴向的步长,即每个刻度之间的距离.X方向刻度在这里控制
        var Y_STEP=6.25;// 定义Y轴向的步长,即每个刻度之间的距离.Y方向刻度在这里控制

        // Initialize canvas
        var canvas=document.getElementById("myCanvus");
        var canvasWidth=canvas.width;
        var canvasHeight=canvas.height;
        var context=canvas.getContext("2d");
        
        // 清出白屏画黑线
        context.fillStyle = "white";
        context.fillRect(0, 0, canvasWidth, canvasHeight);
        context.strokeStyle = "black";
        context.fillStyle = "black";
        
        // 进行坐标变换:把原点放在左下角,东方为X轴正向,北方为Y轴正向
        var offsetY=50;// Y向偏移值,正值向上偏,用来画坐标轴
        var offsetX=50;// X向偏移值,正值向右偏,用来画坐标轴

        context.save();
        context.translate(0+offsetX,canvasHeight-offsetY);

        // 在旋转之前写文字,以免要转来转去
        draw_XAxisText(context,0,canvasWidth-100,X_STEP);
        draw_YAxisText(context,115-canvas.height,0,Y_STEP);
        drawTitles(context);

        context.rotate(getRad(180));
        context.scale(-1,1);        

        draw_XAxis(context,0,canvasWidth-100,X_STEP);        
        draw_YAxis(context,0,canvas.height-100,Y_STEP);  
        
        drawCurve(context,0,canvasWidth-100,X_STEP);       

        context.restore();        
    }

    // 计算后描绘曲线
    function drawCurve(ctx,start,end,step){
        var cds=[{}];// 用来容纳坐标

        var i=0,x=0,y=0.0,arr;
        for(i=step;i<=end;i+=step){   
            x=i/step;
            y+=1/x;
            console.log(x+","+y);
            arr={"x":x,"y":y};
            cds.push(arr);
    
        }

        paintCurve(ctx,"red",cds,step);
       
    }

    // 画曲线
    function paintCurve(ctx,color,cds,step){
        ctx.strokeStyle = color;
        ctx.beginPath();        
        for(var i=0; i<cds.length; i++){  
            ctx.lineTo(cds[i].x*step,cds[i].y*step);// 注意y轴比例
        }         
        ctx.stroke();
        ctx.closePath();
    }

    // 画Y轴的文字
    function draw_YAxisText(ctx,start,end,step){        
        ctx.lineWidth=0.5;
        ctx.strokeStyle='navy';
        ctx.fillStyle='navy';

        // 写文字
        var x=-19,y=5,index=1;
        for(y=start;y<end;y+=step){
            if (index % 5==0){
                ctx.fillText(-y/step,x,y);// 注意y轴比例
            }

            index++;
        }
    }

    // 画Y轴的轴线,箭头以及刻度
    function draw_YAxis(ctx,start,end,step){
        ctx.save();
        
        ctx.lineWidth=0.5;
        ctx.strokeStyle='navy';
        ctx.fillStyle='navy';

        var arrowEnd=end+12.5;

        // 画轴
        ctx.beginPath();
        ctx.moveTo(0, start);
        ctx.lineTo(0, arrowEnd);
        ctx.stroke();
        ctx.closePath();

        // 画箭头
        
        ctx.beginPath();
        ctx.moveTo(Math.sin(getRad(15))*10, arrowEnd-Math.cos(getRad(15))*10);
        ctx.lineTo(0, arrowEnd);
        ctx.lineTo(-Math.sin(getRad(15))*10, arrowEnd-Math.cos(getRad(15))*10);
        ctx.stroke();
        ctx.closePath();
        
        // 画刻度
        var x,y;
        x=5;
        for(y=start;y<end;y+=step){// 注意y轴比例
            ctx.beginPath();
            ctx.moveTo(x, y);
            ctx.lineTo(0, y);
            
            ctx.stroke();
            ctx.closePath();
        }
    }

    // 画X轴的轴线,箭头以及刻度
    function draw_XAxisText(ctx,start,end,step){        
        ctx.lineWidth=0.5;
        ctx.strokeStyle='navy';
        ctx.fillStyle='navy';

        // 写文字
        var i=0,x=0,y=5;
        var index=4;
        for(i=start;i<end;i+=step){
            index++;

            x=i/step;
            if(index % 5==0){
                ctx.fillText(x,i,y+10);
            }
        }
    }

    // 画X轴的轴线,箭头以及刻度
    function draw_XAxis(ctx,start,end,step){
        ctx.save();
        
        ctx.lineWidth=0.5;
        ctx.strokeStyle='navy';
        ctx.fillStyle='navy';

        var arrowEnd=end+12.5;

        // 画轴
        ctx.beginPath();
        ctx.moveTo(start, 0);
        ctx.lineTo(arrowEnd, 0);
        ctx.stroke();
        ctx.closePath();

        // 画箭头
        ctx.beginPath();
        ctx.moveTo(arrowEnd-Math.cos(getRad(15))*10, Math.sin(getRad(15))*10);
        ctx.lineTo(arrowEnd, 0);
        ctx.lineTo(arrowEnd-Math.cos(getRad(15))*10, -Math.sin(getRad(15))*10);
        ctx.stroke();
        ctx.closePath();
        
        // 画刻度
        var x,y;
        y=5;
        for(x=start;x<end;x+=step){
            ctx.beginPath();
            ctx.moveTo(x, 0);
            ctx.lineTo(x, y);
            
            ctx.stroke();
            ctx.closePath();
        }

        ctx.restore();
    }

    
    // 在画布上写说明文字
    function drawTitles(ctx){        
        ctx.lineWidth=0.5;
        ctx.strokeStyle='navy';
        ctx.font="bold 16px 宋体";
        ctx.fillStyle='navy';

        // 这两个位置自己根据感觉调,摆得合适不遮挡即可
        var x=800;
        var y=-550;

        // 写文字
        ctx.fillText("y=1+1/2+1/3+1/4+1/5+1/6+1/7+1/8+....",x,y);    
        ctx.fillText("调和级数Harmonic series 图线",x,y+20);    

        ctx.fillText("  绘制:逆火狂飙",x+170,y+40);
    }

    // 由角度得到弧度
    function getRad(degree){
        return degree/180*Math.PI;
    }

    // 截短长字符串
    function cutShort(str,length){
        if(str.length>length){
            str=str.substr(0,length)+"...";
        }
        
        return str;
    }
//-->
</script>

在console上输出的坐标点,看得出来y一直在增加,虽然增加得越来越慢,但一直没有趋近收敛:

tiaohe.html:64 1,1
tiaohe.html:64 2,1.5
tiaohe.html:64 3,1.8333333333333333
tiaohe.html:64 4,2.083333333333333
tiaohe.html:64 5,2.283333333333333
tiaohe.html:64 6,2.4499999999999997
tiaohe.html:64 7,2.5928571428571425
tiaohe.html:64 8,2.7178571428571425
tiaohe.html:64 9,2.8289682539682537
tiaohe.html:64 10,2.9289682539682538
tiaohe.html:64 11,3.0198773448773446
tiaohe.html:64 12,3.103210678210678
tiaohe.html:64 13,3.180133755133755
tiaohe.html:64 14,3.251562326562327
tiaohe.html:64 15,3.3182289932289937
tiaohe.html:64 16,3.3807289932289937
tiaohe.html:64 17,3.439552522640758
tiaohe.html:64 18,3.4951080781963135
tiaohe.html:64 19,3.547739657143682
tiaohe.html:64 20,3.597739657143682
tiaohe.html:64 21,3.6453587047627294
tiaohe.html:64 22,3.690813250217275
tiaohe.html:64 23,3.73429151108684
tiaohe.html:64 24,3.7759581777535067
tiaohe.html:64 25,3.8159581777535068
tiaohe.html:64 26,3.854419716215045
tiaohe.html:64 27,3.8914567532520823
tiaohe.html:64 28,3.927171038966368
tiaohe.html:64 29,3.9616537975870574
tiaohe.html:64 30,3.9949871309203906
tiaohe.html:64 31,4.02724519543652
tiaohe.html:64 32,4.05849519543652
tiaohe.html:64 33,4.08879822573955
tiaohe.html:64 34,4.118209990445433
tiaohe.html:64 35,4.146781419016861
tiaohe.html:64 36,4.174559196794639
tiaohe.html:64 37,4.201586223821666
tiaohe.html:64 38,4.22790201329535
tiaohe.html:64 39,4.2535430389363755
tiaohe.html:64 40,4.278543038936376
tiaohe.html:64 41,4.302933282838815
tiaohe.html:64 42,4.326742806648339
tiaohe.html:64 43,4.349998620601827
tiaohe.html:64 44,4.3727258933290996
tiaohe.html:64 45,4.394948115551322
tiaohe.html:64 46,4.416687245986104
tiaohe.html:64 47,4.4379638417307845
tiaohe.html:64 48,4.4587971750641175
tiaohe.html:64 49,4.4792053383294235
tiaohe.html:64 50,4.499205338329423
tiaohe.html:64 51,4.518813181466678
tiaohe.html:64 52,4.538043950697447
tiaohe.html:64 53,4.556911875225749
tiaohe.html:64 54,4.575430393744267
tiaohe.html:64 55,4.593612211926086
tiaohe.html:64 56,4.611469354783229
tiaohe.html:64 57,4.6290132144323515
tiaohe.html:64 58,4.646254593742697
tiaohe.html:64 59,4.6632037462850695
tiaohe.html:64 60,4.679870412951736
tiaohe.html:64 61,4.696263855574687
tiaohe.html:64 62,4.712392887832752
tiaohe.html:64 63,4.7282659037057675
tiaohe.html:64 64,4.7438909037057675
tiaohe.html:64 65,4.759275519090383
tiaohe.html:64 66,4.774427034241898
tiaohe.html:64 67,4.789352407376227
tiaohe.html:64 68,4.804058289729168
tiaohe.html:64 69,4.818551043352357
tiaohe.html:64 70,4.832836757638071
tiaohe.html:64 71,4.846921264680325
tiaohe.html:64 72,4.860810153569214
tiaohe.html:64 73,4.8745087837062
tiaohe.html:64 74,4.888022297219713
tiaohe.html:64 75,4.901355630553047
tiaohe.html:64 76,4.914513525289889
tiaohe.html:64 77,4.927500538276902
tiaohe.html:64 78,4.940321051097415
tiaohe.html:64 79,4.9529792789455165
tiaohe.html:64 80,4.965479278945517
tiaohe.html:64 81,4.977824957957862
tiaohe.html:64 82,4.9900200799090815
tiaohe.html:64 83,5.002068272680166
tiaohe.html:64 84,5.013973034584928
tiaohe.html:64 85,5.025737740467281
tiaohe.html:64 86,5.037365647444025
tiaohe.html:64 87,5.048859900317588
tiaohe.html:64 88,5.0602235366812245
tiaohe.html:64 89,5.0714594917374045
tiaohe.html:64 90,5.082570602848516
tiaohe.html:64 91,5.0935596138375265
tiaohe.html:64 92,5.104429179054918
tiaohe.html:64 93,5.115181867226961
tiaohe.html:64 94,5.125820165099301
tiaohe.html:64 95,5.136346480888775
tiaohe.html:64 96,5.146763147555442
tiaohe.html:64 97,5.157072425905957
tiaohe.html:64 98,5.1672765075386105
tiaohe.html:64 99,5.177377517639621
tiaohe.html:64 100,5.187377517639621
tiaohe.html:64 101,5.1972785077386305
tiaohe.html:64 102,5.207082429307258
tiaohe.html:64 103,5.216791167171336
tiaohe.html:64 104,5.226406551786721
tiaohe.html:64 105,5.235930361310531
tiaohe.html:64 106,5.245364323574681
tiaohe.html:64 107,5.254710117967204
tiaohe.html:64 108,5.263969377226464
tiaohe.html:64 109,5.273143689153069
tiaohe.html:64 110,5.282234598243978
tiaohe.html:64 111,5.291243607252987
tiaohe.html:64 112,5.300172178681558
tiaohe.html:64 113,5.3090217362036825
tiaohe.html:64 114,5.317793666028244
tiaohe.html:64 115,5.3264893182021575
tiaohe.html:64 116,5.33511000785733
tiaohe.html:64 117,5.343657016404339
tiaohe.html:64 118,5.3521315926755255
tiaohe.html:64 119,5.360534954020063
tiaohe.html:64 120,5.368868287353397
tiaohe.html:64 121,5.377132750163314
tiaohe.html:64 122,5.3853294714747895
tiaohe.html:64 123,5.393459552775602
tiaohe.html:64 124,5.401524068904634
tiaohe.html:64 125,5.409524068904634
tiaohe.html:64 126,5.417460576841142
tiaohe.html:64 127,5.425334592589174
tiaohe.html:64 128,5.433147092589174
tiaohe.html:64 129,5.44089903057367
tiaohe.html:64 130,5.448591338265977
tiaohe.html:64 131,5.456224926052237
tiaohe.html:64 132,5.463800683627995
tiaohe.html:64 133,5.471319480620476
tiaohe.html:64 134,5.47878216718764
tiaohe.html:64 135,5.4861895745950475
tiaohe.html:64 136,5.4935425157715185
tiaohe.html:64 137,5.5008417858445116
tiaohe.html:64 138,5.508088162656105
tiaohe.html:64 139,5.515282407260422
tiaohe.html:64 140,5.522425264403279
tiaohe.html:64 141,5.529517462984839
tiaohe.html:64 142,5.536559716505966
tiaohe.html:64 143,5.543552723498973
tiaohe.html:64 144,5.550497167943417
tiaohe.html:64 145,5.557393719667555
tiaohe.html:64 146,5.564243034736048
tiaohe.html:64 147,5.571045755824484
tiaohe.html:64 148,5.577802512581241
tiaohe.html:64 149,5.584513921977214
tiaohe.html:64 150,5.591180588643881
tiaohe.html:64 151,5.597803105200172
tiaohe.html:64 152,5.604382052568593
tiaohe.html:64 153,5.6109180002810115
tiaohe.html:64 154,5.617411506774518
tiaohe.html:64 155,5.623863119677744
tiaohe.html:64 156,5.630273376088001
tiaohe.html:64 157,5.636642802839593
tiaohe.html:64 158,5.642971916763644
tiaohe.html:64 159,5.649261224939744
tiaohe.html:64 160,5.655511224939744
tiaohe.html:64 161,5.661722405063967
tiaohe.html:64 162,5.66789524457014
tiaohe.html:64 163,5.674030213895294
tiaohe.html:64 164,5.680127774870903
tiaohe.html:64 165,5.686188380931509
tiaohe.html:64 166,5.692212477317051
tiaohe.html:64 167,5.698200501269147
tiaohe.html:64 168,5.704152882221528
tiaohe.html:64 169,5.710070041984842
tiaohe.html:64 170,5.715952394926019
tiaohe.html:64 171,5.721800348142393
tiaohe.html:64 172,5.727614301630765
tiaohe.html:64 173,5.733394648451574
tiaohe.html:64 174,5.739141774888355
tiaohe.html:64 175,5.744856060602641
tiaohe.html:64 176,5.750537878784459
tiaohe.html:64 177,5.756187596298584
tiaohe.html:64 178,5.761805573826673
tiaohe.html:64 179,5.767392166005444
tiaohe.html:64 180,5.772947721561
tiaohe.html:64 181,5.778472583439453
tiaohe.html:64 182,5.7839670889339585
tiaohe.html:64 183,5.789431569808276
tiaohe.html:64 184,5.794866352416971
tiaohe.html:64 185,5.800271757822377
tiaohe.html:64 186,5.805648101908399
tiaohe.html:64 187,5.810995695491286
tiaohe.html:64 188,5.816314844427456
tiaohe.html:64 189,5.821605849718462
tiaohe.html:64 190,5.826869007613198
tiaohe.html:64 191,5.832104609707439
tiaohe.html:64 192,5.837312943040772
tiaohe.html:64 193,5.8424942901910315
tiaohe.html:64 194,5.847648929366289
tiaohe.html:64 195,5.852777134494494
tiaohe.html:64 196,5.857879175310821

使用Java程序得到的坐标点:

1.0,1.0
2.0,1.5
3.0,1.8333333333333333
4.0,2.083333333333333
5.0,2.283333333333333
6.0,2.4499999999999997
7.0,2.5928571428571425
8.0,2.7178571428571425
9.0,2.8289682539682537
10.0,2.9289682539682538
11.0,3.0198773448773446
12.0,3.103210678210678
13.0,3.180133755133755
14.0,3.251562326562327
15.0,3.3182289932289937
16.0,3.3807289932289937
17.0,3.439552522640758
18.0,3.4951080781963135
19.0,3.547739657143682
20.0,3.597739657143682
21.0,3.6453587047627294
22.0,3.690813250217275
23.0,3.73429151108684
24.0,3.7759581777535067
25.0,3.8159581777535068
26.0,3.854419716215045
27.0,3.8914567532520823
28.0,3.927171038966368
29.0,3.9616537975870574
30.0,3.9949871309203906
31.0,4.02724519543652
32.0,4.05849519543652
33.0,4.08879822573955
34.0,4.118209990445433
35.0,4.146781419016861
36.0,4.174559196794639
37.0,4.201586223821666
38.0,4.22790201329535
39.0,4.2535430389363755
40.0,4.278543038936376
41.0,4.302933282838815
42.0,4.326742806648339
43.0,4.349998620601827
44.0,4.3727258933290996
45.0,4.394948115551322
46.0,4.416687245986104
47.0,4.4379638417307845
48.0,4.4587971750641175
49.0,4.4792053383294235
50.0,4.499205338329423
51.0,4.518813181466678
52.0,4.538043950697447
53.0,4.556911875225749
54.0,4.575430393744267
55.0,4.593612211926086
56.0,4.611469354783229
57.0,4.6290132144323515
58.0,4.646254593742697
59.0,4.6632037462850695
60.0,4.679870412951736
61.0,4.696263855574687
62.0,4.712392887832752
63.0,4.7282659037057675
64.0,4.7438909037057675
65.0,4.759275519090383
66.0,4.774427034241898
67.0,4.789352407376227
68.0,4.804058289729168
69.0,4.818551043352357
70.0,4.832836757638071
71.0,4.846921264680325
72.0,4.860810153569214
73.0,4.8745087837062
74.0,4.888022297219713
75.0,4.901355630553047
76.0,4.914513525289889
77.0,4.927500538276902
78.0,4.940321051097415
79.0,4.9529792789455165
80.0,4.965479278945517
81.0,4.977824957957862
82.0,4.9900200799090815
83.0,5.002068272680166
84.0,5.013973034584928
85.0,5.025737740467281
86.0,5.037365647444025
87.0,5.048859900317588
88.0,5.0602235366812245
89.0,5.0714594917374045
90.0,5.082570602848516
91.0,5.0935596138375265
92.0,5.104429179054918
93.0,5.115181867226961
94.0,5.125820165099301
95.0,5.136346480888775
96.0,5.146763147555442
97.0,5.157072425905957
98.0,5.1672765075386105
99.0,5.177377517639621
100.0,5.187377517639621
101.0,5.1972785077386305
102.0,5.207082429307258
103.0,5.216791167171336
104.0,5.226406551786721
105.0,5.235930361310531
106.0,5.245364323574681
107.0,5.254710117967204
108.0,5.263969377226464
109.0,5.273143689153069
110.0,5.282234598243978
111.0,5.291243607252987
112.0,5.300172178681558
113.0,5.3090217362036825
114.0,5.317793666028244
115.0,5.3264893182021575
116.0,5.33511000785733
117.0,5.343657016404339
118.0,5.3521315926755255
119.0,5.360534954020063
120.0,5.368868287353397
121.0,5.377132750163314
122.0,5.3853294714747895
123.0,5.393459552775602
124.0,5.401524068904634
125.0,5.409524068904634
126.0,5.417460576841142
127.0,5.425334592589174
128.0,5.433147092589174
129.0,5.44089903057367
130.0,5.448591338265977
131.0,5.456224926052237
132.0,5.463800683627995
133.0,5.471319480620476
134.0,5.47878216718764
135.0,5.4861895745950475
136.0,5.4935425157715185
137.0,5.5008417858445116
138.0,5.508088162656105
139.0,5.515282407260422
140.0,5.522425264403279
141.0,5.529517462984839
142.0,5.536559716505966
143.0,5.543552723498973
144.0,5.550497167943417
145.0,5.557393719667555
146.0,5.564243034736048
147.0,5.571045755824484
148.0,5.577802512581241
149.0,5.584513921977214
150.0,5.591180588643881
151.0,5.597803105200172
152.0,5.604382052568593
153.0,5.6109180002810115
154.0,5.617411506774518
155.0,5.623863119677744
156.0,5.630273376088001
157.0,5.636642802839593
158.0,5.642971916763644
159.0,5.649261224939744
160.0,5.655511224939744
161.0,5.661722405063967
162.0,5.66789524457014
163.0,5.674030213895294
164.0,5.680127774870903
165.0,5.686188380931509
166.0,5.692212477317051
167.0,5.698200501269147
168.0,5.704152882221528
169.0,5.710070041984842
170.0,5.715952394926019
171.0,5.721800348142393
172.0,5.727614301630765
173.0,5.733394648451574
174.0,5.739141774888355
175.0,5.744856060602641
176.0,5.750537878784459
177.0,5.756187596298584
178.0,5.761805573826673
179.0,5.767392166005444
180.0,5.772947721561
181.0,5.778472583439453
182.0,5.7839670889339585
183.0,5.789431569808276
184.0,5.794866352416971
185.0,5.800271757822377
186.0,5.805648101908399
187.0,5.810995695491286
188.0,5.816314844427456
189.0,5.821605849718462
190.0,5.826869007613198
191.0,5.832104609707439
192.0,5.837312943040772
193.0,5.8424942901910315
194.0,5.847648929366289
195.0,5.852777134494494
196.0,5.857879175310821
197.0,5.8629553174428
198.0,5.868005822493306
199.0,5.873030948121446

Java程序如下:

package com.hy;

/**
 * 计算调和级数
 * @author horn1
 *
 */
public class HarmonicSeries {
    public static void main(String[] args) {
        double y=0;
        
        for(double x=1;x<200;x++) {
            y+=1/x;
            System.out.println(x+","+y);
        }
    }
}

 

--END-- 2019年11月10日08:20:36

posted @ 2019-11-04 18:01  逆火狂飙  阅读(477)  评论(0编辑  收藏  举报
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东