构建范德蒙德矩阵使用for循环。

huangapple go评论62阅读模式
英文:

Constructing vandermonde matrix using for loop

问题

I am trying to create a matrix using a for loop. The goal of the for loop is to construct a Vandermonde matrix using the years 2010 up until 2022.

x = np.arange(2010, 2022, 1)
y = np.array([143335, 141079, 136607, 132865, 129394, 125637, 122645, 121102, 116426, 110234, 97208, 96123])
vandermonde = np.zeros((12, 12))
for i in range(12):
    for j in range(12):
        vandermonde[i,j] = x[i]**j
        print(vandermonde)
        print(x[i]**j)
        print("end j=", j)

The problem is that at x[0]**3 my loop is giving me -469333592.

The correct answer should be 2010^3 = 8120601000. I think my loop is running correctly since it is filling in the correct spots in the right order, but I can't figure out what is causing this problem.

Any help is greatly appreciated.

Additionally, I made sure that my thinking was correct with the following:

power2010 = np.zeros((12,1))

for i in range(12):
    power2010 = 2010**i
    print(power2010)

I tried to look at each run through of the nested for loop to see if there were any errors with the way the matrix was being written, but I didn't find any errors. I think I have the correct method of coding as I tested a for loop for a single value, 2010, and calculated exponents 2010^0 up until 2010^11.

英文:

I am trying to create a matrix using a for loop. The goal of the for loop is to construct a Vandermonde matrix using the years 2010 up until 2022.

x = np.arange(2010, 2022, 1) 
y = np.array([143335, 141079, 136607, 132865, 129394, 125637, 122645, 121102, 116426, 110234, 97208, 96123])
vandermonde = np.zeros((12, 12))
for i in range(12):
    for j in range(12):
        vandermonde[i,j] = x[i]**j
        print(vandermonde)
        print(x[i]**j)
        print("end j=", j)

The problem is that at x[0]**3 my loop is giving me -469333592.

The correct answer should be 2010^3 = 8120601000. I think my loop is running correctly since it is filling in the correct spots in the right order, but I can't figure out what is causing this problem.

Any help is greatly appreciated.

Additionally, I made sure that my thinking was correct with the following:

power2010 = np.zeros((12,1))

for i in range(12):
    power2010 = 2010**i
    print(power2010)

I tried to look at each run through of the nested for loop to see if there were any errors with the way the matrix was being written, but I didn't find any errors. I think I have the correct method of coding as I tested a for loop for a single value, 2010, and calculated exponents 2010^0 up until 2010^11.

答案1

得分: 2

由于溢出,您得到了负数,您可以将数组更改为np.float64

x = np.arange(2010, 2022, 1, dtype=np.float64)  # 将其更改为 np.float64

# 百万吨纸张消耗量
y = np.array([143335, 141079, 136607, 132865, 129394, 125637, 122645, 121102, 116426, 110234, 97208, 96123])

# 初始化范德蒙矩阵
vandermonde = np.zeros((12, 12))

# i 控制行
for i in range(12):

    # j 控制列
    for j in range(12):
        vandermonde[i,j] = x[i]**j
        print(vandermonde)
        print(x[i]**j)
        print("end j=", j)

# 我的代码结束

# 输出
1.0
end j= 0
2010.0
end j= 1
4040100.0
end j= 2
8120601000.0
end j= 3
16322408010000.0
end j= 4
3.28080401001e+16
end j= 5
6.5944160601201e+19
end j= 6
1.3254776280841401e+23
end j= 7
2.6642100324491215e+26
end j= 8
5.3550621652227346e+29
end j= 9
1.0763674952097696e+33
end j= 10
2.163498665371637e+36
end j= 11
1.0
end j= 0
2011.0
end j= 1
4044121.0
end j= 2
8132727331.0
end j= 3
16354914662641.0
end j= 4
3.2889733386571052e+16
end j= 5
6.614125384039439e+19
end j= 6
1.3301006147303311e+23
end j= 7
2.6748323362226958e+26
end j= 8
5.379087828143841e+29
end j= 9
1.0817345622397264e+33
end j= 10
2.1753682046640898e+36
end j= 11
1.0
end j= 0
2012.0
end j= 1
4048144.0
end j= 2
8144865728.0
end j= 3
16387469844736.0
end j= 4
3.297158932760883e+16
end j= 5
6.6338837727148966e+19
end j= 6
1.3347374150702373e+23
end j= 7
2.6854916791213173e+26
end j= 8
5.4032092583920904e+29
end j= 9
1.0871257027884886e+33
end j= 10
2.1872969140104392e+36
end j= 11
1.0
end j= 0
2013.0
end j= 1
4052169.0
end j= 2
8157016197.0
end j= 3
16420073604561.0
end j= 4
3.3053608165981292e+16
end j= 5
6.653691323812034e+19
end j= 6
1.3393880634833625e+23
end j= 7
2.696188171792009e+26
end j= 8
5.427426789817313e+29
end j= 9
1.0925410127902252e+33
end j= 10
2.1992850587467235e+36
end j= 11
1.0
end j= 0
2014.0
end j= 1
4056196.0
end j= 2
8169178744.0
end j= 3
16452725990416.0
end j= 4
3.3135790144697824e+16
end j= 5
6.673548135142142e+19
end j= 6
1.3440525944176274e+23
end j= 7
2.7069219251571014e+26
end j= 8
5.4517407572664026e+29
end j= 9
1.0979805885134534e+33
end j= 10
2.2113329052660953e+36
end j= 11
1.0
end j= 0
2015.0
end j= 1
4060225.0
end j= 2
8181353375.0
end j= 3
16485427050625.0
end j= 4
3.3218135507009376e+16
end j= 5
6.693454304662389e+19
end j= 6
1.3487310423894714e+23
end j= 7
2.717693050414785e+26
end j= 8
5.476151496585791e+29
end j= 9
1.103444526562037e+33
end j= 10
2.2234407210225044e+36
end j= 11
1.0
end j= 0
2016.0
end j= 1
4064256.0
end j= 2
8193540096.0
end j= 3
16518176833536.0
end j= 4
3.3300644496408576e+16
end j= 5
6.713409930475969e+19
end j= 6
1.3534234419839553e+23
end j= 7
2.728501659039654e+26
end j= 8
5.500659344623943e+29
end j= 9
1.1089329238761868e+33
end j= 10
2.2356087745343927e+36
end j= 11
1.0
end j= 0
2017.0
end j= 1
406

<details>
<summary>英文:</summary>

You are getting negative numbers because of the overflow; you can make your array `np.float64`:

    x = np.arange(2010, 2022, 1, dtype=np.float64)  # change it to np.float64
    
    #million metric tons of paper consumed
    
    y = np.array([143335, 141079, 136607, 132865, 129394, 125637, 122645, 121102, 116426, 110234, 97208, 96123])
    
    #initializing vandermonde matrix
    
    vandermonde = np.zeros((12, 12))
    
    #i controls rows
    
    for i in range(12):
    
        #j controls columns
    
        for j in range(12):
            vandermonde[i,j] = x[i]**j
            print(vandermonde)
            print(x[i]**j)
            print(&quot;end j=&quot;, j)
    
    #end of my code

    #output
    1.0
    end j= 0
    2010.0
    end j= 1
    4040100.0
    end j= 2
    8120601000.0
    end j= 3
    16322408010000.0
    end j= 4
    3.28080401001e+16
    end j= 5
    6.5944160601201e+19
    end j= 6
    1.3254776280841401e+23
    end j= 7
    2.6642100324491215e+26
    end j= 8
    5.3550621652227346e+29
    end j= 9
    1.0763674952097696e+33
    end j= 10
    2.163498665371637e+36
    end j= 11
    1.0
    end j= 0
    2011.0
    end j= 1
    4044121.0
    end j= 2
    8132727331.0
    end j= 3
    16354914662641.0
    end j= 4
    3.2889733386571052e+16
    end j= 5
    6.614125384039439e+19
    end j= 6
    1.3301006147303311e+23
    end j= 7
    2.6748323362226958e+26
    end j= 8
    5.379087828143841e+29
    end j= 9
    1.0817345622397264e+33
    end j= 10
    2.1753682046640898e+36
    end j= 11
    1.0
    end j= 0
    2012.0
    end j= 1
    4048144.0
    end j= 2
    8144865728.0
    end j= 3
    16387469844736.0
    end j= 4
    3.297158932760883e+16
    end j= 5
    6.6338837727148966e+19
    end j= 6
    1.3347374150702373e+23
    end j= 7
    2.6854916791213173e+26
    end j= 8
    5.4032092583920904e+29
    end j= 9
    1.0871257027884886e+33
    end j= 10
    2.1872969140104392e+36
    end j= 11
    1.0
    end j= 0
    2013.0
    end j= 1
    4052169.0
    end j= 2
    8157016197.0
    end j= 3
    16420073604561.0
    end j= 4
    3.3053608165981292e+16
    end j= 5
    6.653691323812034e+19
    end j= 6
    1.3393880634833625e+23
    end j= 7
    2.696188171792009e+26
    end j= 8
    5.427426789817313e+29
    end j= 9
    1.0925410127902252e+33
    end j= 10
    2.1992850587467235e+36
    end j= 11
    1.0
    end j= 0
    2014.0
    end j= 1
    4056196.0
    end j= 2
    8169178744.0
    end j= 3
    16452725990416.0
    end j= 4
    3.3135790144697824e+16
    end j= 5
    6.673548135142142e+19
    end j= 6
    1.3440525944176274e+23
    end j= 7
    2.7069219251571014e+26
    end j= 8
    5.4517407572664026e+29
    end j= 9
    1.0979805885134534e+33
    end j= 10
    2.2113329052660953e+36
    end j= 11
    1.0
    end j= 0
    2015.0
    end j= 1
    4060225.0
    end j= 2
    8181353375.0
    end j= 3
    16485427050625.0
    end j= 4
    3.3218135507009376e+16
    end j= 5
    6.693454304662389e+19
    end j= 6
    1.3487310423894714e+23
    end j= 7
    2.717693050414785e+26
    end j= 8
    5.476151496585791e+29
    end j= 9
    1.103444526562037e+33
    end j= 10
    2.2234407210225044e+36
    end j= 11
    1.0
    end j= 0
    2016.0
    end j= 1
    4064256.0
    end j= 2
    8193540096.0
    end j= 3
    16518176833536.0
    end j= 4
    3.3300644496408576e+16
    end j= 5
    6.713409930475969e+19
    end j= 6
    1.3534234419839553e+23
    end j= 7
    2.728501659039654e+26
    end j= 8
    5.500659344623943e+29
    end j= 9
    1.1089329238761868e+33
    end j= 10
    2.2356087745343927e+36
    end j= 11
    1.0
    end j= 0
    2017.0
    end j= 1
    4068289.0
    end j= 2
    8205738913.0
    end j= 3
    16550975387521.0
    end j= 4
    3.3383317356629856e+16
    end j= 5
    6.733415110832242e+19
    end j= 6
    1.3581298278548632e+23
    end j= 7
    2.7393478627832593e+26
    end j= 8
    5.5252646392338334e+29
    end j= 9
    1.1144458777334643e+33
    end j= 10
    2.2478373353883975e+36
    end j= 11
    1.0
    end j= 0
    2018.0
    end j= 1
    4072324.0
    end j= 2
    8217949832.0
    end j= 3
    16583822760976.0
    end j= 4
    3.346615433164957e+16
    end j= 5
    6.753469944126883e+19
    end j= 6
    1.3628502347248049e+23
    end j= 7
    2.7502317736746564e+26
    end j= 8
    5.5499677192754564e+29
    end j= 9
    1.1199834857497871e+33
    end j= 10
    2.2601266742430705e+36
    end j= 11
    1.0
    end j= 0
    2019.0
    end j= 1
    4076361.0
    end j= 2
    8230172859.0
    end j= 3
    16616719002321.0
    end j= 4
    3.35491556656861e+16
    end j= 5
    6.7735745289020236e+19
    end j= 6
    1.3675846973853184e+23
    end j= 7
    2.7611535040209582e+26
    end j= 8
    5.5747689246183146e+29
    end j= 9
    1.1255458458804377e+33
    end j= 10
    2.2724770628326038e+36
    end j= 11
    1.0
    end j= 0
    2020.0
    end j= 1
    4080400.0
    end j= 2
    8242408000.0
    end j= 3
    16649664160000.0
    end j= 4
    3.36323216032e+16
    end j= 5
    6.7937289638464e+19
    end j= 6
    1.3723332506969728e+23
    end j= 7
    2.772113166407885e+26
    end j= 8
    5.599668596143928e+29
    end j= 9
    1.1311330564210734e+33
    end j= 10
    2.2848887739705682e+36
    end j= 11
    1.0
    end j= 0
    2021.0
    end j= 1
    4084441.0
    end j= 2
    8254655261.0
    end j= 3
    16682658282481.0
    end j= 4
    3.37156523888941e+16
    end j= 5
    6.813933347795498e+19
    end j= 6
    1.3770959295894702e+23
    end j= 7
    2.783110873700319e+26
    end j= 8
    5.624667075748345e+29
    end j= 9
    1.1367452160087405e+33
    end j= 10
    2.2973620815536645e+36
    end j= 11
    



</details>



huangapple
  • 本文由 发表于 2023年5月13日 14:17:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76241363.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定