Why is this matrix not working in my program?

  • Thread starter Thread starter etotheipi
  • Start date Start date
  • Tags Tags
    Matrix Program
Click For Summary

Discussion Overview

The discussion revolves around a programming issue related to matrix creation and polynomial interpolation in Python, specifically using NumPy. Participants are troubleshooting code that is intended to solve a problem from Project Euler, focusing on the construction of a matrix and the subsequent calculations involving polynomial fitting.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant encounters an IndexError when attempting to assign values to the matrix M, suggesting a potential off-by-one error in indexing.
  • Another participant proposes correcting the indexing to M[x-1,y-1], which is acknowledged by the original poster.
  • Despite the indexing correction, the original poster reports that the code still does not yield the correct answer.
  • One participant expresses confusion regarding Python's range() function and suggests that it may be a source of error, recommending that all loops start at 0 for clarity.
  • Another participant suggests looking for a simpler method of polynomial interpolation as an alternative approach.
  • Multiple participants express frustration with the output of the code, indicating that it produces unexpected results.

Areas of Agreement / Disagreement

Participants generally agree that there are issues with the code, particularly regarding indexing and the output results. However, there is no consensus on the specific cause of the incorrect output or the best approach to resolve the problem.

Contextual Notes

Limitations include potential misunderstandings of Python's indexing and range behavior, as well as unresolved mathematical steps in the polynomial fitting process.

etotheipi
https://projecteuler.net/problem=101
Code:
import numpy as np
for j in range (1,11):
    M = np.empty([j, j])
    for x in range(1,j+1):
        for y in range(1,j+1):
            M[y,x] = y**(j-x)
    Minv = np.linalg.inv(M)
The ##j^{\mathrm{th}}## estimate ##\mathrm{OP}(j,n)## which fits ##j## data points is a ##(j-1)^{\mathrm{th}}## degree polynomial ##u^{(j)}(n) = a_{j-1} n^{j-1} + \dots + a_{0}## which agrees with ##u_n## on the set ##n \in \{1, \dots, j \}##, so$$\mathbf{a} := \begin{pmatrix} a_{j-1} \\ \vdots \\ a_0 \end{pmatrix} = \begin{pmatrix} 1 & 1 & \cdots & 1 \\ 2^{j-1} & 2^{j-2} & \cdots & 1 \\ \vdots & \vdots & & \vdots \\ j^{j-1} & j^{j-2} & \cdots & 1\end{pmatrix}^{-1} \begin{pmatrix} u_1 \\ \vdots \\ u_j \end{pmatrix} := \mathbf{M}^{-1} \mathbf{u}$$I need to make the matrix ##\mathbf{M}##, why does it not work?
Code:
M[y,x] = y**(j-x)
IndexError: index 1 is out of bounds for axis 0 with size 1
 
Last edited by a moderator:
Technology news on Phys.org
For the M[x,y] assignment do you mean M[x-1,y-1] ?
 
  • Like
Likes   Reactions: etotheipi
oh yeah that's it thanks :smile:
 
Last edited by a moderator:
It does not give me the correct answer :frown:
Python:
import numpy as np
sum = 0
def p(k):
    s = 0
    for i in range(0,11):
        s += ((-1)**i)*(k**i)
    return s
for j in range (1,11):
    M = np.empty(shape=(j,j))
    for x in range(1,j+1):
        for y in range(1,j+1):
            M[y-1,x-1] = y**(j-x)
    Minv = np.linalg.inv(M)
    U = np.empty(shape=(j,1))
    for i in range(1, j+1):
        U[i-1] = p(i)
    A = np.matmul(Minv, U)
    z = 1
    FIT_found = False
    while(FIT_found == False):
        estimate = 0
        for r in range(0,j):
            estimate += A[r]*z**r
        if(estimate != p(z)):
            sum += estimate
            FIT_found = True
        else:
            z = z + 1      
print(sum)
 
Last edited by a moderator:
etotheipi said:
It does not give me the correct answer :frown:
I think the idea of doing Project Euler problems is to work on your code until it does :smile:

When posting code here it helps if you add the language so syntax highlighting works:
Python:
import numpy as np
sum = 0
def p(k):
    s = 0
    for i in range(0,11):
        s += ((-1)**i)*(k**i)
    return s
for j in range (1,11):
    M = np.empty(shape=(j,j))
    for x in range(1,j+1):
        for y in range(1,j+1):
            M[y-1,x-1] = y**(j-x)
    Minv = np.linalg.inv(M)
    U = np.empty(shape=(j,1))
    for i in range(1, j+1):
        U[i-1] = p(i)
    A = np.matmul(Minv, U)
    z = 1
    FIT_found = False
    while(FIT_found == False):
        estimate = 0
        for r in range(0,j):
            estimate += A[r]*z**r
        if(estimate != p(z)):
            sum += estimate
            FIT_found = True
        else:
            z = z + 1
print(sum)
 
  • Like
Likes   Reactions: etotheipi
I always get confused by Python's range() function: note that range(1, 11) gives you [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]: this is the first thing I'd check.

And then i'd check that my j, x, and y values are doing what I want in the inner loop (either with an IDE or a simple print statement).

Edit: first of all I'd refactor it so all my loops start at 0, it makes it much easier to keep track.
 
  • Like
Likes   Reactions: etotheipi
thanks okay I'm going to finish this tomorrow because I have homework and my stupid code is giving me -670.99996864
 
  • Haha
Likes   Reactions: pbuk
etotheipi said:
thanks okay I'm going to finish this tomorrow because I have homework and my stupid code is giving me -670.99996864
:eek: yeah, I don't think that's right.
 
pbuk said:
I always get confused by Python's range() function: note that range(1, 11) gives you [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]: this is the first thing I'd check.

And then i'd check that my j, x, and y values are doing what I want in the inner loop (either with an IDE or a simple print statement).

Edit: first of all I'd refactor it so all my loops start at 0, it makes it much easier to keep track.

That’s how I checked the code. I called that seldom used secret programmers function:

Python:
print(‘j = %d, x = %d, y = %d’%(j,x,y))

just before his M assignment.
 
  • #10
I'd look for a simpler method of polynomial interpolation if I were you.
 

Similar threads

Replies
31
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 75 ·
3
Replies
75
Views
6K
Replies
2
Views
2K